]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/tsc.c
xnu-2782.20.48.tar.gz
[apple/xnu.git] / osfmk / i386 / tsc.c
1 /*
2 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31
32 /*
33 * File: i386/tsc.c
34 * Purpose: Initializes the TSC and the various conversion
35 * factors needed by other parts of the system.
36 */
37
38
39 #include <mach/mach_types.h>
40
41 #include <kern/cpu_data.h>
42 #include <kern/cpu_number.h>
43 #include <kern/clock.h>
44 #include <kern/host_notify.h>
45 #include <kern/macro_help.h>
46 #include <kern/misc_protos.h>
47 #include <kern/spl.h>
48 #include <kern/assert.h>
49 #include <mach/vm_prot.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_kern.h> /* for kernel_map */
52 #include <architecture/i386/pio.h>
53 #include <i386/machine_cpu.h>
54 #include <i386/cpuid.h>
55 #include <i386/mp.h>
56 #include <i386/machine_routines.h>
57 #include <i386/proc_reg.h>
58 #include <i386/tsc.h>
59 #include <i386/misc_protos.h>
60 #include <pexpert/pexpert.h>
61 #include <machine/limits.h>
62 #include <machine/commpage.h>
63 #include <sys/kdebug.h>
64 #include <pexpert/device_tree.h>
65
66 uint64_t busFCvtt2n = 0;
67 uint64_t busFCvtn2t = 0;
68 uint64_t tscFreq = 0;
69 uint64_t tscFCvtt2n = 0;
70 uint64_t tscFCvtn2t = 0;
71 uint64_t tscGranularity = 0;
72 uint64_t bus2tsc = 0;
73 uint64_t busFreq = 0;
74 uint32_t flex_ratio = 0;
75 uint32_t flex_ratio_min = 0;
76 uint32_t flex_ratio_max = 0;
77
78 uint64_t tsc_at_boot = 0;
79
80 #define bit(n) (1ULL << (n))
81 #define bitmask(h,l) ((bit(h)|(bit(h)-1)) & ~(bit(l)-1))
82 #define bitfield(x,h,l) (((x) & bitmask(h,l)) >> l)
83
84 /* Decimal powers: */
85 #define kilo (1000ULL)
86 #define Mega (kilo * kilo)
87 #define Giga (kilo * Mega)
88 #define Tera (kilo * Giga)
89 #define Peta (kilo * Tera)
90
91 #define CPU_FAMILY_PENTIUM_M (0x6)
92
93 static const char FSB_Frequency_prop[] = "FSBFrequency";
94 static const char TSC_at_boot_prop[] = "InitialTSC";
95 /*
96 * This routine extracts the bus frequency in Hz from the device tree.
97 * Also reads any initial TSC value at boot from the device tree.
98 */
99 static uint64_t
100 EFI_FSB_frequency(void)
101 {
102 uint64_t frequency = 0;
103 DTEntry entry;
104 void *value;
105 unsigned int size;
106
107 if (DTLookupEntry(0, "/efi/platform", &entry) != kSuccess) {
108 kprintf("EFI_FSB_frequency: didn't find /efi/platform\n");
109 return 0;
110 }
111 if (DTGetProperty(entry,FSB_Frequency_prop,&value,&size) != kSuccess) {
112 kprintf("EFI_FSB_frequency: property %s not found\n",
113 FSB_Frequency_prop);
114 return 0;
115 }
116 if (size == sizeof(uint64_t)) {
117 frequency = *(uint64_t *) value;
118 kprintf("EFI_FSB_frequency: read %s value: %llu\n",
119 FSB_Frequency_prop, frequency);
120 if (!(90*Mega < frequency && frequency < 10*Giga)) {
121 kprintf("EFI_FSB_frequency: value out of range\n");
122 frequency = 0;
123 }
124 } else {
125 kprintf("EFI_FSB_frequency: unexpected size %d\n", size);
126 }
127
128 /*
129 * While we're here, see if EFI published an initial TSC value.
130 */
131 if (DTGetProperty(entry,TSC_at_boot_prop,&value,&size) == kSuccess) {
132 if (size == sizeof(uint64_t)) {
133 tsc_at_boot = *(uint64_t *) value;
134 kprintf("EFI_FSB_frequency: read %s value: %llu\n",
135 TSC_at_boot_prop, tsc_at_boot);
136 }
137 }
138
139 return frequency;
140 }
141
142 /*
143 * Initialize the various conversion factors needed by code referencing
144 * the TSC.
145 */
146 void
147 tsc_init(void)
148 {
149 boolean_t N_by_2_bus_ratio = FALSE;
150
151 if (cpuid_vmm_present()) {
152 kprintf("VMM vendor %u TSC frequency %u KHz bus frequency %u KHz\n",
153 cpuid_vmm_info()->cpuid_vmm_family,
154 cpuid_vmm_info()->cpuid_vmm_tsc_frequency,
155 cpuid_vmm_info()->cpuid_vmm_bus_frequency);
156
157 if (cpuid_vmm_info()->cpuid_vmm_tsc_frequency &&
158 cpuid_vmm_info()->cpuid_vmm_bus_frequency) {
159
160 busFreq = (uint64_t)cpuid_vmm_info()->cpuid_vmm_bus_frequency * kilo;
161 busFCvtt2n = ((1 * Giga) << 32) / busFreq;
162 busFCvtn2t = 0xFFFFFFFFFFFFFFFFULL / busFCvtt2n;
163
164 tscFreq = (uint64_t)cpuid_vmm_info()->cpuid_vmm_tsc_frequency * kilo;
165 tscFCvtt2n = ((1 * Giga) << 32) / tscFreq;
166 tscFCvtn2t = 0xFFFFFFFFFFFFFFFFULL / tscFCvtt2n;
167
168 tscGranularity = tscFreq / busFreq;
169
170 bus2tsc = tmrCvt(busFCvtt2n, tscFCvtn2t);
171
172 return;
173 }
174 }
175
176 /*
177 * Get the FSB frequency and conversion factors from EFI.
178 */
179 busFreq = EFI_FSB_frequency();
180
181 switch (cpuid_cpufamily()) {
182 default: {
183 uint64_t msr_flex_ratio;
184 uint64_t msr_platform_info;
185
186 /* See if FLEX_RATIO is being used */
187 msr_flex_ratio = rdmsr64(MSR_FLEX_RATIO);
188 msr_platform_info = rdmsr64(MSR_PLATFORM_INFO);
189 flex_ratio_min = (uint32_t)bitfield(msr_platform_info, 47, 40);
190 flex_ratio_max = (uint32_t)bitfield(msr_platform_info, 15, 8);
191 /* No BIOS-programed flex ratio. Use hardware max as default */
192 tscGranularity = flex_ratio_max;
193 if (msr_flex_ratio & bit(16)) {
194 /* Flex Enabled: Use this MSR if less than max */
195 flex_ratio = (uint32_t)bitfield(msr_flex_ratio, 15, 8);
196 if (flex_ratio < flex_ratio_max)
197 tscGranularity = flex_ratio;
198 }
199
200 /* If EFI isn't configured correctly, use a constant
201 * value. See 6036811.
202 */
203 if (busFreq == 0)
204 busFreq = BASE_NHM_CLOCK_SOURCE;
205
206 break;
207 }
208 case CPUFAMILY_INTEL_MEROM:
209 case CPUFAMILY_INTEL_PENRYN: {
210 uint64_t prfsts;
211
212 prfsts = rdmsr64(IA32_PERF_STS);
213 tscGranularity = (uint32_t)bitfield(prfsts, 44, 40);
214 N_by_2_bus_ratio = (prfsts & bit(46)) != 0;
215 }
216 }
217
218 if (busFreq != 0) {
219 busFCvtt2n = ((1 * Giga) << 32) / busFreq;
220 busFCvtn2t = 0xFFFFFFFFFFFFFFFFULL / busFCvtt2n;
221 } else {
222 panic("tsc_init: EFI not supported!\n");
223 }
224
225 kprintf(" BUS: Frequency = %6d.%06dMHz, "
226 "cvtt2n = %08X.%08X, cvtn2t = %08X.%08X\n",
227 (uint32_t)(busFreq / Mega),
228 (uint32_t)(busFreq % Mega),
229 (uint32_t)(busFCvtt2n >> 32), (uint32_t)busFCvtt2n,
230 (uint32_t)(busFCvtn2t >> 32), (uint32_t)busFCvtn2t);
231
232 /*
233 * Get the TSC increment. The TSC is incremented by this
234 * on every bus tick. Calculate the TSC conversion factors
235 * to and from nano-seconds.
236 * The tsc granularity is also called the "bus ratio". If the N/2 bit
237 * is set this indicates the bus ration is 0.5 more than this - i.e.
238 * that the true bus ratio is (2*tscGranularity + 1)/2. If we cannot
239 * determine the TSC conversion, assume it ticks at the bus frequency.
240 */
241 if (tscGranularity == 0)
242 tscGranularity = 1;
243
244 if (N_by_2_bus_ratio)
245 tscFCvtt2n = busFCvtt2n * 2 / (1 + 2*tscGranularity);
246 else
247 tscFCvtt2n = busFCvtt2n / tscGranularity;
248
249 tscFreq = ((1 * Giga) << 32) / tscFCvtt2n;
250 tscFCvtn2t = 0xFFFFFFFFFFFFFFFFULL / tscFCvtt2n;
251
252 kprintf(" TSC: Frequency = %6d.%06dMHz, "
253 "cvtt2n = %08X.%08X, cvtn2t = %08X.%08X, gran = %lld%s\n",
254 (uint32_t)(tscFreq / Mega),
255 (uint32_t)(tscFreq % Mega),
256 (uint32_t)(tscFCvtt2n >> 32), (uint32_t)tscFCvtt2n,
257 (uint32_t)(tscFCvtn2t >> 32), (uint32_t)tscFCvtn2t,
258 tscGranularity, N_by_2_bus_ratio ? " (N/2)" : "");
259
260 /*
261 * Calculate conversion from BUS to TSC
262 */
263 bus2tsc = tmrCvt(busFCvtt2n, tscFCvtn2t);
264 }
265
266 void
267 tsc_get_info(tscInfo_t *info)
268 {
269 info->busFCvtt2n = busFCvtt2n;
270 info->busFCvtn2t = busFCvtn2t;
271 info->tscFreq = tscFreq;
272 info->tscFCvtt2n = tscFCvtt2n;
273 info->tscFCvtn2t = tscFCvtn2t;
274 info->tscGranularity = tscGranularity;
275 info->bus2tsc = bus2tsc;
276 info->busFreq = busFreq;
277 info->flex_ratio = flex_ratio;
278 info->flex_ratio_min = flex_ratio_min;
279 info->flex_ratio_max = flex_ratio_max;
280 }