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;
100uint64_ttscFrequency, fsbFrequency, cpuFrequency;
101uint64_tmsr, flex_ratio;
102uint8_tmaxcoef, maxdiv, currcoef, currdiv, mindiv;
103
104maxcoef = maxdiv = currcoef = currdiv = mindiv = 0;
105
106/* get cpuid values */
107for( ; i <= 3; i++)
108{
109do_cpuid(i, p->CPU.CPUID[i]);
110}
111
112do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
113do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
114if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
115do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
116}
117#if DEBUG_CPU
118{
119inti;
120DBG("CPUID Raw Values:\n");
121for (i=0; i<CPUID_MAX; i++) {
122DBG("%02d: %08x-%08x-%08x-%08x\n", i,
123p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
124p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
125}
126}
127#endif
128p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
129p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
130p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
131p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
132p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
133p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
134p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
135p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
136p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;
137
138p->CPU.Model += (p->CPU.ExtModel << 4);
139
140/* get brand string (if supported) */
141/* Copyright: from Apple's XNU cpuid.c */
142if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {
143uint32_treg[4];
144 char str[128], *s;
145/*
146 * The brand string 48 bytes (max), guaranteed to
147 * be NUL terminated.
148 */
149do_cpuid(0x80000002, reg);
150bcopy((char *)reg, &str[0], 16);
151do_cpuid(0x80000003, reg);
152bcopy((char *)reg, &str[16], 16);
153do_cpuid(0x80000004, reg);
154bcopy((char *)reg, &str[32], 16);
155for (s = str; *s != '\0'; s++) {
156if (*s != ' ') break;
157}
158
159strlcpy(p->CPU.BrandString,s, sizeof(p->CPU.BrandString));
160
161if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, min(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {
162 /*
163 * This string means we have a firmware-programmable brand string,
164 * and the firmware couldn't figure out what sort of CPU we have.
165 */
166 p->CPU.BrandString[0] = '\0';
167 }
168}
169
170/* setup features */
171p->CPU.Features |= (CPU_FEATURE_MMX | CPU_FEATURE_SSE | CPU_FEATURE_SSE2 | CPU_FEATURE_MSR) & p->CPU.CPUID[CPUID_1][3];
172p->CPU.Features |= (CPU_FEATURE_SSE3 | CPU_FEATURE_SSE41 | CPU_FEATURE_SSE42) & p->CPU.CPUID[CPUID_1][2];
173p->CPU.Features |= (CPU_FEATURE_EM64T) & p->CPU.CPUID[CPUID_81][3];
174
175
176//if ((CPU_FEATURE_HTT & p->CPU.CPUID[CPUID_1][3]) != 0) {
177if (p->CPU.NoThreads > p->CPU.NoCores) {
178p->CPU.Features |= CPU_FEATURE_HTT;
179}
180
181
182tscFrequency = measure_tsc_frequency();
183DBG("measure_tsc_frequency = %dMHz\n", tscFrequency / MEGA);
184fsbFrequency = 0;
185cpuFrequency = 0;
186
187if ((p->CPU.Vendor == 0x756E6547 /* Intel */) &&
188((p->CPU.Family == 0x06) ||
189 (p->CPU.Family == 0x0f)))
190{
191if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) ||
192(p->CPU.Family == 0x0f && p->CPU.Model >= 0x03))
193{
194/* Nehalem CPU model */
195if (p->CPU.Family == 0x06 && (p->CPU.Model == 0x1a || p->CPU.Model == 0x1e ||
196 p->CPU.Model == 0x1f || p->CPU.Model == 0x25 ||
197 p->CPU.Model == 0x19 || p->CPU.Model == 0x2c))
198{
199msr = rdmsr64(MSR_PLATFORM_INFO);
200DBG("msr(0x%04x): platform_info %08x-%08x\n", MSR_PLATFORM_INFO,
201(msr >> 32) & 0xffffffff, msr & 0xffffffff);
202mindiv = (msr >> 40) & 0xff;
203flex_ratio = (msr >> 8) & 0xff;
204//Slice - doesn't work
205/*
206msr = rdmsr64(MSR_FLEX_RATIO);
207DBG("msr(0x%04x): flex_ratio %08x\n", MSR_FLEX_RATIO, msr & 0xffffffff);
208if ((msr >> 16) & 0x01) {
209flex_ratio = (msr >> 8) & 0xff;
210if (currcoef > flex_ratio) {
211currcoef = flex_ratio;
212}
213}*/
214msr = rdmsr64(MSR_IA32_PERF_STATUS);
215if (msr) {
216currcoef = msr & 0x1f;
217}
218if (currcoef < flex_ratio) {
219currcoef = flex_ratio;
220}
221if (currcoef < mindiv) {
222currcoef = mindiv;
223}
224
225if (currcoef) {
226fsbFrequency = (tscFrequency / currcoef);
227}
228cpuFrequency = tscFrequency;
229}
230else //not nehalem
231{
232//Slice - it is not FSB frequency. It is System Bus Speed: FSB = SBS * 4;
233//crash with i7?
234#if 1 //NOTI7
235if (p->CPU.Family != 0x0d){
236msr = rdmsr64(MSR_FSB_FREQ);
237switch (msr & 7) {
238case 0:
239fsbFrequency = 266670 * 1000;
240break;
241case 1:
242fsbFrequency = 133330 * 1000;
243break;
244case 2:
245fsbFrequency = 200000 * 1000;
246break;
247case 3:
248fsbFrequency = 166670 * 1000;
249break;
250case 4:
251fsbFrequency = 333330 * 1000;
252break;
253case 5:
254fsbFrequency = 200000 * 1000;
255break;
256case 6:
257fsbFrequency = 400000 * 1000;
258break;
259default:
260fsbFrequency = 0;
261break;
262}
263DBG("msr(0x%04x): MSR_FSB_FREQ %dMHz\n", MSR_FSB_FREQ, fsbFrequency/MEGA);
264}
265#endif
266
267msr = rdmsr64(MSR_PLATFORM_INFO);
268uint32_t m2 = msr & 0xffffffff;
269DBG("msr(0x%04x): platform_info %08x-%08x\n", MSR_PLATFORM_INFO,
270(msr >> 32) & 0xffffffff, m2);
271currcoef = (msr >> 40) & 0xff;
272msr = rdmsr64(MSR_IA32_PERF_STATUS);
273mindiv = (msr >> 24) & 0xf;
274if (currcoef < mindiv) {
275currcoef = mindiv;
276}
277
278/* Non-integer bus ratio for the max-multi*/
279maxdiv = (msr >> 46) & 0x01;
280/* Non-integer bus ratio for the current-multi (undocumented)*/
281currdiv = (msr >> 14) & 0x01;
282
283if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) ||
284(p->CPU.Family == 0x0f)) // This will always be model >= 3
285{
286/* On these models, maxcoef defines TSC freq */
287maxcoef = (msr >> 40) & 0x1f;
288}
289else
290{
291/* On lower models, currcoef defines TSC freq */
292/* XXX */
293maxcoef = currcoef;
294}
295
296if (maxcoef)
297{
298if (!fsbFrequency) {
299if (maxdiv)
300{
301fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
302}
303else
304{
305fsbFrequency = (tscFrequency / maxcoef);
306}
307
308}
309
310if (currdiv)
311{
312cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
313}
314else
315{
316cpuFrequency = (fsbFrequency * currcoef);
317}
318DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
319}
320}
321}
322/* Mobile CPU ? */
323//Slice
324msr = rdmsr64(MSR_IA32_PLATFORM_ID);
325DBG("msr(0x%04x): MSR_IA32_PLATFORM_ID 0x%08x\n", MSR_IA32_PLATFORM_ID, msr & 0xffffffff); //__LINE__ - source line number :)
326if (!scanDMI() && msr) {
327p->CPU.Mobile = FALSE;
328switch (p->CPU.Model) {
329case 0x0D:
330p->CPU.Mobile = TRUE; // CPU_FEATURE_MOBILE;
331break;
332case 0x0F:
333p->CPU.Mobile = FALSE; // CPU_FEATURE_MOBILE;
334break;
335case 0x02:
336case 0x03:
337case 0x04:
338case 0x06:
339p->CPU.Mobile = (rdmsr64(MSR_P4_EBC_FREQUENCY_ID) && (1 << 21));
340break;
341default:
342p->CPU.Mobile = (rdmsr64(MSR_IA32_PLATFORM_ID) && (1<<28));
343break;
344}
345if (p->CPU.Mobile) {
346p->CPU.Features |= CPU_FEATURE_MOBILE;
347}
348}
349DBG("CPU is %s\n", p->CPU.Mobile?"Mobile":"Desktop");
350
351}
352#if 0
353else if((p->CPU.Vendor == 0x68747541 /* AMD */) && (p->CPU.Family == 0x0f))
354{
355if(p->CPU.ExtFamily == 0x00 /* K8 */)
356{
357msr = rdmsr64(K8_FIDVID_STATUS);
358currcoef = (msr & 0x3f) / 2 + 4;
359currdiv = (msr & 0x01) * 2;
360}
361else if(p->CPU.ExtFamily >= 0x01 /* K10+ */)
362{
363msr = rdmsr64(K10_COFVID_STATUS);
364if(p->CPU.ExtFamily == 0x01 /* K10 */)
365currcoef = (msr & 0x3f) + 0x10;
366else /* K11+ */
367currcoef = (msr & 0x3f) + 0x08;
368currdiv = (2 << ((msr >> 6) & 0x07));
369}
370
371if (currcoef)
372{
373if (currdiv)
374{
375fsbFrequency = ((tscFrequency * currdiv) / currcoef);
376DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
377}
378else
379{
380fsbFrequency = (tscFrequency / currcoef);
381DBG("%d\n", currcoef);
382}
383fsbFrequency = (tscFrequency / currcoef);
384cpuFrequency = tscFrequency;
385}
386}
387#endif
388else if(p->CPU.Vendor == 0x746e6543 && p->CPU.Family == 6)
389{
390switch (p->CPU.Model) {
391case CPU_VIA_NANO:
392// NOTE: TSC is constant, irrelevent of speed steping
393break;
394default:
395break;
396}
397
398msr = rdmsr64(MSR_NANO_FCR2);
399printf("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);
400
401//msr = msr >> 32;
402msr |= VIA_ALTERNATIVE_VENDOR_BIT;
403//msr = msr << 32;
404
405printf("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);
406wrmsr64(MSR_NANO_FCR2, msr);
407msr = rdmsr64(MSR_NANO_FCR2);
408printf("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);
409
410
411/* get cpuid values */
412for( ; i <= 3; i++)
413{
414do_cpuid(i, p->CPU.CPUID[i]);
415}
416//int numcpuid_supported = p->CPU.CPUID[CPUID_0][0];// max number cpuid call
417//int numextcpuid = p->CPU.CPUID[CPUID_80][0];
418//p->CPU.Features = 0;
419//bitfield(p->CPU.CPUID[CPUID_1][1], 0, 0) FEATURE_C
420
421// CPUID_0 -> largest cpuid val in EAX
422// CPUID_0 -> rem = vendor string
423/*
424CPUID_1 EDX:
425 0 -> FPU
426 1 -> VME
427 2 -> DE
428 3 -> PSE
429 4 -> TSC
430 5 -> MSR
431 6 -> PAE
432 7 -> MCE
433 8 -> CX8
434 9 -> APIC
435 10 -> Reserved
436 11 -> Fast Call
437 12 -> MTTR
438 13 -> PGE
439 14 -> MCA
440 15 -> CMOV
441 16 -> PAT
442 17 -> PSE36
443 18 -> Serial Number
444 23 -> MMX
445 24 -> FXSR
446 25 -> SSE
447 */
448
449//CPUID_80 -> largest excpuid value in EAX
450//CPUID_81,EAX -> Signature
451//CPUID_80,EDX -> Ext Features
452//CPUID_82 -> CPU String
453//CPUID_83 -> CPU String
454//CPUID_84 -> CPU String
455p->CPU.NoThreads = p->CPU.NoCores;
456
457}
458
459if (!fsbFrequency) {
460fsbFrequency = (DEFAULT_FSB * 1000);
461cpuFrequency = tscFrequency;
462msglog("CPU: fsb=0 ! using the default value 100MHz !\n");
463}
464
465
466p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
467p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
468p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
469p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
470p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
471p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
472p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
473p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
474
475
476
477p->CPU.MaxCoef = maxcoef;
478p->CPU.MaxDiv = maxdiv;
479p->CPU.MinCoef = mindiv;
480p->CPU.CurrCoef = currcoef;
481p->CPU.CurrDiv = currdiv;
482p->CPU.TSCFrequency = tscFrequency;
483p->CPU.FSBFrequency = fsbFrequency;
484p->CPU.CPUFrequency = cpuFrequency;
485
486DBG("CPU: Vendor/Model/ExtModel: 0x%x/0x%x/0x%x\n", p->CPU.Vendor, p->CPU.Model, p->CPU.ExtModel);
487DBG("CPU: Family/ExtFamily: 0x%x/0x%x\n", p->CPU.Family, p->CPU.ExtFamily);
488DBG("CPU: MaxCoef/CurrCoef: 0x%x/0x%x\n", p->CPU.MaxCoef, p->CPU.CurrCoef);
489DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv?2:1, p->CPU.CurrDiv?2:1);
490DBG("CPU: TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
491DBG("CPU: FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);
492DBG("CPU: CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
493DBG("CPU: NoCores/NoThreads: %d/%d\n", p->CPU.NoCores, p->CPU.NoThreads);
494DBG("CPU: Features: 0x%08x\n", p->CPU.Features);
495#if DEBUG_CPU
496pause();
497#endif
498}
499

Archive Download this file

Revision: 676