1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/timer.h>
6 #include <linux/acpi_pmtmr.h>
10 unsigned int cpu_khz; /* TSC clocks / usec, not used here */
11 EXPORT_SYMBOL(cpu_khz);
13 EXPORT_SYMBOL(tsc_khz);
16 * TSC can be unstable due to cpufreq or due to unsynced TSCs
20 /* native_sched_clock() is called before tsc_init(), so
21 we must start with the TSC soft disabled to prevent
22 erroneous rdtsc usage on !cpu_has_tsc processors */
23 int tsc_disabled = -1;
26 * Scheduler clock - returns current time in nanosec units.
28 u64 native_sched_clock(void)
33 * Fall back to jiffies if there's no TSC available:
34 * ( But note that we still use it if the TSC is marked
35 * unstable. We do this because unlike Time Of Day,
36 * the scheduler clock tolerates small errors and it's
37 * very important for it to be as fast as the platform
40 if (unlikely(tsc_disabled)) {
41 /* No locking but a rare wrong value is not a big deal: */
42 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
45 /* read the Time Stamp Counter: */
48 /* return the value in ns */
49 return cycles_2_ns(this_offset);
52 /* We need to define a real function for sched_clock, to override the
53 weak default version */
54 #ifdef CONFIG_PARAVIRT
55 unsigned long long sched_clock(void)
57 return paravirt_sched_clock();
61 sched_clock(void) __attribute__((alias("native_sched_clock")));
64 int check_tsc_unstable(void)
68 EXPORT_SYMBOL_GPL(check_tsc_unstable);
71 int __init notsc_setup(char *str)
73 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
74 "cannot disable TSC completely.\n");
80 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
83 int __init notsc_setup(char *str)
85 setup_clear_cpu_cap(X86_FEATURE_TSC);
90 __setup("notsc", notsc_setup);
93 #define SMI_TRESHOLD 50000
96 * Read TSC and the reference counters. Take care of SMI disturbance
98 static u64 __init tsc_read_refs(u64 *pm, u64 *hpet)
103 for (i = 0; i < MAX_RETRIES; i++) {
106 *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
108 *pm = acpi_pm_read_early();
110 if ((t2 - t1) < SMI_TRESHOLD)
117 * tsc_calibrate - calibrate the tsc on boot
119 static unsigned int __init tsc_calibrate(void)
122 u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
123 int hpet = is_hpet_enabled();
124 unsigned int tsc_khz_val = 0;
126 local_irq_save(flags);
128 tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
130 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
133 outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
134 outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
136 while ((inb(0x61) & 0x20) == 0);
139 tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
141 local_irq_restore(flags);
144 * Preset the result with the raw and inaccurate PIT
151 /* hpet or pmtimer available ? */
152 if (!hpet && !pm1 && !pm2) {
153 printk(KERN_INFO "TSC calibrated against PIT\n");
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");
164 tsc2 = (tsc2 - tsc1) * 1000000LL;
167 printk(KERN_INFO "TSC calibrated against HPET\n");
169 hpet2 += 0x100000000ULL;
171 tsc1 = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
172 do_div(tsc1, 1000000);
174 printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
176 pm2 += (u64)ACPI_PM_OVRRUN;
178 tsc1 = pm2 * 1000000000LL;
179 do_div(tsc1, PMTMR_TICKS_PER_SEC);
189 unsigned long native_calculate_cpu_khz(void)
191 return tsc_calibrate();
195 /* Only called from the Powernow K7 cpu freq driver */
196 int recalibrate_cpu_khz(void)
199 unsigned long cpu_khz_old = cpu_khz;
202 cpu_khz = calculate_cpu_khz();
204 cpu_data(0).loops_per_jiffy =
205 cpufreq_scale(cpu_data(0).loops_per_jiffy,
206 cpu_khz_old, cpu_khz);
215 EXPORT_SYMBOL(recalibrate_cpu_khz);
217 #endif /* CONFIG_X86_32 */