Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2406