|
| 1 | +#include <linux/kernel.h> |
1 | 2 | #include <linux/sched.h> |
2 | 3 | #include <linux/init.h> |
3 | 4 | #include <linux/module.h> |
4 | 5 | #include <linux/timer.h> |
| 6 | +#include <linux/acpi_pmtmr.h> |
| 7 | + |
| 8 | +#include <asm/hpet.h> |
5 | 9 |
|
6 | 10 | unsigned int cpu_khz; /* TSC clocks / usec, not used here */ |
7 | 11 | EXPORT_SYMBOL(cpu_khz); |
@@ -84,3 +88,130 @@ int __init notsc_setup(char *str) |
84 | 88 | #endif |
85 | 89 |
|
86 | 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 | + */ |
| 98 | +static 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 | + */ |
| 119 | +static 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 | + |
| 185 | +out: |
| 186 | + return tsc_khz_val; |
| 187 | +} |
| 188 | + |
| 189 | +unsigned 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 */ |
| 196 | +int 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 | + |
| 215 | +EXPORT_SYMBOL(recalibrate_cpu_khz); |
| 216 | + |
| 217 | +#endif /* CONFIG_X86_32 */ |
0 commit comments