]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/tsc.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / osfmk / i386 / tsc.c
CommitLineData
89b3af67
A
1/*
2 * Copyright (c) 2005-2006 Apple Computer, 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#include <platforms.h>
39#include <mach_kdb.h>
40
41#include <mach/mach_types.h>
42
43#include <kern/cpu_data.h>
44#include <kern/cpu_number.h>
45#include <kern/clock.h>
46#include <kern/host_notify.h>
47#include <kern/macro_help.h>
48#include <kern/misc_protos.h>
49#include <kern/spl.h>
50#include <kern/assert.h>
51#include <mach/vm_prot.h>
52#include <vm/pmap.h>
53#include <vm/vm_kern.h> /* for kernel_map */
54#include <i386/ipl.h>
55#include <i386/pit.h>
56#include <architecture/i386/pio.h>
57#include <i386/misc_protos.h>
58#include <i386/proc_reg.h>
59#include <i386/machine_cpu.h>
60#include <i386/mp.h>
61#include <i386/cpuid.h>
62#include <i386/cpu_data.h>
63#include <i386/cpu_threads.h>
64#include <i386/perfmon.h>
65#include <i386/machine_routines.h>
66#include <pexpert/pexpert.h>
67#include <machine/limits.h>
68#include <machine/commpage.h>
69#include <sys/kdebug.h>
70#include <pexpert/device_tree.h>
71#include <i386/tsc.h>
72
73uint64_t busFCvtt2n = 0;
74uint64_t busFCvtn2t = 0;
75uint64_t tscFreq = 0;
76uint64_t tscFCvtt2n = 0;
77uint64_t tscFCvtn2t = 0;
78uint64_t tscGranularity = 0;
79uint64_t bus2tsc = 0;
80
81/* Decimal powers: */
82#define kilo (1000ULL)
83#define Mega (kilo * kilo)
84#define Giga (kilo * Mega)
85#define Tera (kilo * Giga)
86#define Peta (kilo * Tera)
87
88static const char FSB_Frequency_prop[] = "FSBFrequency";
89/*
90 * This routine extracts the front-side bus frequency in Hz from
91 * the device tree.
92 */
93static uint64_t
94EFI_FSB_frequency(void)
95{
96 uint64_t frequency = 0;
97 DTEntry entry;
98 void *value;
99 int size;
100
101 if (DTLookupEntry(0, "/efi/platform", &entry) != kSuccess) {
102 kprintf("EFI_FSB_frequency: didn't find /efi/platform\n");
103 return 0;
104 }
105 if (DTGetProperty(entry,FSB_Frequency_prop,&value,&size) != kSuccess) {
106 kprintf("EFI_FSB_frequency: property %s not found\n");
107 return 0;
108 }
109 if (size == sizeof(uint64_t)) {
110 frequency = *(uint64_t *) value;
111 kprintf("EFI_FSB_frequency: read %s value: %llu\n",
112 FSB_Frequency_prop, frequency);
113 if (!(90*Mega < frequency && frequency < 10*Giga)) {
114 kprintf("EFI_FSB_frequency: value out of range\n");
115 frequency = 0;
116 }
117 } else {
118 kprintf("EFI_FSB_frequency: unexpected size %d\n", size);
119 }
120 return frequency;
121}
122
123/*
124 * Initialize the various conversion factors needed by code referencing
125 * the TSC.
126 */
127void
128tsc_init(void)
129{
130 uint64_t busFreq;
131 uint64_t busFCvtInt;
132 uint32_t cpuModel;
133 uint32_t cpuFamily;
134 uint32_t xcpuid[4];
135
136 /*
137 * Get the FSB frequency and conversion factors.
138 */
139 busFreq = EFI_FSB_frequency();
140 if (busFreq != 0) {
141 busFCvtt2n = ((1 * Giga) << 32) / busFreq;
142 busFCvtn2t = 0xFFFFFFFFFFFFFFFFULL / busFCvtt2n;
143 busFCvtInt = tmrCvt(1 * Peta, 0xFFFFFFFFFFFFFFFFULL / busFreq);
144 } else {
145 panic("rtclock_init: EFI not supported!\n");
146 }
147
148 kprintf(" BUS: Frequency = %6d.%04dMHz, "
149 "cvtt2n = %08X.%08X, cvtn2t = %08X.%08X, "
150 "cvtInt = %08X.%08X\n",
151 (uint32_t)(busFreq / Mega),
152 (uint32_t)(busFreq % Mega),
153 (uint32_t)(busFCvtt2n >> 32), (uint32_t)busFCvtt2n,
154 (uint32_t)(busFCvtn2t >> 32), (uint32_t)busFCvtn2t,
155 (uint32_t)(busFCvtInt >> 32), (uint32_t)busFCvtInt);
156
157 do_cpuid(1, xcpuid);
158 cpuFamily = ( xcpuid[eax] >> 8 ) & 0xf;
159 /*
160 * Get the extended family if necessary.
161 */
162 if (cpuFamily == 0x0f)
163 cpuFamily += (xcpuid[eax] >> 20) & 0x00ff;
164
165 cpuModel = ( xcpuid[eax] >> 4 ) & 0xf;
166 /*
167 * Get the extended model if necessary.
168 */
169 if (cpuFamily == CPUID_FAMILY_686
170 || cpuFamily == CPUID_FAMILY_EXTENDED)
171 cpuModel += ((xcpuid[eax] >> 16) & 0xf) << 4;
172
173 /*
174 * Get the TSC increment. The TSC is incremented by this
175 * on every bus tick. Calculate the TSC conversion factors
176 * to and from nano-seconds.
177 */
178 if (cpuFamily == CPUID_FAMILY_686) {
179 if (cpuModel == CPUID_MODEL_CORE || cpuModel == CPUID_MODEL_CORE2) {
180 uint64_t prfsts;
181
182 prfsts = rdmsr64(IA32_PERF_STS);
183 tscGranularity = (uint32_t)(prfsts >> BusRatioShift) & BusRatioMask;
184 } else {
185 panic("rtclock_init: unknown CPU model: 0x%X\n",
186 cpuModel);
187 }
188 } else {
189 panic("rtclock_init: unknown CPU family: 0x%X\n",
190 cpuFamily);
191 }
192
193 tscFCvtt2n = busFCvtt2n / (uint64_t)tscGranularity;
194 tscFreq = ((1 * Giga) << 32) / tscFCvtt2n;
195 tscFCvtn2t = 0xFFFFFFFFFFFFFFFFULL / tscFCvtt2n;
196
197 kprintf(" TSC: Frequency = %6d.%04dMHz, "
198 "cvtt2n = %08X.%08X, cvtn2t = %08X.%08X, gran = %d\n",
199 (uint32_t)(tscFreq / Mega),
200 (uint32_t)(tscFreq % Mega),
201 (uint32_t)(tscFCvtt2n >> 32), (uint32_t)tscFCvtt2n,
202 (uint32_t)(tscFCvtn2t >> 32), (uint32_t)tscFCvtn2t,
203 tscGranularity);
204
205 /*
206 * Calculate conversion from BUS to TSC
207 */
208 bus2tsc = tmrCvt(busFCvtt2n, tscFCvtn2t);
209}
210
211void
212tsc_get_info(tscInfo_t *info)
213{
214 info->busFCvtt2n = busFCvtt2n;
215 info->busFCvtn2t = busFCvtn2t;
216 info->tscFreq = tscFreq;
217 info->tscFCvtt2n = tscFCvtt2n;
218 info->tscFCvtn2t = tscFCvtn2t;
219 info->tscGranularity = tscGranularity;
220 info->bus2tsc = bus2tsc;
221}