Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1174