Chameleon

Chameleon Svn Source Tree

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

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 "mem.h"
9#include "smbios_patcher.h"
10#include "cpu.h"
11
12#ifndef DEBUG_CPU
13#define DEBUG_CPU 0
14#endif
15
16#if DEBUG_CPU
17#define DBG(x...)printf(x)
18#else
19#define DBG(x...)msglog(x)
20#endif
21
22
23/*
24 * DFE: Measures the TSC frequency in Hz (64-bit) using the ACPI PM timer
25 */
26static uint64_t measure_tsc_frequency(void)
27{
28 uint64_t tscStart;
29 uint64_t tscEnd;
30 uint64_t tscDelta = 0xffffffffffffffffULL;
31 unsigned long pollCount;
32 uint64_t retval = 0;
33 int i;
34
35 /* Time how many TSC ticks elapse in 30 msec using the 8254 PIT
36 * counter 2. We run this loop 3 times to make sure the cache
37 * is hot and we take the minimum delta from all of the runs.
38 * That is to say that we're biased towards measuring the minimum
39 * number of TSC ticks that occur while waiting for the timer to
40 * expire. That theoretically helps avoid inconsistencies when
41 * running under a VM if the TSC is not virtualized and the host
42 * steals time. The TSC is normally virtualized for VMware.
43 */
44 for(i = 0; i < 10; ++i)
45 {
46 enable_PIT2();
47 set_PIT2_mode0(CALIBRATE_LATCH);
48 tscStart = rdtsc64();
49 pollCount = poll_PIT2_gate();
50 tscEnd = rdtsc64();
51 /* The poll loop must have run at least a few times for accuracy */
52 if(pollCount <= 1)
53 continue;
54 /* The TSC must increment at LEAST once every millisecond. We
55 * should have waited exactly 30 msec so the TSC delta should
56 * be >= 30. Anything less and the processor is way too slow.
57 */
58 if((tscEnd - tscStart) <= CALIBRATE_TIME_MSEC)
59 continue;
60 // tscDelta = min(tscDelta, (tscEnd - tscStart))
61 if( (tscEnd - tscStart) < tscDelta )
62 tscDelta = tscEnd - tscStart;
63 }
64 /* tscDelta is now the least number of TSC ticks the processor made in
65 * a timespan of 0.03 s (e.g. 30 milliseconds)
66 * Linux thus divides by 30 which gives the answer in kiloHertz because
67 * 1 / ms = kHz. But we're xnu and most of the rest of the code uses
68 * Hz so we need to convert our milliseconds to seconds. Since we're
69 * dividing by the milliseconds, we simply multiply by 1000.
70 */
71
72 /* Unlike linux, we're not limited to 32-bit, but we do need to take care
73 * that we're going to multiply by 1000 first so we do need at least some
74 * arithmetic headroom. For now, 32-bit should be enough.
75 * Also unlike Linux, our compiler can do 64-bit integer arithmetic.
76 */
77 if(tscDelta > (1ULL<<32))
78 retval = 0;
79 else
80 {
81 retval = tscDelta * 1000 / 30;
82 }
83 disable_PIT2();
84 return retval;
85}
86
87/*
88 * Calculates the FSB and CPU frequencies using specific MSRs for each CPU
89 * - multi. is read from a specific MSR. In the case of Intel, there is:
90 * a max multi. (used to calculate the FSB freq.),
91 * and a current multi. (used to calculate the CPU freq.)
92 * - fsbFrequency = tscFrequency / multi
93 * - cpuFrequency = fsbFrequency * multi
94 */
95
96void scan_cpu() //PlatformInfo_t *p)
97{
98PlatformInfo_t *p = Platform;
99int i = 0;
100uint8_t turbo;
101uint64_ttscFrequency, fsbFrequency, cpuFrequency;
102uint64_tmsr; //, flex_ratio;
103uint8_tmaxcoef, maxdiv, currcoef, currdiv, mindiv;
104
105maxcoef = maxdiv = currcoef = currdiv = mindiv = 0;
106
107#if DEBUG_CPU
108printf("Enter cpuid_info\n");
109pause();
110#endif
111cpuid_update_generic_info();
112
113#if DEBUG_CPU
114printf("...OK\n");
115pause();
116#endif
117
118#if OLDMETHOD
119/* get cpuid values */
120for( ; i <= 3; i++)
121{
122do_cpuid(i, p->CPU.CPUID[i]);
123}
124
125do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
126do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
127if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
128do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
129}
130#if DEBUG_CPU
131{
132inti;
133DBG("CPUID Raw Values:\n");
134for (i=0; i<CPUID_MAX; i++) {
135DBG("%02d: %08x-%08x-%08x-%08x\n", i,
136p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
137p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
138}
139}
140#endif
141p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
142p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
143p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
144p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
145p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
146p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
147p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
148p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
149p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;
150
151p->CPU.Model += (p->CPU.ExtModel << 4);
152
153/* get brand string (if supported) */
154/* Copyright: from Apple's XNU cpuid.c */
155if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {
156uint32_treg[4];
157 char str[128], *s;
158/*
159 * The brand string 48 bytes (max), guaranteed to
160 * be NUL terminated.
161 */
162do_cpuid(0x80000002, reg);
163bcopy((char *)reg, &str[0], 16);
164do_cpuid(0x80000003, reg);
165bcopy((char *)reg, &str[16], 16);
166do_cpuid(0x80000004, reg);
167bcopy((char *)reg, &str[32], 16);
168for (s = str; *s != '\0'; s++) {
169if (*s != ' ') break;
170}
171
172strlcpy(p->CPU.BrandString,s, sizeof(p->CPU.BrandString));
173
174if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, min(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {
175 /*
176 * This string means we have a firmware-programmable brand string,
177 * and the firmware couldn't figure out what sort of CPU we have.
178 */
179 p->CPU.BrandString[0] = '\0';
180 }
181}
182
183/* setup features */
184p->CPU.Features |= (CPU_FEATURE_MMX | CPU_FEATURE_SSE | CPU_FEATURE_SSE2 | CPU_FEATURE_MSR) & p->CPU.CPUID[CPUID_1][3];
185p->CPU.Features |= (CPU_FEATURE_SSE3 | CPU_FEATURE_SSE41 | CPU_FEATURE_SSE42) & p->CPU.CPUID[CPUID_1][2];
186p->CPU.Features |= (CPU_FEATURE_EM64T) & p->CPU.CPUID[CPUID_81][3];
187
188
189//if ((CPU_FEATURE_HTT & p->CPU.CPUID[CPUID_1][3]) != 0) {
190if (p->CPU.NoThreads > p->CPU.NoCores) {
191p->CPU.Features |= CPU_FEATURE_HTT;
192}
193#else //Slice
194p->CPU.Vendor= *(UInt32*)&cpuid_info()->cpuid_vendor;
195p->CPU.Signature= cpuid_info()->cpuid_signature;
196p->CPU.Stepping= cpuid_info()->cpuid_stepping;
197p->CPU.Model= cpuid_info()->cpuid_model;
198p->CPU.Family= cpuid_info()->cpuid_family;
199p->CPU.ExtModel= cpuid_info()->cpuid_extmodel;
200p->CPU.ExtFamily= cpuid_info()->cpuid_extfamily;
201//DBG("CPU: Vendor/Model/ExtModel: 0x%x/0x%x/0x%x\n", p->CPU.Vendor, p->CPU.Model, p->CPU.ExtModel);
202//DBG("CPU: Family/ExtFamily: 0x%x/0x%x\n", p->CPU.Family, p->CPU.ExtFamily);
203
204strlcpy(p->CPU.BrandString, cpuid_info()->cpuid_brand_string, sizeof(p->CPU.BrandString));
205DBG("CPU: BrandString %s\n", p->CPU.BrandString);
206p->CPU.Features = cpuid_info()->cpuid_features;
207p->CPU.NoCores = cpuid_info()->core_count;
208p->CPU.NoThreads = cpuid_info()->thread_count;
209//DBG("CPU: MaxCoef/CurrCoef: 0x%x/0x%x\n", p->CPU.MaxCoef, p->CPU.CurrCoef);
210//DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv?2:1, p->CPU.CurrDiv?2:1);
211//DBG("CPU: TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
212//DBG("CPU: FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);
213//DBG("CPU: CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
214//DBG("CPU: NoCores/NoThreads: %d/%d\n", p->CPU.NoCores, p->CPU.NoThreads);
215//DBG("CPU: Features: 0x%08x\n", p->CPU.Features);
216#if DEBUG_CPU
217pause();
218#endif
219
220#endif
221
222tscFrequency = measure_tsc_frequency();
223DBG("measure_tsc_frequency = %dMHz\n", tscFrequency / MEGA);
224fsbFrequency = 0;
225cpuFrequency = 0;
226
227if ((p->CPU.Vendor == 0x756E6547 /* Intel */) &&
228((p->CPU.Family == 0x06) ||
229 (p->CPU.Family == 0x0f)))
230{
231if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) ||
232(p->CPU.Family == 0x0f && p->CPU.Model >= 0x03))
233{
234/* Nehalem CPU model */
235if (p->CPU.Family == 0x06 && (p->CPU.Model == 0x1a || p->CPU.Model == 0x1e ||
236 p->CPU.Model == 0x1f || p->CPU.Model == 0x25 ||
237 p->CPU.Model == 0x19 || p->CPU.Model == 0x2c))
238{
239msr = rdmsr64(MSR_PLATFORM_INFO);
240DBG("msr(0x%04x): platform_info %08x-%08x\n", MSR_PLATFORM_INFO,
241(msr >> 32) & 0xffffffff, msr & 0xffffffff);
242mindiv = (msr >> 40) & 0xff;
243maxcoef = (msr >> 8) & 0xff;
244
245msr = rdmsr64(MSR_TURBO_RATIO);
246turbo = msr & 0x7f;
247//Slice - doesn't work
248/*
249msr = rdmsr64(MSR_FLEX_RATIO);
250DBG("msr(0x%04x): flex_ratio %08x\n", MSR_FLEX_RATIO, msr & 0xffffffff);
251if ((msr >> 16) & 0x01) {
252flex_ratio = (msr >> 8) & 0xff;
253if (currcoef > flex_ratio) {
254currcoef = flex_ratio;
255}
256}*/
257msr = rdmsr64(MSR_IA32_PERF_STATUS);
258if (msr) {
259currcoef = msr & 0x1f;
260}
261
262if (!currcoef) {
263currcoef = maxcoef;
264}
265
266if (currcoef < mindiv) {
267currcoef = mindiv;
268}
269
270if (currcoef) {
271fsbFrequency = (tscFrequency / currcoef);
272}
273cpuFrequency = tscFrequency;
274}
275else //not nehalem
276{
277//Slice - it is not FSB frequency. It is System Bus Speed: FSB = SBS * 4;
278if (p->CPU.Family != 0x0d){
279msr = rdmsr64(MSR_FSB_FREQ);
280switch (msr & 7) {
281case 0:
282fsbFrequency = 266670 * 1000;
283break;
284case 1:
285fsbFrequency = 133330 * 1000;
286break;
287case 2:
288fsbFrequency = 200000 * 1000;
289break;
290case 3:
291fsbFrequency = 166670 * 1000;
292break;
293case 4:
294fsbFrequency = 333330 * 1000;
295break;
296case 5:
297fsbFrequency = 200000 * 1000;
298break;
299case 6:
300fsbFrequency = 400000 * 1000;
301break;
302default:
303fsbFrequency = 0;
304break;
305}
306DBG("msr(0x%04x): MSR_FSB_FREQ %d.%dMHz\n", MSR_FSB_FREQ,
307fsbFrequency/MEGA, (fsbFrequency%MEGA)/1000);
308}
309
310msr = rdmsr64(MSR_PLATFORM_INFO); //info only?
311uint32_t m2 = msr >> 32;
312DBG("msr(0x%04x): platform_info %08x-%08x\n", MSR_PLATFORM_INFO,
313m2 & 0xffffffff, msr & 0xffffffff);
314turbo = (m2 >> 8) & 0x1f;
315
316msr = rdmsr64(MSR_IA32_PERF_STATUS);
317m2 = msr >> 32;
318DBG("msr(0x%04x): MSR_IA32_PERF_STATUS %08x-%08x\n", MSR_IA32_PERF_STATUS,
319m2 & 0xffffffff, msr & 0xffffffff);
320
321currcoef = (msr >> 8) & 0x1f;
322mindiv = (msr >> 24) & 0xf;
323if (currcoef < mindiv) {
324currcoef = mindiv;
325}
326
327/* Non-integer bus ratio for the max-multi*/
328maxdiv = (msr >> 46) & 0x01;
329/* Non-integer bus ratio for the current-multi (undocumented)*/
330currdiv = (msr >> 14) & 0x01;
331
332if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) ||
333(p->CPU.Family == 0x0f)) // This will always be model >= 3
334{
335/* On these models, maxcoef defines TSC freq */
336maxcoef = (msr >> 40) & 0x1f;
337}
338else
339{
340/* On lower models, currcoef defines TSC freq */
341/* XXX */
342maxcoef = currcoef;
343}
344
345if (maxcoef)
346{
347if (!fsbFrequency) {
348if (maxdiv)
349{
350fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
351}
352else
353{
354fsbFrequency = (tscFrequency / maxcoef);
355}
356
357}
358
359if (currdiv)
360{
361cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
362}
363else
364{
365cpuFrequency = (fsbFrequency * currcoef);
366}
367DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
368}
369}
370}
371/* Mobile CPU ? */
372//Slice
373msr = rdmsr64(MSR_IA32_PLATFORM_ID);
374DBG("msr(0x%04x): MSR_IA32_PLATFORM_ID 0x%08x\n", MSR_IA32_PLATFORM_ID, msr & 0xffffffff); //__LINE__ - source line number :)
375if (!scanDMI() && msr) {
376p->CPU.Mobile = FALSE;
377switch (p->CPU.Model) {
378case 0x0D:
379p->CPU.Mobile = TRUE; // CPU_FEATURE_MOBILE;
380break;
381case 0x0F:
382p->CPU.Mobile = FALSE; // CPU_FEATURE_MOBILE;
383break;
384case 0x02:
385case 0x03:
386case 0x04:
387case 0x06:
388p->CPU.Mobile = (rdmsr64(MSR_P4_EBC_FREQUENCY_ID) && (1 << 21));
389break;
390default:
391p->CPU.Mobile = (rdmsr64(MSR_IA32_PLATFORM_ID) && (1<<28));
392break;
393}
394if (p->CPU.Mobile) {
395p->CPU.Features |= CPU_FEATURE_MOBILE;
396}
397}
398DBG("CPU is %s\n", p->CPU.Mobile?"Mobile":"Desktop");
399
400}
401#if 0
402else if((p->CPU.Vendor == 0x68747541 /* AMD */) && (p->CPU.Family == 0x0f))
403{
404if(p->CPU.ExtFamily == 0x00 /* K8 */)
405{
406msr = rdmsr64(K8_FIDVID_STATUS);
407currcoef = (msr & 0x3f) / 2 + 4;
408currdiv = (msr & 0x01) * 2;
409}
410else if(p->CPU.ExtFamily >= 0x01 /* K10+ */)
411{
412msr = rdmsr64(K10_COFVID_STATUS);
413if(p->CPU.ExtFamily == 0x01 /* K10 */)
414currcoef = (msr & 0x3f) + 0x10;
415else /* K11+ */
416currcoef = (msr & 0x3f) + 0x08;
417currdiv = (2 << ((msr >> 6) & 0x07));
418}
419
420if (currcoef)
421{
422if (currdiv)
423{
424fsbFrequency = ((tscFrequency * currdiv) / currcoef);
425DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
426}
427else
428{
429fsbFrequency = (tscFrequency / currcoef);
430DBG("%d\n", currcoef);
431}
432fsbFrequency = (tscFrequency / currcoef);
433cpuFrequency = tscFrequency;
434}
435}
436#endif
437else if(p->CPU.Vendor == 0x746e6543 && p->CPU.Family == 6)
438{
439switch (p->CPU.Model) {
440case CPU_VIA_NANO:
441// NOTE: TSC is constant, irrelevent of speed steping
442break;
443default:
444break;
445}
446
447msr = rdmsr64(MSR_NANO_FCR2);
448verbose("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);
449
450//msr = msr >> 32;
451msr |= VIA_ALTERNATIVE_VENDOR_BIT;
452//msr = msr << 32;
453
454verbose("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);
455wrmsr64(MSR_NANO_FCR2, msr);
456msr = rdmsr64(MSR_NANO_FCR2);
457verbose("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);
458
459
460/* get cpuid values */
461for( ; i <= 3; i++)
462{
463do_cpuid(i, p->CPU.CPUID[i]);
464}
465//int numcpuid_supported = p->CPU.CPUID[CPUID_0][0];// max number cpuid call
466//int numextcpuid = p->CPU.CPUID[CPUID_80][0];
467//p->CPU.Features = 0;
468//bitfield(p->CPU.CPUID[CPUID_1][1], 0, 0) FEATURE_C
469
470// CPUID_0 -> largest cpuid val in EAX
471// CPUID_0 -> rem = vendor string
472/*
473CPUID_1 EDX:
474 0 -> FPU
475 1 -> VME
476 2 -> DE
477 3 -> PSE
478 4 -> TSC
479 5 -> MSR
480 6 -> PAE
481 7 -> MCE
482 8 -> CX8
483 9 -> APIC
484 10 -> Reserved
485 11 -> Fast Call
486 12 -> MTTR
487 13 -> PGE
488 14 -> MCA
489 15 -> CMOV
490 16 -> PAT
491 17 -> PSE36
492 18 -> Serial Number
493 23 -> MMX
494 24 -> FXSR
495 25 -> SSE
496 */
497
498//CPUID_80 -> largest excpuid value in EAX
499//CPUID_81,EAX -> Signature
500//CPUID_80,EDX -> Ext Features
501//CPUID_82 -> CPU String
502//CPUID_83 -> CPU String
503//CPUID_84 -> CPU String
504p->CPU.NoThreads = p->CPU.NoCores;
505
506}
507
508if (!fsbFrequency) {
509fsbFrequency = (DEFAULT_FSB * 1000);
510cpuFrequency = tscFrequency;
511msglog("CPU: fsb=0 ! using the default value 100MHz !\n");
512}
513
514/*
515p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
516p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
517p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
518p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
519p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
520p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
521p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
522p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
523*/
524
525
526p->CPU.MaxCoef = turbo;
527p->CPU.MaxDiv = maxdiv;
528p->CPU.MinCoef = mindiv;
529p->CPU.CurrCoef = currcoef;
530p->CPU.CurrDiv = currdiv;
531p->CPU.TSCFrequency = tscFrequency;
532p->CPU.FSBFrequency = fsbFrequency;
533p->CPU.CPUFrequency = cpuFrequency;
534
535DBG("CPU: Vendor/Model/ExtModel: 0x%x/0x%x/0x%x\n", p->CPU.Vendor, p->CPU.Model, p->CPU.ExtModel);
536DBG("CPU: Family/ExtFamily: 0x%x/0x%x\n", p->CPU.Family, p->CPU.ExtFamily);
537DBG("CPU: MaxCoef/CurrCoef/Turbo: 0x%x/0x%x\n", p->CPU.MaxCoef, p->CPU.CurrCoef, turbo);
538DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv?2:1, p->CPU.CurrDiv?2:1);
539DBG("CPU: TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
540DBG("CPU: FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);
541DBG("CPU: CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
542DBG("CPU: NoCores/NoThreads: %d/%d\n", p->CPU.NoCores, p->CPU.NoThreads);
543DBG("CPU: Features: 0x%08x\n", p->CPU.Features);
544#if DEBUG_CPU
545pause();
546#endif
547}
548

Archive Download this file

Revision: 708