delay.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Precise Delay Loops for i386
  4. *
  5. * Copyright (C) 1993 Linus Torvalds
  6. * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  7. * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
  8. *
  9. * The __delay function must _NOT_ be inlined as its execution time
  10. * depends wildly on alignment on many x86 processors. The additional
  11. * jump magic is needed to get the timing stable on all the CPU's
  12. * we have to worry about.
  13. */
  14. #include <linux/export.h>
  15. #include <linux/sched.h>
  16. #include <linux/timex.h>
  17. #include <linux/preempt.h>
  18. #include <linux/delay.h>
  19. #include <asm/processor.h>
  20. #include <asm/delay.h>
  21. #include <asm/timer.h>
  22. #include <asm/mwait.h>
  23. #ifdef CONFIG_SMP
  24. # include <asm/smp.h>
  25. #endif
  26. /* simple loop based delay: */
  27. static void delay_loop(unsigned long loops)
  28. {
  29. asm volatile(
  30. " test %0,%0 \n"
  31. " jz 3f \n"
  32. " jmp 1f \n"
  33. ".align 16 \n"
  34. "1: jmp 2f \n"
  35. ".align 16 \n"
  36. "2: dec %0 \n"
  37. " jnz 2b \n"
  38. "3: dec %0 \n"
  39. : /* we don't need output */
  40. :"a" (loops)
  41. );
  42. }
  43. /* TSC based delay: */
  44. static void delay_tsc(unsigned long __loops)
  45. {
  46. u64 bclock, now, loops = __loops;
  47. int cpu;
  48. preempt_disable();
  49. cpu = smp_processor_id();
  50. bclock = rdtsc_ordered();
  51. for (;;) {
  52. now = rdtsc_ordered();
  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. bclock = rdtsc_ordered();
  72. }
  73. }
  74. preempt_enable();
  75. }
  76. /*
  77. * On some AMD platforms, MWAITX has a configurable 32-bit timer, that
  78. * counts with TSC frequency. The input value is the loop of the
  79. * counter, it will exit when the timer expires.
  80. */
  81. static void delay_mwaitx(unsigned long __loops)
  82. {
  83. u64 start, end, delay, loops = __loops;
  84. /*
  85. * Timer value of 0 causes MWAITX to wait indefinitely, unless there
  86. * is a store on the memory monitored by MONITORX.
  87. */
  88. if (loops == 0)
  89. return;
  90. start = rdtsc_ordered();
  91. for (;;) {
  92. delay = min_t(u64, MWAITX_MAX_LOOPS, loops);
  93. /*
  94. * Use cpu_tss_rw as a cacheline-aligned, seldomly
  95. * accessed per-cpu variable as the monitor target.
  96. */
  97. __monitorx(raw_cpu_ptr(&cpu_tss_rw), 0, 0);
  98. /*
  99. * AMD, like Intel's MWAIT version, supports the EAX hint and
  100. * EAX=0xf0 means, do not enter any deep C-state and we use it
  101. * here in delay() to minimize wakeup latency.
  102. */
  103. __mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
  104. end = rdtsc_ordered();
  105. if (loops <= end - start)
  106. break;
  107. loops -= end - start;
  108. start = end;
  109. }
  110. }
  111. /*
  112. * Since we calibrate only once at boot, this
  113. * function should be set once at boot and not changed
  114. */
  115. static void (*delay_fn)(unsigned long) = delay_loop;
  116. void use_tsc_delay(void)
  117. {
  118. if (delay_fn == delay_loop)
  119. delay_fn = delay_tsc;
  120. }
  121. void use_mwaitx_delay(void)
  122. {
  123. delay_fn = delay_mwaitx;
  124. }
  125. int read_current_timer(unsigned long *timer_val)
  126. {
  127. if (delay_fn == delay_tsc) {
  128. *timer_val = rdtsc();
  129. return 0;
  130. }
  131. return -1;
  132. }
  133. void __delay(unsigned long loops)
  134. {
  135. delay_fn(loops);
  136. }
  137. EXPORT_SYMBOL(__delay);
  138. void __const_udelay(unsigned long xloops)
  139. {
  140. unsigned long lpj = this_cpu_read(cpu_info.loops_per_jiffy) ? : loops_per_jiffy;
  141. int d0;
  142. xloops *= 4;
  143. asm("mull %%edx"
  144. :"=d" (xloops), "=&a" (d0)
  145. :"1" (xloops), "0" (lpj * (HZ / 4)));
  146. __delay(++xloops);
  147. }
  148. EXPORT_SYMBOL(__const_udelay);
  149. void __udelay(unsigned long usecs)
  150. {
  151. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  152. }
  153. EXPORT_SYMBOL(__udelay);
  154. void __ndelay(unsigned long nsecs)
  155. {
  156. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  157. }
  158. EXPORT_SYMBOL(__ndelay);