delay.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/delay.h>
  15. #include <asm/processor.h>
  16. #include <asm/delay.h>
  17. #include <asm/timer.h>
  18. #ifdef CONFIG_SMP
  19. # include <asm/smp.h>
  20. #endif
  21. /* simple loop based delay: */
  22. static void delay_loop(unsigned long loops)
  23. {
  24. int d0;
  25. __asm__ __volatile__(
  26. "\tjmp 1f\n"
  27. ".align 16\n"
  28. "1:\tjmp 2f\n"
  29. ".align 16\n"
  30. "2:\tdecl %0\n\tjns 2b"
  31. :"=&a" (d0)
  32. :"0" (loops));
  33. }
  34. /* TSC based delay: */
  35. static void delay_tsc(unsigned long loops)
  36. {
  37. unsigned long bclock, now;
  38. rdtscl(bclock);
  39. do {
  40. rep_nop();
  41. rdtscl(now);
  42. } while ((now-bclock) < loops);
  43. }
  44. /*
  45. * Since we calibrate only once at boot, this
  46. * function should be set once at boot and not changed
  47. */
  48. static void (*delay_fn)(unsigned long) = delay_loop;
  49. void use_tsc_delay(void)
  50. {
  51. delay_fn = delay_tsc;
  52. }
  53. int read_current_timer(unsigned long *timer_val)
  54. {
  55. if (delay_fn == delay_tsc) {
  56. rdtscl(*timer_val);
  57. return 0;
  58. }
  59. return -1;
  60. }
  61. void __delay(unsigned long loops)
  62. {
  63. delay_fn(loops);
  64. }
  65. inline void __const_udelay(unsigned long xloops)
  66. {
  67. int d0;
  68. xloops *= 4;
  69. __asm__("mull %0"
  70. :"=d" (xloops), "=&a" (d0)
  71. :"1" (xloops), "0"
  72. (cpu_data[raw_smp_processor_id()].loops_per_jiffy * (HZ/4)));
  73. __delay(++xloops);
  74. }
  75. void __udelay(unsigned long usecs)
  76. {
  77. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  78. }
  79. void __ndelay(unsigned long nsecs)
  80. {
  81. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  82. }
  83. EXPORT_SYMBOL(__delay);
  84. EXPORT_SYMBOL(__const_udelay);
  85. EXPORT_SYMBOL(__udelay);
  86. EXPORT_SYMBOL(__ndelay);