Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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 "cpu.h"
9#include "bootstruct.h"
10#include "boot.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#define quad(hi,lo)(((uint64_t)(hi)) << 32 | (lo))
23
24/* Only for 32bit values */
25#define bit32(n)(1U << (n))
26#define bitmask32(h,l)((bit32(h)|(bit32(h)-1)) & ~(bit32(l)-1))
27#define bitfield32(x,h,l)((((x) & bitmask32(h,l)) >> l))
28
29/*
30 * timeRDTSC()
31 * This routine sets up PIT counter 2 to count down 1/20 of a second.
32 * It pauses until the value is latched in the counter
33 * and then reads the time stamp counter to return to the caller.
34 */
35uint64_t timeRDTSC(void)
36{
37intattempts = 0;
38uint64_t latchTime;
39uint64_tsaveTime,intermediate;
40unsigned int timerValue, lastValue;
41//boolean_tint_enabled;
42/*
43 * Table of correction factors to account for
44 * - timer counter quantization errors, and
45 * - undercounts 0..5
46 */
47#define SAMPLE_CLKS_EXACT(((double) CLKNUM) / 20.0)
48#define SAMPLE_CLKS_INT((int) CLKNUM / 20)
49#define SAMPLE_NSECS(2000000000LL)
50#define SAMPLE_MULTIPLIER(((double)SAMPLE_NSECS)*SAMPLE_CLKS_EXACT)
51#define ROUND64(x)((uint64_t)((x) + 0.5))
52uint64_tscale[6] = {
53ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-0)),
54ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-1)),
55ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-2)),
56ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-3)),
57ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-4)),
58ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-5))
59};
60
61//int_enabled = ml_set_interrupts_enabled(FALSE);
62
63restart:
64if (attempts >= 9) // increase to up to 9 attempts.
65{
66 // This will flash-reboot. TODO: Use tscPanic instead.
67printf("Timestamp counter calibation failed with %d attempts\n", attempts);
68}
69attempts++;
70enable_PIT2();// turn on PIT2
71set_PIT2(0);// reset timer 2 to be zero
72latchTime = rdtsc64();// get the time stamp to time
73latchTime = get_PIT2(&timerValue) - latchTime; // time how long this takes
74set_PIT2(SAMPLE_CLKS_INT);// set up the timer for (almost) 1/20th a second
75saveTime = rdtsc64();// now time how long a 20th a second is...
76get_PIT2(&lastValue);
77get_PIT2(&lastValue);// read twice, first value may be unreliable
78do {
79intermediate = get_PIT2(&timerValue);
80if (timerValue > lastValue)
81{
82// Timer wrapped
83set_PIT2(0);
84disable_PIT2();
85goto restart;
86}
87lastValue = timerValue;
88} while (timerValue > 5);
89printf("timerValue %d\n",timerValue);
90printf("intermediate 0x%016llx\n",intermediate);
91printf("saveTime 0x%016llx\n",saveTime);
92
93intermediate -= saveTime;// raw count for about 1/20 second
94intermediate *= scale[timerValue];// rescale measured time spent
95intermediate /= SAMPLE_NSECS;// so its exactly 1/20 a second
96intermediate += latchTime;// add on our save fudge
97
98set_PIT2(0);// reset timer 2 to be zero
99disable_PIT2();// turn off PIT 2
100
101//ml_set_interrupts_enabled(int_enabled);
102return intermediate;
103}
104
105/*
106 * DFE: Measures the TSC frequency in Hz (64-bit) using the ACPI PM timer
107 */
108static uint64_t measure_tsc_frequency(void)
109{
110uint64_t tscStart;
111uint64_t tscEnd;
112uint64_t tscDelta = 0xffffffffffffffffULL;
113unsigned long pollCount;
114uint64_t retval = 0;
115int i;
116
117/* Time how many TSC ticks elapse in 30 msec using the 8254 PIT
118 * counter 2. We run this loop 3 times to make sure the cache
119 * is hot and we take the minimum delta from all of the runs.
120 * That is to say that we're biased towards measuring the minimum
121 * number of TSC ticks that occur while waiting for the timer to
122 * expire. That theoretically helps avoid inconsistencies when
123 * running under a VM if the TSC is not virtualized and the host
124 * steals time. The TSC is normally virtualized for VMware.
125 */
126for(i = 0; i < 10; ++i)
127{
128enable_PIT2();
129set_PIT2_mode0(CALIBRATE_LATCH);
130tscStart = rdtsc64();
131pollCount = poll_PIT2_gate();
132tscEnd = rdtsc64();
133/* The poll loop must have run at least a few times for accuracy */
134if (pollCount <= 1) {
135continue;
136}
137/* The TSC must increment at LEAST once every millisecond.
138 * We should have waited exactly 30 msec so the TSC delta should
139 * be >= 30. Anything less and the processor is way too slow.
140 */
141if ((tscEnd - tscStart) <= CALIBRATE_TIME_MSEC) {
142continue;
143}
144// tscDelta = MIN(tscDelta, (tscEnd - tscStart))
145if ( (tscEnd - tscStart) < tscDelta ) {
146tscDelta = tscEnd - tscStart;
147}
148}
149/* tscDelta is now the least number of TSC ticks the processor made in
150 * a timespan of 0.03 s (e.g. 30 milliseconds)
151 * Linux thus divides by 30 which gives the answer in kiloHertz because
152 * 1 / ms = kHz. But we're xnu and most of the rest of the code uses
153 * Hz so we need to convert our milliseconds to seconds. Since we're
154 * dividing by the milliseconds, we simply multiply by 1000.
155 */
156
157/* Unlike linux, we're not limited to 32-bit, but we do need to take care
158 * that we're going to multiply by 1000 first so we do need at least some
159 * arithmetic headroom. For now, 32-bit should be enough.
160 * Also unlike Linux, our compiler can do 64-bit integer arithmetic.
161 */
162if (tscDelta > (1ULL<<32)) {
163retval = 0;
164} else {
165retval = tscDelta * 1000 / 30;
166}
167disable_PIT2();
168return retval;
169}
170
171/*
172 * Original comment/code:
173 * "DFE: Measures the Max Performance Frequency in Hz (64-bit)"
174 *
175 * Measures the Actual Performance Frequency in Hz (64-bit)
176 * (just a naming change, mperf --> aperf )
177 */
178static uint64_t measure_aperf_frequency(void)
179{
180uint64_t aperfStart;
181uint64_t aperfEnd;
182uint64_t aperfDelta = 0xffffffffffffffffULL;
183unsigned long pollCount;
184uint64_t retval = 0;
185int i;
186
187/* Time how many APERF ticks elapse in 30 msec using the 8254 PIT
188 * counter 2. We run this loop 3 times to make sure the cache
189 * is hot and we take the minimum delta from all of the runs.
190 * That is to say that we're biased towards measuring the minimum
191 * number of APERF ticks that occur while waiting for the timer to
192 * expire.
193 */
194for(i = 0; i < 10; ++i)
195{
196enable_PIT2();
197set_PIT2_mode0(CALIBRATE_LATCH);
198aperfStart = rdmsr64(MSR_AMD_APERF);
199pollCount = poll_PIT2_gate();
200aperfEnd = rdmsr64(MSR_AMD_APERF);
201/* The poll loop must have run at least a few times for accuracy */
202if (pollCount <= 1) {
203continue;
204}
205/* The TSC must increment at LEAST once every millisecond.
206 * We should have waited exactly 30 msec so the APERF delta should
207 * be >= 30. Anything less and the processor is way too slow.
208 */
209if ((aperfEnd - aperfStart) <= CALIBRATE_TIME_MSEC) {
210continue;
211}
212// tscDelta = MIN(tscDelta, (tscEnd - tscStart))
213if ( (aperfEnd - aperfStart) < aperfDelta ) {
214aperfDelta = aperfEnd - aperfStart;
215}
216}
217/* mperfDelta is now the least number of MPERF ticks the processor made in
218 * a timespan of 0.03 s (e.g. 30 milliseconds)
219 */
220
221if (aperfDelta > (1ULL<<32)) {
222retval = 0;
223} else {
224retval = aperfDelta * 1000 / 30;
225}
226disable_PIT2();
227return retval;
228}
229
230/*
231 * Calculates the FSB and CPU frequencies using specific MSRs for each CPU
232 * - multi. is read from a specific MSR. In the case of Intel, there is:
233 * a max multi. (used to calculate the FSB freq.),
234 * and a current multi. (used to calculate the CPU freq.)
235 * - fsbFrequency = tscFrequency / multi
236 * - cpuFrequency = fsbFrequency * multi
237 */
238void scan_cpu(PlatformInfo_t *p)
239{
240uint64_ttscFrequency = 0;
241uint64_tfsbFrequency = 0;
242uint64_tcpuFrequency = 0;
243uint64_tmsr = 0;
244uint64_tflex_ratio = 0;
245uint32_tmax_ratio = 0;
246uint32_tmin_ratio = 0;
247uint8_tbus_ratio_max = 0;
248uint8_tcurrdiv = 0;
249uint8_tcurrcoef = 0;
250uint8_tmaxdiv = 0;
251uint8_tmaxcoef = 0;
252const char*newratio;
253intlen = 0;
254intmyfsb = 0;
255uint8_tbus_ratio_min = 0;
256
257/* get cpuid values */
258do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]);
259do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]);
260do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]);
261do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]);
262do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
263do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
264if (p->CPU.CPUID[CPUID_0][0] >= 0x5) {
265do_cpuid(5, p->CPU.CPUID[CPUID_5]);
266}
267if (p->CPU.CPUID[CPUID_0][0] >= 6) {
268do_cpuid(6, p->CPU.CPUID[CPUID_6]);
269}
270if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8) {
271do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
272do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
273} else if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
274do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
275}
276
277#if DEBUG_CPU
278{
279inti;
280printf("CPUID Raw Values:\n");
281for (i=0; i<CPUID_MAX; i++) {
282printf("%02d: %08x-%08x-%08x-%08x\n", i,
283 p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
284 p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
285}
286}
287#endif
288
289/* http://www.flounder.com/cpuid_explorer2.htm
290 EAX (Intel):
291 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
292 +--------+----------------+--------+----+----+--------+--------+--------+
293 |########|Extended family |Extmodel|####|type|familyid| model |stepping|
294 +--------+----------------+--------+----+----+--------+--------+--------+
295
296 EAX (AMD):
297 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
298 +--------+----------------+--------+----+----+--------+--------+--------+
299 |########|Extended family |Extmodel|####|####|familyid| model |stepping|
300 +--------+----------------+--------+----+----+--------+--------+--------+
301*/
302
303p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
304p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
305// stepping = cpu_feat_eax & 0xF;
306p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
307// model = (cpu_feat_eax >> 4) & 0xF;
308p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
309// family = (cpu_feat_eax >> 8) & 0xF;
310p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
311// type = (cpu_feat_eax >> 12) & 0x3;
312//p->CPU.Type= bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12);
313// ext_model = (cpu_feat_eax >> 16) & 0xF;
314p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
315// ext_family = (cpu_feat_eax >> 20) & 0xFF;
316p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
317
318p->CPU.Model += (p->CPU.ExtModel << 4);
319
320if (p->CPU.Vendor == CPUID_VENDOR_INTEL &&
321p->CPU.Family == 0x06 &&
322p->CPU.Model >= CPU_MODEL_NEHALEM &&
323p->CPU.Model != CPU_MODEL_ATOM// MSR is *NOT* available on the Intel Atom CPU
324) {
325msr = rdmsr64(MSR_CORE_THREAD_COUNT);// Undocumented MSR in Nehalem and newer CPUs
326p->CPU.NoCores= bitfield((uint32_t)msr, 31, 16);// Using undocumented MSR to get actual values
327p->CPU.NoThreads= bitfield((uint32_t)msr, 15, 0);// Using undocumented MSR to get actual values
328} else if (p->CPU.Vendor == CPUID_VENDOR_AMD) {
329p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
330p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_88][2], 7, 0) + 1;
331} else {
332// Use previous method for Cores and Threads
333p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
334p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;
335}
336
337/* get brand string (if supported) */
338/* Copyright: from Apple's XNU cpuid.c */
339if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {
340uint32_treg[4];
341charstr[128], *s;
342/*
343 * The brand string 48 bytes (max), guaranteed to
344 * be NULL terminated.
345 */
346do_cpuid(0x80000002, reg);
347bcopy((char *)reg, &str[0], 16);
348do_cpuid(0x80000003, reg);
349bcopy((char *)reg, &str[16], 16);
350do_cpuid(0x80000004, reg);
351bcopy((char *)reg, &str[32], 16);
352for (s = str; *s != '\0'; s++) {
353if (*s != ' ') {
354break;
355}
356}
357
358strlcpy(p->CPU.BrandString, s, sizeof(p->CPU.BrandString));
359
360if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, MIN(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {
361/*
362 * This string means we have a firmware-programmable brand string,
363 * and the firmware couldn't figure out what sort of CPU we have.
364 */
365p->CPU.BrandString[0] = '\0';
366}
367}
368
369/* setup features */
370if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0) {
371p->CPU.Features |= CPU_FEATURE_MMX;
372}
373if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0) {
374p->CPU.Features |= CPU_FEATURE_SSE;
375}
376if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0) {
377p->CPU.Features |= CPU_FEATURE_SSE2;
378}
379if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0) {
380p->CPU.Features |= CPU_FEATURE_SSE3;
381}
382if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0) {
383p->CPU.Features |= CPU_FEATURE_SSE41;
384}
385if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0) {
386p->CPU.Features |= CPU_FEATURE_SSE42;
387}
388if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0) {
389p->CPU.Features |= CPU_FEATURE_EM64T;
390}
391if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0) {
392p->CPU.Features |= CPU_FEATURE_MSR;
393}
394//if ((bit(28) & p->CPU.CPUID[CPUID_1][3]) != 0) {
395if (p->CPU.NoThreads > p->CPU.NoCores) {
396p->CPU.Features |= CPU_FEATURE_HTT;
397}
398
399tscFrequency = measure_tsc_frequency();
400DBG("cpu freq classic = 0x%016llx\n", tscFrequency);
401/* if usual method failed */
402if ( tscFrequency < 1000 ) { //TEST
403tscFrequency = timeRDTSC() * 20;//measure_tsc_frequency();
404// DBG("cpu freq timeRDTSC = 0x%016llx\n", tscFrequency);
405} else {
406// DBG("cpu freq timeRDTSC = 0x%016llxn", timeRDTSC() * 20);
407}
408fsbFrequency = 0;
409cpuFrequency = 0;
410
411if ((p->CPU.Vendor == CPUID_VENDOR_INTEL) && ((p->CPU.Family == 0x06) || (p->CPU.Family == 0x0f))) {
412int intelCPU = p->CPU.Model;
413if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)){
414/* Nehalem CPU model */
415if (p->CPU.Family == 0x06 && (p->CPU.Model == CPU_MODEL_NEHALEM||
416 p->CPU.Model == CPU_MODEL_FIELDS||
417 p->CPU.Model == CPU_MODEL_DALES||
418 p->CPU.Model == CPU_MODEL_DALES_32NM||
419 p->CPU.Model == CPU_MODEL_WESTMERE||
420 p->CPU.Model == CPU_MODEL_NEHALEM_EX||
421 p->CPU.Model == CPU_MODEL_WESTMERE_EX ||
422 p->CPU.Model == CPU_MODEL_SANDYBRIDGE ||
423 p->CPU.Model == CPU_MODEL_JAKETOWN ||
424 p->CPU.Model == CPU_MODEL_IVYBRIDGE_XEON||
425 p->CPU.Model == CPU_MODEL_IVYBRIDGE ||
426 p->CPU.Model == CPU_MODEL_HASWELL ||
427 p->CPU.Model == CPU_MODEL_HASWELL_SVR ||
428 //p->CPU.Model == CPU_MODEL_HASWELL_H ||
429 p->CPU.Model == CPU_MODEL_HASWELL_ULT ||
430 p->CPU.Model == CPU_MODEL_CRYSTALWELL ))
431{
432msr = rdmsr64(MSR_PLATFORM_INFO);
433DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
434bus_ratio_max = bitfield(msr, 15, 8);
435bus_ratio_min = bitfield(msr, 47, 40); //valv: not sure about this one (Remarq.1)
436msr = rdmsr64(MSR_FLEX_RATIO);
437DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
438if (bitfield(msr, 16, 16)) {
439flex_ratio = bitfield(msr, 15, 8);
440/* bcc9: at least on the gigabyte h67ma-ud2h,
441 where the cpu multipler can't be changed to
442 allow overclocking, the flex_ratio msr has unexpected (to OSX)
443 contents.These contents cause mach_kernel to
444 fail to compute the bus ratio correctly, instead
445 causing the system to crash since tscGranularity
446 is inadvertently set to 0.
447 */
448if (flex_ratio == 0) {
449/* Clear bit 16 (evidently the presence bit) */
450wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
451msr = rdmsr64(MSR_FLEX_RATIO);
452verbose("Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
453} else {
454if (bus_ratio_max > flex_ratio) {
455bus_ratio_max = flex_ratio;
456}
457}
458}
459
460if (bus_ratio_max) {
461fsbFrequency = (tscFrequency / bus_ratio_max);
462}
463//valv: Turbo Ratio Limit
464if ((intelCPU != 0x2e) && (intelCPU != 0x2f)) {
465msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
466cpuFrequency = bus_ratio_max * fsbFrequency;
467max_ratio = bus_ratio_max * 10;
468} else {
469cpuFrequency = tscFrequency;
470}
471if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4)) {
472max_ratio = atoi(newratio);
473max_ratio = (max_ratio * 10);
474if (len >= 3) {
475max_ratio = (max_ratio + 5);
476}
477
478verbose("Bus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
479
480// extreme overclockers may love 320 ;)
481if ((max_ratio >= min_ratio) && (max_ratio <= 320)) {
482cpuFrequency = (fsbFrequency * max_ratio) / 10;
483if (len >= 3) {
484maxdiv = 1;
485} else {
486maxdiv = 0;
487}
488} else {
489max_ratio = (bus_ratio_max * 10);
490}
491}
492//valv: to be uncommented if Remarq.1 didn't stick
493/*if (bus_ratio_max > 0) bus_ratio = flex_ratio;*/
494p->CPU.MaxRatio = max_ratio;
495p->CPU.MinRatio = min_ratio;
496
497myfsb = fsbFrequency / 1000000;
498verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio/10); // Bungo: fixed wrong Bus-Ratio readout
499currcoef = bus_ratio_max;
500} else {
501msr = rdmsr64(MSR_IA32_PERF_STATUS);
502DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
503currcoef = bitfield(msr, 12, 8); // Bungo: reverted to 2263 state because of wrong old CPUs freq. calculating
504/* Non-integer bus ratio for the max-multi*/
505maxdiv = bitfield(msr, 46, 46);
506/* Non-integer bus ratio for the current-multi (undocumented)*/
507currdiv = bitfield(msr, 14, 14);
508
509// This will always be model >= 3
510if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f)) {
511/* On these models, maxcoef defines TSC freq */
512maxcoef = bitfield(msr, 44, 40);
513} else {
514/* On lower models, currcoef defines TSC freq */
515/* XXX */
516maxcoef = currcoef;
517}
518
519if (maxcoef) {
520if (maxdiv) {
521fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
522} else {
523fsbFrequency = (tscFrequency / maxcoef);
524}
525if (currdiv) {
526cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
527} else {
528cpuFrequency = (fsbFrequency * currcoef);
529}
530DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
531}
532}
533}
534/* Mobile CPU */
535if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28)) {
536p->CPU.Features |= CPU_FEATURE_MOBILE;
537}
538} else if ((p->CPU.Vendor == CPUID_VENDOR_AMD) && (p->CPU.Family == 0x0f)) {
539switch(p->CPU.ExtFamily) {
540case 0x00: /* K8 */
541msr = rdmsr64(K8_FIDVID_STATUS);
542maxcoef = bitfield(msr, 21, 16) / 2 + 4;
543currcoef = bitfield(msr, 5, 0) / 2 + 4;
544break;
545
546case 0x01: /* K10 */
547msr = rdmsr64(K10_COFVID_STATUS);
548do_cpuid2(0x00000006, 0, p->CPU.CPUID[CPUID_6]);
549// EffFreq: effective frequency interface
550if (bitfield(p->CPU.CPUID[CPUID_6][2], 0, 0) == 1) {
551//uint64_t mperf = measure_mperf_frequency();
552uint64_t aperf = measure_aperf_frequency();
553cpuFrequency = aperf;
554}
555// NOTE: tsc runs at the maccoeff (non turbo)
556//*not* at the turbo frequency.
557maxcoef = bitfield(msr, 54, 49) / 2 + 4;
558currcoef = bitfield(msr, 5, 0) + 0x10;
559currdiv = 2 << bitfield(msr, 8, 6);
560
561break;
562
563case 0x05: /* K14 */
564msr = rdmsr64(K10_COFVID_STATUS);
565currcoef = (bitfield(msr, 54, 49) + 0x10) << 2;
566currdiv = (bitfield(msr, 8, 4) + 1) << 2;
567currdiv += bitfield(msr, 3, 0);
568
569break;
570
571case 0x02: /* K11 */
572// not implimented
573break;
574}
575
576if (maxcoef) {
577if (currdiv) {
578if (!currcoef) {
579currcoef = maxcoef;
580}
581
582if (!cpuFrequency) {
583fsbFrequency = ((tscFrequency * currdiv) / currcoef);
584} else {
585fsbFrequency = ((cpuFrequency * currdiv) / currcoef);
586}
587DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
588} else {
589if (!cpuFrequency) {
590fsbFrequency = (tscFrequency / maxcoef);
591} else {
592fsbFrequency = (cpuFrequency / maxcoef);
593}
594DBG("%d\n", currcoef);
595}
596} else if (currcoef) {
597if (currdiv) {
598fsbFrequency = ((tscFrequency * currdiv) / currcoef);
599DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
600} else {
601fsbFrequency = (tscFrequency / currcoef);
602DBG("%d\n", currcoef);
603}
604}
605if (!cpuFrequency) cpuFrequency = tscFrequency;
606}
607
608#if 0
609if (!fsbFrequency) {
610fsbFrequency = (DEFAULT_FSB * 1000);
611cpuFrequency = tscFrequency;
612DBG("0 ! using the default value for FSB !\n");
613}
614
615DBG("cpu freq = 0x%016llxn", timeRDTSC() * 20);
616
617#endif
618
619p->CPU.MaxCoef = maxcoef;
620p->CPU.MaxDiv = maxdiv;
621p->CPU.CurrCoef = currcoef;
622p->CPU.CurrDiv = currdiv;
623p->CPU.TSCFrequency = tscFrequency;
624p->CPU.FSBFrequency = fsbFrequency;
625p->CPU.CPUFrequency = cpuFrequency;
626
627// keep formatted with spaces instead of tabs
628DBG("\n---------------------------------------------\n");
629 DBG("--------------- CPU INFO --------------------\n");
630DBG("---------------------------------------------\n");
631DBG("Brand String: %s\n", p->CPU.BrandString); // Processor name (BIOS)
632DBG("Vendor: 0x%x\n", p->CPU.Vendor); // Vendor ex: GenuineIntel
633DBG("Family: 0x%x\n", p->CPU.Family); // Family ex: 6 (06h)
634DBG("ExtFamily: 0x%x\n", p->CPU.ExtFamily);
635DBG("Signature: %x\n", p->CPU.Signature); // CPUID signature
636/*switch (p->CPU.Type) {
637case PT_OEM:
638DBG("Processor type: Intel Original OEM Processor\n");
639break;
640case PT_OD:
641DBG("Processor type: Intel Over Drive Processor\n");
642break;
643case PT_DUAL:
644DBG("Processor type: Intel Dual Processor\n");
645break;
646case PT_RES:
647DBG("Processor type: Intel Reserved\n");
648break;
649default:
650break;
651}*/
652DBG("Model: 0x%x\n", p->CPU.Model); // Model ex: 37 (025h)
653DBG("ExtModel: 0x%x\n", p->CPU.ExtModel);
654DBG("Stepping: 0x%x\n", p->CPU.Stepping); // Stepping ex: 5 (05h)
655DBG("MaxCoef/CurrCoef: 0x%x/0x%x\n", p->CPU.MaxCoef, p->CPU.CurrCoef);
656DBG("MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv, p->CPU.CurrDiv);
657DBG("TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
658DBG("FSBFreq: %dMHz\n", (p->CPU.FSBFrequency + 500000) / 1000000);
659DBG("CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
660DBG("Cores: %d\n", p->CPU.NoCores); // Cores
661DBG("Logical processor: %d\n", p->CPU.NoThreads); // Logical procesor
662DBG("Features: 0x%08x\n", p->CPU.Features);
663DBG("\n---------------------------------------------\n");
664#if DEBUG_CPU
665pause();
666#endif
667}
668

Archive Download this file

Revision: 2366