Chameleon

Chameleon Svn Source Tree

Root/branches/rewrite/i386/libsaio/cpu.c

Source at commit 1066 created 12 years 10 months ago.
By meklort, Removing even more code...
1/*
2 * Copyright 2008 Islam Ahmed Zaid. All rights reserved. <azismed@gmail.com>
3 * AsereBLN: 2009: cleanup and bugfix
4 */
5
6#include "libsaio.h"
7#include "platform.h"
8#include "cpu.h"
9#include "boot.h"
10
11#ifndef DEBUG_CPU
12#define DEBUG_CPU 0
13#endif
14
15#if DEBUG_CPU
16#define DBG(x...)printf(x)
17#else
18#define DBG(x...)msglog(x)
19#endif
20
21/*
22 * DFE: Measures the TSC frequency in Hz (64-bit) using the ACPI PM timer
23 */
24static uint64_t measure_tsc_frequency(void)
25{
26 uint64_t tscStart;
27 uint64_t tscEnd;
28 uint64_t tscDelta = 0xffffffffffffffffULL;
29 unsigned long pollCount;
30 uint64_t retval = 0;
31 int i;
32
33 /* Time how many TSC ticks elapse in 30 msec using the 8254 PIT
34 * counter 2. We run this loop 3 times to make sure the cache
35 * is hot and we take the minimum delta from all of the runs.
36 * That is to say that we're biased towards measuring the minimum
37 * number of TSC ticks that occur while waiting for the timer to
38 * expire. That theoretically helps avoid inconsistencies when
39 * running under a VM if the TSC is not virtualized and the host
40 * steals time. The TSC is normally virtualized for VMware.
41 */
42 for(i = 0; i < 10; ++i)
43 {
44 enable_PIT2();
45 set_PIT2_mode0(CALIBRATE_LATCH);
46 tscStart = rdtsc64();
47 pollCount = poll_PIT2_gate();
48 tscEnd = rdtsc64();
49 /* The poll loop must have run at least a few times for accuracy */
50 if(pollCount <= 1)
51 continue;
52 /* The TSC must increment at LEAST once every millisecond. We
53 * should have waited exactly 30 msec so the TSC delta should
54 * be >= 30. Anything less and the processor is way too slow.
55 */
56 if((tscEnd - tscStart) <= CALIBRATE_TIME_MSEC)
57 continue;
58 // tscDelta = MIN(tscDelta, (tscEnd - tscStart))
59 if( (tscEnd - tscStart) < tscDelta )
60 tscDelta = tscEnd - tscStart;
61 }
62 /* tscDelta is now the least number of TSC ticks the processor made in
63 * a timespan of 0.03 s (e.g. 30 milliseconds)
64 * Linux thus divides by 30 which gives the answer in kiloHertz because
65 * 1 / ms = kHz. But we're xnu and most of the rest of the code uses
66 * Hz so we need to convert our milliseconds to seconds. Since we're
67 * dividing by the milliseconds, we simply multiply by 1000.
68 */
69
70 /* Unlike linux, we're not limited to 32-bit, but we do need to take care
71 * that we're going to multiply by 1000 first so we do need at least some
72 * arithmetic headroom. For now, 32-bit should be enough.
73 * Also unlike Linux, our compiler can do 64-bit integer arithmetic.
74 */
75 if(tscDelta > (1ULL<<32))
76 retval = 0;
77 else
78 {
79 retval = tscDelta * 1000 / 30;
80 }
81 disable_PIT2();
82 return retval;
83}
84
85/*
86 * Calculates the FSB and CPU frequencies using specific MSRs for each CPU
87 * - multi. is read from a specific MSR. In the case of Intel, there is:
88 * a max multi. (used to calculate the FSB freq.),
89 * and a current multi. (used to calculate the CPU freq.)
90 * - fsbFrequency = tscFrequency / multi
91 * - cpuFrequency = fsbFrequency * multi
92 */
93
94void scan_cpu(PlatformInfo_t *p)
95{
96uint64_ttscFrequency, fsbFrequency, cpuFrequency;
97uint64_tmsr, flex_ratio;
98uint8_tmaxcoef, maxdiv, currcoef, bus_ratio_max, currdiv;
99int myfsb;
100uint8_t bus_ratio_min;
101uint32_t max_ratio, min_ratio;
102
103max_ratio = min_ratio = myfsb = bus_ratio_min = 0;
104maxcoef = maxdiv = bus_ratio_max = currcoef = currdiv = 0;
105
106/* get cpuid values */
107do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]);
108do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]);
109do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]);
110do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]);
111do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
112do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
113if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
114do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
115}
116#if DEBUG_CPU
117{
118inti;
119printf("CPUID Raw Values:\n");
120for (i=0; i<CPUID_MAX; i++) {
121printf("%02d: %08x-%08x-%08x-%08x\n", i,
122p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
123p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
124}
125}
126#endif
127p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
128p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
129p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
130p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
131p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
132p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
133p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
134
135 p->CPU.Model += (p->CPU.ExtModel << 4);
136
137 if (p->CPU.Vendor == 0x756E6547 /* Intel */ &&
138 p->CPU.Family == 0x06 &&
139 p->CPU.Model >= CPUID_MODEL_NEHALEM &&
140 p->CPU.Model != CPUID_MODEL_ATOM // MSR is *NOT* available on the Intel Atom CPU
141 ){
142 msr = rdmsr64(MSR_CORE_THREAD_COUNT);// Undocumented MSR in Nehalem and newer CPUs
143 p->CPU.NoCores= bitfield((uint32_t)msr, 31, 16);// Using undocumented MSR to get actual values
144 p->CPU.NoThreads= bitfield((uint32_t)msr, 15, 0);// Using undocumented MSR to get actual values
145} else {
146 p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);// Use previous method for Cores and Threads
147 p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;
148}
149
150/* get brand string (if supported) */
151/* Copyright: from Apple's XNU cpuid.c */
152if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {
153uint32_treg[4];
154 char str[128], *s;
155/*
156 * The brand string 48 bytes (max), guaranteed to
157 * be NULL terminated.
158 */
159do_cpuid(0x80000002, reg);
160bcopy((char *)reg, &str[0], 16);
161do_cpuid(0x80000003, reg);
162bcopy((char *)reg, &str[16], 16);
163do_cpuid(0x80000004, reg);
164bcopy((char *)reg, &str[32], 16);
165for (s = str; *s != '\0'; s++) {
166if (*s != ' ') break;
167}
168
169strlcpy(p->CPU.BrandString,s, sizeof(p->CPU.BrandString));
170
171if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, MIN(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {
172 /*
173 * This string means we have a firmware-programmable brand string,
174 * and the firmware couldn't figure out what sort of CPU we have.
175 */
176 p->CPU.BrandString[0] = '\0';
177 }
178}
179
180/* setup features */
181if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0) {
182p->CPU.Features |= CPU_FEATURE_MMX;
183}
184if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0) {
185p->CPU.Features |= CPU_FEATURE_SSE;
186}
187if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0) {
188p->CPU.Features |= CPU_FEATURE_SSE2;
189}
190if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0) {
191p->CPU.Features |= CPU_FEATURE_SSE3;
192}
193if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0) {
194p->CPU.Features |= CPU_FEATURE_SSE41;
195}
196if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0) {
197p->CPU.Features |= CPU_FEATURE_SSE42;
198}
199if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0) {
200p->CPU.Features |= CPU_FEATURE_EM64T;
201}
202if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0) {
203p->CPU.Features |= CPU_FEATURE_MSR;
204}
205//if ((bit(28) & p->CPU.CPUID[CPUID_1][3]) != 0) {
206if (p->CPU.NoThreads > p->CPU.NoCores) {
207p->CPU.Features |= CPU_FEATURE_HTT;
208}
209
210tscFrequency = measure_tsc_frequency();
211fsbFrequency = 0;
212cpuFrequency = 0;
213
214if ((p->CPU.Vendor == 0x756E6547 /* Intel */) && ((p->CPU.Family == 0x06) || (p->CPU.Family == 0x0f))) {
215int intelCPU = p->CPU.Model;
216if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)) {
217/* Nehalem CPU model */
218if (p->CPU.Family == 0x06 && (p->CPU.Model == CPU_MODEL_NEHALEM ||
219 p->CPU.Model == CPU_MODEL_FIELDS ||
220 p->CPU.Model == CPU_MODEL_DALES ||
221 p->CPU.Model == CPU_MODEL_DALES_32NM ||
222 p->CPU.Model == CPU_MODEL_WESTMERE ||
223 p->CPU.Model == CPU_MODEL_NEHALEM_EX ||
224 p->CPU.Model == CPU_MODEL_WESTMERE_EX ||
225 p->CPU.Model == CPU_MODEL_SANDY ||
226 p->CPU.Model == CPU_MODEL_SANDY_XEON)) {
227msr = rdmsr64(MSR_PLATFORM_INFO);
228DBG("msr(%d): platform_info %08x\n", __LINE__, msr & 0xffffffff);
229bus_ratio_max = (msr >> 8) & 0xff;
230bus_ratio_min = (msr >> 40) & 0xff; //valv: not sure about this one (Remarq.1)
231msr = rdmsr64(MSR_FLEX_RATIO);
232DBG("msr(%d): flex_ratio %08x\n", __LINE__, msr & 0xffffffff);
233if ((msr >> 16) & 0x01) {
234flex_ratio = (msr >> 8) & 0xff;
235/* bcc9: at least on the gigabyte h67ma-ud2h,
236 where the cpu multipler can't be changed to
237 allow overclocking, the flex_ratio msr has unexpected (to OSX)
238 contents. These contents cause mach_kernel to
239 fail to compute the bus ratio correctly, instead
240 causing the system to crash since tscGranularity
241 is inadvertently set to 0.
242*/
243if (flex_ratio == 0) {
244/* Clear bit 16 (evidently the
245 presence bit) */
246wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
247msr = rdmsr64(MSR_FLEX_RATIO);
248verbose("Unusable flex ratio detected. Patched MSR now %08x\n", msr & 0xffffffff);
249} else {
250if (bus_ratio_max > flex_ratio) {
251bus_ratio_max = flex_ratio;
252}
253}
254}
255
256if (bus_ratio_max) {
257fsbFrequency = (tscFrequency / bus_ratio_max);
258}
259//valv: Turbo Ratio Limit
260if ((intelCPU != 0x2e) && (intelCPU != 0x2f)) {
261msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
262cpuFrequency = bus_ratio_max * fsbFrequency;
263max_ratio = bus_ratio_max * 10;
264} else {
265cpuFrequency = tscFrequency;
266}
267
268//valv: to be uncommented if Remarq.1 didn't stick
269/*if(bus_ratio_max > 0) bus_ratio = flex_ratio;*/
270p->CPU.MaxRatio = max_ratio;
271p->CPU.MinRatio = min_ratio;
272
273myfsb = fsbFrequency / 1000000;
274verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio);
275currcoef = bus_ratio_max;
276} else {
277msr = rdmsr64(MSR_IA32_PERF_STATUS);
278DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, msr & 0xffffffff);
279currcoef = (msr >> 8) & 0x1f;
280/* Non-integer bus ratio for the max-multi*/
281maxdiv = (msr >> 46) & 0x01;
282/* Non-integer bus ratio for the current-multi (undocumented)*/
283currdiv = (msr >> 14) & 0x01;
284
285if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f)) // This will always be model >= 3
286{
287/* On these models, maxcoef defines TSC freq */
288maxcoef = (msr >> 40) & 0x1f;
289} else {
290/* On lower models, currcoef defines TSC freq */
291/* XXX */
292maxcoef = currcoef;
293}
294
295if (maxcoef) {
296if (maxdiv) {
297fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
298} else {
299fsbFrequency = (tscFrequency / maxcoef);
300}
301if (currdiv) {
302cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
303} else {
304cpuFrequency = (fsbFrequency * currcoef);
305}
306DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
307}
308}
309}
310/* Mobile CPU */
311if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28)) {
312p->CPU.Features |= CPU_FEATURE_MOBILE;
313}
314}
315#if 0
316else if((p->CPU.Vendor == 0x68747541 /* AMD */) && (p->CPU.Family == 0x0f)) {
317if(p->CPU.ExtFamily == 0x00 /* K8 */) {
318msr = rdmsr64(K8_FIDVID_STATUS);
319currcoef = (msr & 0x3f) / 2 + 4;
320currdiv = (msr & 0x01) * 2;
321} else if(p->CPU.ExtFamily >= 0x01 /* K10+ */) {
322msr = rdmsr64(K10_COFVID_STATUS);
323if(p->CPU.ExtFamily == 0x01 /* K10 */)
324currcoef = (msr & 0x3f) + 0x10;
325else /* K11+ */
326currcoef = (msr & 0x3f) + 0x08;
327currdiv = (2 << ((msr >> 6) & 0x07));
328}
329
330if (currcoef) {
331if (currdiv) {
332fsbFrequency = ((tscFrequency * currdiv) / currcoef);
333DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
334} else {
335fsbFrequency = (tscFrequency / currcoef);
336DBG("%d\n", currcoef);
337}
338fsbFrequency = (tscFrequency / currcoef);
339cpuFrequency = tscFrequency;
340}
341}
342
343if (!fsbFrequency) {
344fsbFrequency = (DEFAULT_FSB * 1000);
345cpuFrequency = tscFrequency;
346DBG("0 ! using the default value for FSB !\n");
347}
348#endif
349
350p->CPU.MaxCoef = maxcoef;
351p->CPU.MaxDiv = maxdiv;
352p->CPU.CurrCoef = currcoef;
353p->CPU.CurrDiv = currdiv;
354p->CPU.TSCFrequency = tscFrequency;
355p->CPU.FSBFrequency = fsbFrequency;
356p->CPU.CPUFrequency = cpuFrequency;
357
358DBG("CPU: Brand String: %s\n",p->CPU.BrandString);
359DBG("CPU: Vendor/Family/ExtFamily: 0x%x/0x%x/0x%x\n",p->CPU.Vendor, p->CPU.Family, p->CPU.ExtFamily);
360DBG("CPU: Model/ExtModel/Stepping: 0x%x/0x%x/0x%x\n",p->CPU.Model, p->CPU.ExtModel, p->CPU.Stepping);
361DBG("CPU: MaxCoef/CurrCoef: 0x%x/0x%x\n",p->CPU.MaxCoef, p->CPU.CurrCoef);
362DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n",p->CPU.MaxDiv, p->CPU.CurrDiv);
363DBG("CPU: TSCFreq: %dMHz\n",p->CPU.TSCFrequency / 1000000);
364DBG("CPU: FSBFreq: %dMHz\n",p->CPU.FSBFrequency / 1000000);
365DBG("CPU: CPUFreq: %dMHz\n",p->CPU.CPUFrequency / 1000000);
366DBG("CPU: NoCores/NoThreads: %d/%d\n",p->CPU.NoCores, p->CPU.NoThreads);
367DBG("CPU: Features: 0x%08x\n",p->CPU.Features);
368#if DEBUG_CPU
369pause();
370#endif
371}
372

Archive Download this file

Revision: 1066