blob: 553f8fd23cc4733d0edafa862b95446f7a04bab1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Jiri Hladkye01b70e2008-06-02 12:00:19 +02007 * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Paul Gortmakere6830142016-07-13 20:18:57 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sched.h>
Andrew Morton941e4922008-02-06 01:36:42 -080017#include <linux/timex.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080018#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/delay.h>
john stultz6f84fa22006-06-26 00:25:11 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/processor.h>
22#include <asm/delay.h>
23#include <asm/timer.h>
Huang Ruib466bdb2015-08-10 12:19:54 +020024#include <asm/mwait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#ifdef CONFIG_SMP
john stultz6f84fa22006-06-26 00:25:11 -070027# include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29
john stultz6f84fa22006-06-26 00:25:11 -070030/* simple loop based delay: */
31static void delay_loop(unsigned long loops)
32{
Glauber Costaf0fbf0a2008-07-03 12:35:41 -030033 asm volatile(
Jiri Hladkye01b70e2008-06-02 12:00:19 +020034 " test %0,%0 \n"
35 " jz 3f \n"
36 " jmp 1f \n"
37
38 ".align 16 \n"
39 "1: jmp 2f \n"
40
41 ".align 16 \n"
Glauber Costaff1b15b2008-06-24 09:27:19 -030042 "2: dec %0 \n"
Jiri Hladkye01b70e2008-06-02 12:00:19 +020043 " jnz 2b \n"
Glauber Costaff1b15b2008-06-24 09:27:19 -030044 "3: dec %0 \n"
Jiri Hladkye01b70e2008-06-02 12:00:19 +020045
46 : /* we don't need output */
47 :"a" (loops)
48 );
john stultz6f84fa22006-06-26 00:25:11 -070049}
50
51/* TSC based delay: */
Thomas Gleixnera7f42552012-03-09 20:55:10 +010052static void delay_tsc(unsigned long __loops)
john stultz6f84fa22006-06-26 00:25:11 -070053{
Andy Lutomirski9cfa1a02015-06-25 18:44:00 +020054 u64 bclock, now, loops = __loops;
Steven Rostedt5c1ea082008-05-25 11:13:32 -040055 int cpu;
john stultz6f84fa22006-06-26 00:25:11 -070056
Steven Rostedt5c1ea082008-05-25 11:13:32 -040057 preempt_disable();
58 cpu = smp_processor_id();
Andy Lutomirski03b97302015-06-25 18:44:08 +020059 bclock = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040060 for (;;) {
Andy Lutomirski03b97302015-06-25 18:44:08 +020061 now = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040062 if ((now - bclock) >= loops)
63 break;
64
65 /* Allow RT tasks to run */
66 preempt_enable();
67 rep_nop();
68 preempt_disable();
69
70 /*
71 * It is possible that we moved to another CPU, and
72 * since TSC's are per-cpu we need to calculate
73 * that. The delay must guarantee that we wait "at
74 * least" the amount of time. Being moved to another
75 * CPU could make the wait longer but we just need to
76 * make sure we waited long enough. Rebalance the
77 * counter for this CPU.
78 */
79 if (unlikely(cpu != smp_processor_id())) {
80 loops -= (now - bclock);
81 cpu = smp_processor_id();
Andy Lutomirski03b97302015-06-25 18:44:08 +020082 bclock = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040083 }
84 }
Andrew Morton35d5d082007-11-14 17:00:41 -080085 preempt_enable();
john stultz6f84fa22006-06-26 00:25:11 -070086}
87
88/*
Huang Ruib466bdb2015-08-10 12:19:54 +020089 * On some AMD platforms, MWAITX has a configurable 32-bit timer, that
90 * counts with TSC frequency. The input value is the loop of the
91 * counter, it will exit when the timer expires.
92 */
93static void delay_mwaitx(unsigned long __loops)
94{
95 u64 start, end, delay, loops = __loops;
96
Janakarajan Natarajan88d879d2017-04-25 16:44:03 -050097 /*
98 * Timer value of 0 causes MWAITX to wait indefinitely, unless there
99 * is a store on the memory monitored by MONITORX.
100 */
101 if (loops == 0)
102 return;
103
Huang Ruib466bdb2015-08-10 12:19:54 +0200104 start = rdtsc_ordered();
105
106 for (;;) {
107 delay = min_t(u64, MWAITX_MAX_LOOPS, loops);
108
109 /*
110 * Use cpu_tss as a cacheline-aligned, seldomly
111 * accessed per-cpu variable as the monitor target.
112 */
Borislav Petkov84477332016-03-09 21:56:22 +0100113 __monitorx(raw_cpu_ptr(&cpu_tss), 0, 0);
Huang Ruib466bdb2015-08-10 12:19:54 +0200114
115 /*
116 * AMD, like Intel, supports the EAX hint and EAX=0xf
117 * means, do not enter any deep C-state and we use it
118 * here in delay() to minimize wakeup latency.
119 */
120 __mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
121
122 end = rdtsc_ordered();
123
124 if (loops <= end - start)
125 break;
126
127 loops -= end - start;
128
129 start = end;
130 }
131}
132
133/*
john stultz6f84fa22006-06-26 00:25:11 -0700134 * Since we calibrate only once at boot, this
135 * function should be set once at boot and not changed
136 */
137static void (*delay_fn)(unsigned long) = delay_loop;
138
139void use_tsc_delay(void)
140{
Huang Ruib466bdb2015-08-10 12:19:54 +0200141 if (delay_fn == delay_loop)
142 delay_fn = delay_tsc;
143}
144
145void use_mwaitx_delay(void)
146{
147 delay_fn = delay_mwaitx;
john stultz6f84fa22006-06-26 00:25:11 -0700148}
149
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800150int read_current_timer(unsigned long *timer_val)
john stultz6f84fa22006-06-26 00:25:11 -0700151{
152 if (delay_fn == delay_tsc) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200153 *timer_val = rdtsc();
john stultz6f84fa22006-06-26 00:25:11 -0700154 return 0;
155 }
156 return -1;
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159void __delay(unsigned long loops)
160{
john stultz6f84fa22006-06-26 00:25:11 -0700161 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300163EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165inline void __const_udelay(unsigned long xloops)
166{
Jiri Slaby4c45c512017-01-19 12:47:30 +0100167 unsigned long lpj = this_cpu_read(cpu_info.loops_per_jiffy) ? : loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int d0;
john stultz6f84fa22006-06-26 00:25:11 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 xloops *= 4;
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300171 asm("mull %%edx"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 :"=d" (xloops), "=&a" (d0)
Jiri Slaby4c45c512017-01-19 12:47:30 +0100173 :"1" (xloops), "0" (lpj * (HZ / 4)));
john stultz6f84fa22006-06-26 00:25:11 -0700174
175 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300177EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179void __udelay(unsigned long usecs)
180{
john stultz6f84fa22006-06-26 00:25:11 -0700181 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300183EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185void __ndelay(unsigned long nsecs)
186{
john stultz6f84fa22006-06-26 00:25:11 -0700187 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700189EXPORT_SYMBOL(__ndelay);