aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/tsc.c
diff options
context:
space:
mode:
authorAlok Kataria <akataria@vmware.com>2008-07-01 14:43:24 -0400
committerIngo Molnar <mingo@elte.hu>2008-07-09 01:43:25 -0400
commitbfc0f5947afa5e3a13e55867f4478c8a92c11dca (patch)
treebb642adee69c7804eaafbb7fad66af61b4f9f56f /arch/x86/kernel/tsc.c
parent0ef95533326a7b37d16025af9edc0c18e644b346 (diff)
x86: merge tsc calibration
Merge the tsc calibration code for the 32bit and 64bit kernel. The paravirtualized calculate_cpu_khz for 64bit now points to the correct tsc_calibrate code as in 32bit. Original native_calculate_cpu_khz for 64 bit is now called as calibrate_cpu. Also moved the recalibrate_cpu_khz function in the common file. Note that this function is called only from powernow K7 cpu freq driver. Signed-off-by: Alok N Kataria <akataria@vmware.com> Signed-off-by: Dan Hecht <dhecht@vmware.com> Cc: Dan Hecht <dhecht@vmware.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'arch/x86/kernel/tsc.c')
-rw-r--r--arch/x86/kernel/tsc.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 5d0be778fadd..e6ee14533c75 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -1,7 +1,11 @@
1#include <linux/kernel.h>
1#include <linux/sched.h> 2#include <linux/sched.h>
2#include <linux/init.h> 3#include <linux/init.h>
3#include <linux/module.h> 4#include <linux/module.h>
4#include <linux/timer.h> 5#include <linux/timer.h>
6#include <linux/acpi_pmtmr.h>
7
8#include <asm/hpet.h>
5 9
6unsigned int cpu_khz; /* TSC clocks / usec, not used here */ 10unsigned int cpu_khz; /* TSC clocks / usec, not used here */
7EXPORT_SYMBOL(cpu_khz); 11EXPORT_SYMBOL(cpu_khz);
@@ -84,3 +88,130 @@ int __init notsc_setup(char *str)
84#endif 88#endif
85 89
86__setup("notsc", notsc_setup); 90__setup("notsc", notsc_setup);
91
92#define MAX_RETRIES 5
93#define SMI_TRESHOLD 50000
94
95/*
96 * Read TSC and the reference counters. Take care of SMI disturbance
97 */
98static u64 __init tsc_read_refs(u64 *pm, u64 *hpet)
99{
100 u64 t1, t2;
101 int i;
102
103 for (i = 0; i < MAX_RETRIES; i++) {
104 t1 = get_cycles();
105 if (hpet)
106 *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
107 else
108 *pm = acpi_pm_read_early();
109 t2 = get_cycles();
110 if ((t2 - t1) < SMI_TRESHOLD)
111 return t2;
112 }
113 return ULLONG_MAX;
114}
115
116/**
117 * tsc_calibrate - calibrate the tsc on boot
118 */
119static unsigned int __init tsc_calibrate(void)
120{
121 unsigned long flags;
122 u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
123 int hpet = is_hpet_enabled();
124 unsigned int tsc_khz_val = 0;
125
126 local_irq_save(flags);
127
128 tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
129
130 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
131
132 outb(0xb0, 0x43);
133 outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
134 outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
135 tr1 = get_cycles();
136 while ((inb(0x61) & 0x20) == 0);
137 tr2 = get_cycles();
138
139 tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
140
141 local_irq_restore(flags);
142
143 /*
144 * Preset the result with the raw and inaccurate PIT
145 * calibration value
146 */
147 delta = (tr2 - tr1);
148 do_div(delta, 50);
149 tsc_khz_val = delta;
150
151 /* hpet or pmtimer available ? */
152 if (!hpet && !pm1 && !pm2) {
153 printk(KERN_INFO "TSC calibrated against PIT\n");
154 goto out;
155 }
156
157 /* Check, whether the sampling was disturbed by an SMI */
158 if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX) {
159 printk(KERN_WARNING "TSC calibration disturbed by SMI, "
160 "using PIT calibration result\n");
161 goto out;
162 }
163
164 tsc2 = (tsc2 - tsc1) * 1000000LL;
165
166 if (hpet) {
167 printk(KERN_INFO "TSC calibrated against HPET\n");
168 if (hpet2 < hpet1)
169 hpet2 += 0x100000000ULL;
170 hpet2 -= hpet1;
171 tsc1 = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
172 do_div(tsc1, 1000000);
173 } else {
174 printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
175 if (pm2 < pm1)
176 pm2 += (u64)ACPI_PM_OVRRUN;
177 pm2 -= pm1;
178 tsc1 = pm2 * 1000000000LL;
179 do_div(tsc1, PMTMR_TICKS_PER_SEC);
180 }
181
182 do_div(tsc2, tsc1);
183 tsc_khz_val = tsc2;
184
185out:
186 return tsc_khz_val;
187}
188
189unsigned long native_calculate_cpu_khz(void)
190{
191 return tsc_calibrate();
192}
193
194#ifdef CONFIG_X86_32
195/* Only called from the Powernow K7 cpu freq driver */
196int recalibrate_cpu_khz(void)
197{
198#ifndef CONFIG_SMP
199 unsigned long cpu_khz_old = cpu_khz;
200
201 if (cpu_has_tsc) {
202 cpu_khz = calculate_cpu_khz();
203 tsc_khz = cpu_khz;
204 cpu_data(0).loops_per_jiffy =
205 cpufreq_scale(cpu_data(0).loops_per_jiffy,
206 cpu_khz_old, cpu_khz);
207 return 0;
208 } else
209 return -ENODEV;
210#else
211 return -ENODEV;
212#endif
213}
214
215EXPORT_SYMBOL(recalibrate_cpu_khz);
216
217#endif /* CONFIG_X86_32 */