Commit | Line | Data |
---|---|---|
bfc0f594 | 1 | #include <linux/kernel.h> |
0ef95533 AK |
2 | #include <linux/sched.h> |
3 | #include <linux/init.h> | |
4 | #include <linux/module.h> | |
5 | #include <linux/timer.h> | |
bfc0f594 AK |
6 | #include <linux/acpi_pmtmr.h> |
7 | ||
8 | #include <asm/hpet.h> | |
0ef95533 AK |
9 | |
10 | unsigned int cpu_khz; /* TSC clocks / usec, not used here */ | |
11 | EXPORT_SYMBOL(cpu_khz); | |
12 | unsigned int tsc_khz; | |
13 | EXPORT_SYMBOL(tsc_khz); | |
14 | ||
15 | /* | |
16 | * TSC can be unstable due to cpufreq or due to unsynced TSCs | |
17 | */ | |
18 | int tsc_unstable; | |
19 | ||
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; | |
24 | ||
25 | /* | |
26 | * Scheduler clock - returns current time in nanosec units. | |
27 | */ | |
28 | u64 native_sched_clock(void) | |
29 | { | |
30 | u64 this_offset; | |
31 | ||
32 | /* | |
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 | |
38 | * can achive it. ) | |
39 | */ | |
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); | |
43 | } | |
44 | ||
45 | /* read the Time Stamp Counter: */ | |
46 | rdtscll(this_offset); | |
47 | ||
48 | /* return the value in ns */ | |
49 | return cycles_2_ns(this_offset); | |
50 | } | |
51 | ||
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) | |
56 | { | |
57 | return paravirt_sched_clock(); | |
58 | } | |
59 | #else | |
60 | unsigned long long | |
61 | sched_clock(void) __attribute__((alias("native_sched_clock"))); | |
62 | #endif | |
63 | ||
64 | int check_tsc_unstable(void) | |
65 | { | |
66 | return tsc_unstable; | |
67 | } | |
68 | EXPORT_SYMBOL_GPL(check_tsc_unstable); | |
69 | ||
70 | #ifdef CONFIG_X86_TSC | |
71 | int __init notsc_setup(char *str) | |
72 | { | |
73 | printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, " | |
74 | "cannot disable TSC completely.\n"); | |
75 | tsc_disabled = 1; | |
76 | return 1; | |
77 | } | |
78 | #else | |
79 | /* | |
80 | * disable flag for tsc. Takes effect by clearing the TSC cpu flag | |
81 | * in cpu/common.c | |
82 | */ | |
83 | int __init notsc_setup(char *str) | |
84 | { | |
85 | setup_clear_cpu_cap(X86_FEATURE_TSC); | |
86 | return 1; | |
87 | } | |
88 | #endif | |
89 | ||
90 | __setup("notsc", notsc_setup); | |
bfc0f594 AK |
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 */ |