Chameleon

Chameleon Svn Source Tree

Root/trunk/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 = 0;
234uint64_tfsbFrequency = 0;
235uint64_tcpuFrequency = 0;
236uint64_tmsr = 0;
237uint64_tflex_ratio = 0;
238uint32_tmax_ratio = 0;
239uint32_tmin_ratio = 0;
240uint8_tbus_ratio_max = 0;
241uint8_tcurrdiv = 0;
242uint8_tcurrcoef = 0;
243uint8_tmaxdiv = 0;
244uint8_tmaxcoef = 0;
245const char*newratio;
246intlen = 0;
247intmyfsb = 0;
248uint8_tbus_ratio_min = 0;
249uint32_treg[4];
250charstr[128];
251
252/* get cpuid values */
253do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]);
254do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]);
255
256do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]);
257do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]);
258do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
259
260do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
261if (p->CPU.CPUID[CPUID_0][0] >= 0x5) {
262do_cpuid(5, p->CPU.CPUID[CPUID_5]);
263}
264if (p->CPU.CPUID[CPUID_0][0] >= 6) {
265do_cpuid(6, p->CPU.CPUID[CPUID_6]);
266}
267if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8) {
268do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
269do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
270} else if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
271do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
272}
273
274// #if DEBUG_CPU
275{
276inti;
277DBG("CPUID Raw Values:\n");
278for (i = 0; i < CPUID_MAX; i++) {
279DBG("%02d: %08x-%08x-%08x-%08x\n", i,
280 p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
281 p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
282}
283}
284// #endif
285
286/*
287 EAX (Intel):
288 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
289 +--------+----------------+--------+----+----+--------+--------+--------+
290 |########|Extended family |Extmodel|####|type|familyid| model |stepping|
291 +--------+----------------+--------+----+----+--------+--------+--------+
292
293 EAX (AMD):
294 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
295 +--------+----------------+--------+----+----+--------+--------+--------+
296 |########|Extended family |Extmodel|####|####|familyid| model |stepping|
297 +--------+----------------+--------+----+----+--------+--------+--------+
298*/
299
300p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
301p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
302p->CPU.Stepping= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);// stepping = cpu_feat_eax & 0xF;
303p->CPU.Model= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);// model = (cpu_feat_eax >> 4) & 0xF;
304p->CPU.Family= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);// family = (cpu_feat_eax >> 8) & 0xF;
305//p->CPU.Type= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12);// type = (cpu_feat_eax >> 12) & 0x3;
306p->CPU.ExtModel= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);// ext_model = (cpu_feat_eax >> 16) & 0xF;
307p->CPU.ExtFamily= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);// ext_family = (cpu_feat_eax >> 20) & 0xFF;
308
309p->CPU.Model += (p->CPU.ExtModel << 4);
310
311if (p->CPU.Vendor == CPUID_VENDOR_INTEL)
312{
313/*
314 * Find the number of enabled cores and threads
315 * (which determines whether SMT/Hyperthreading is active).
316 */
317switch (p->CPU.Model)
318{
319case CPU_MODEL_NEHALEM:
320case CPU_MODEL_FIELDS:
321case CPU_MODEL_DALES:
322case CPU_MODEL_NEHALEM_EX:
323case CPU_MODEL_JAKETOWN:
324case CPU_MODEL_SANDYBRIDGE:
325case CPU_MODEL_IVYBRIDGE:
326case CPU_MODEL_HASWELL:
327case CPU_MODEL_HASWELL_SVR:
328//case CPU_MODEL_HASWELL_H:
329case CPU_MODEL_HASWELL_ULT:
330case CPU_MODEL_CRYSTALWELL:
331msr = rdmsr64(MSR_CORE_THREAD_COUNT);
332p->CPU.NoCores= (uint8_t)bitfield((uint32_t)msr, 31, 16);
333p->CPU.NoThreads= (uint8_t)bitfield((uint32_t)msr, 15, 0);
334break;
335
336case CPU_MODEL_DALES_32NM:
337case CPU_MODEL_WESTMERE:
338case CPU_MODEL_WESTMERE_EX:
339msr = rdmsr64(MSR_CORE_THREAD_COUNT);
340p->CPU.NoCores= (uint8_t)bitfield((uint32_t)msr, 19, 16);
341p->CPU.NoThreads= (uint8_t)bitfield((uint32_t)msr, 15, 0);
342break;
343
344default:
345p->CPU.NoCores = 0;
346break;
347} // end switch
348}
349
350if (p->CPU.NoCores == 0)
351{
352p->CPU.NoCores= (uint8_t)(p->CPU.CoresPerPackage & 0xff);
353p->CPU.NoThreads= (uint8_t)(p->CPU.LogicalPerPackage & 0xff);
354}
355
356/* get BrandString (if supported) */
357/* Copyright: from Apple's XNU cpuid.c */
358if (p->CPU.CPUID[CPUID_80][0] > 0x80000004)
359{
360char *s;
361bzero(str, 128);
362/*
363 * The BrandString 48 bytes (max), guaranteed to
364 * be NULL terminated.
365 */
366do_cpuid(0x80000002, reg);
367memcpy(&str[0], (char *)reg, 16);
368do_cpuid(0x80000003, reg);
369memcpy(&str[16], (char *)reg, 16);
370do_cpuid(0x80000004, reg);
371memcpy(&str[32], (char *)reg, 16);
372for (s = str; *s != '\0'; s++)
373{
374if (*s != ' ')
375{
376break;
377}
378}
379strlcpy(p->CPU.BrandString, s, 48);
380
381if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, MIN(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1)))
382{
383/*
384 * This string means we have a firmware-programmable brand string,
385 * and the firmware couldn't figure out what sort of CPU we have.
386 */
387p->CPU.BrandString[0] = '\0';
388}
389p->CPU.BrandString[47] = '\0';
390//DBG("Brandstring = %s\n", p->CPU.BrandString);
391}
392
393//workaround for N270. I don't know why it detected wrong
394// MSR is *NOT* available on the Intel Atom CPU
395if ((p->CPU.Model == CPU_MODEL_ATOM) && (strstr(p->CPU.BrandString, "270")))
396{
397p->CPU.NoCores= 1;
398p->CPU.NoThreads= 2;
399}
400
401if (p->CPU.Vendor == CPUID_VENDOR_AMD)
402{
403p->CPU.NoThreads= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
404p->CPU.NoCores= (uint8_t)bitfield(p->CPU.CPUID[CPUID_88][2], 7, 0) + 1;
405}
406
407/* setup features */
408if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0)
409{
410p->CPU.Features |= CPU_FEATURE_MMX;
411}
412
413if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0)
414{
415p->CPU.Features |= CPU_FEATURE_SSE;
416}
417
418if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0)
419{
420p->CPU.Features |= CPU_FEATURE_SSE2;
421}
422
423if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0)
424{
425p->CPU.Features |= CPU_FEATURE_SSE3;
426}
427
428if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0)
429{
430p->CPU.Features |= CPU_FEATURE_SSE41;
431}
432
433if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0)
434{
435p->CPU.Features |= CPU_FEATURE_SSE42;
436}
437
438if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0)
439{
440p->CPU.Features |= CPU_FEATURE_EM64T;
441}
442
443if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0)
444{
445p->CPU.Features |= CPU_FEATURE_MSR;
446}
447
448//if ((bit(28) & p->CPU.CPUID[CPUID_1][3]) != 0) {
449
450if (p->CPU.NoThreads > p->CPU.NoCores)
451{
452p->CPU.Features |= CPU_FEATURE_HTT;
453}
454
455tscFrequency = measure_tsc_frequency();
456DBG("cpu freq classic = 0x%016llx\n", tscFrequency);
457/* if usual method failed */
458if ( tscFrequency < 1000 )//TEST
459{
460tscFrequency = timeRDTSC() * 20;//measure_tsc_frequency();
461// DBG("cpu freq timeRDTSC = 0x%016llx\n", tscFrequency);
462} else {
463// DBG("cpu freq timeRDTSC = 0x%016llxn", timeRDTSC() * 20);
464}
465
466fsbFrequency = 0;
467cpuFrequency = 0;
468
469if (p->CPU.Vendor == CPUID_VENDOR_INTEL && ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03))) {
470int intelCPU = p->CPU.Model;
471if (p->CPU.Family == 0x06) {
472/* Nehalem CPU model */
473switch (p->CPU.Model) {
474case CPU_MODEL_NEHALEM:
475case CPU_MODEL_FIELDS:
476case CPU_MODEL_DALES:
477case CPU_MODEL_DALES_32NM:
478case CPU_MODEL_WESTMERE:
479case CPU_MODEL_NEHALEM_EX:
480case CPU_MODEL_WESTMERE_EX:
481/* --------------------------------------------------------- */
482case CPU_MODEL_SANDYBRIDGE:
483case CPU_MODEL_JAKETOWN:
484case CPU_MODEL_IVYBRIDGE_XEON:
485case CPU_MODEL_IVYBRIDGE:
486case CPU_MODEL_HASWELL:
487case CPU_MODEL_HASWELL_SVR:
488
489case CPU_MODEL_HASWELL_ULT:
490case CPU_MODEL_CRYSTALWELL:
491/* --------------------------------------------------------- */
492msr = rdmsr64(MSR_PLATFORM_INFO);
493DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
494bus_ratio_max = bitfield(msr, 15, 8);
495bus_ratio_min = bitfield(msr, 47, 40); //valv: not sure about this one (Remarq.1)
496msr = rdmsr64(MSR_FLEX_RATIO);
497DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
498if (bitfield(msr, 16, 16))
499{
500flex_ratio = bitfield(msr, 15, 8);
501/* bcc9: at least on the gigabyte h67ma-ud2h,
502 where the cpu multipler can't be changed to
503 allow overclocking, the flex_ratio msr has unexpected (to OSX)
504 contents.These contents cause mach_kernel to
505 fail to compute the bus ratio correctly, instead
506 causing the system to crash since tscGranularity
507 is inadvertently set to 0.
508 */
509if (flex_ratio == 0)
510{
511/* Clear bit 16 (evidently the presence bit) */
512wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
513msr = rdmsr64(MSR_FLEX_RATIO);
514DBG("Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
515}
516else
517{
518if (bus_ratio_max > flex_ratio)
519{
520bus_ratio_max = flex_ratio;
521}
522}
523}
524
525if (bus_ratio_max)
526{
527fsbFrequency = (tscFrequency / bus_ratio_max);
528}
529
530//valv: Turbo Ratio Limit
531if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
532{
533msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
534
535cpuFrequency = bus_ratio_max * fsbFrequency;
536max_ratio = bus_ratio_max * 10;
537}
538else
539{
540cpuFrequency = tscFrequency;
541}
542if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4))
543{
544max_ratio = atoi(newratio);
545max_ratio = (max_ratio * 10);
546if (len >= 3)
547{
548max_ratio = (max_ratio + 5);
549}
550
551verbose("Bus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
552
553// extreme overclockers may love 320 ;)
554if ((max_ratio >= min_ratio) && (max_ratio <= 320))
555{
556cpuFrequency = (fsbFrequency * max_ratio) / 10;
557if (len >= 3)
558{
559maxdiv = 1;
560}
561else
562{
563maxdiv = 0;
564}
565}
566else
567{
568max_ratio = (bus_ratio_max * 10);
569}
570}
571//valv: to be uncommented if Remarq.1 didn't stick
572/*if (bus_ratio_max > 0) bus_ratio = flex_ratio;*/
573p->CPU.MaxRatio = max_ratio;
574p->CPU.MinRatio = min_ratio;
575
576myfsb = fsbFrequency / 1000000;
577verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio/10); // Bungo: fixed wrong Bus-Ratio readout
578currcoef = bus_ratio_max;
579
580break;
581
582default:
583msr = rdmsr64(MSR_IA32_PERF_STATUS);
584DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
585currcoef = bitfield(msr, 12, 8); // Bungo: reverted to 2263 state because of wrong old CPUs freq. calculating
586/* Non-integer bus ratio for the max-multi*/
587maxdiv = bitfield(msr, 46, 46);
588/* Non-integer bus ratio for the current-multi (undocumented)*/
589currdiv = bitfield(msr, 14, 14);
590
591// This will always be model >= 3
592if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f))
593{
594/* On these models, maxcoef defines TSC freq */
595maxcoef = bitfield(msr, 44, 40);
596}
597else
598{
599/* On lower models, currcoef defines TSC freq */
600/* XXX */
601maxcoef = currcoef;
602}
603
604if (maxcoef)
605{
606if (maxdiv)
607{
608fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
609}
610else
611{
612fsbFrequency = (tscFrequency / maxcoef);
613}
614
615if (currdiv)
616{
617cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
618}
619else
620{
621cpuFrequency = (fsbFrequency * currcoef);
622}
623
624DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
625}
626break;
627}
628}
629/* Mobile CPU */
630if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28))
631{
632p->CPU.Features |= CPU_FEATURE_MOBILE;
633}
634}
635else if ((p->CPU.Vendor == CPUID_VENDOR_AMD) && (p->CPU.Family == 0x0f))
636{
637switch(p->CPU.ExtFamily)
638{
639case 0x00: /* K8 */
640msr = rdmsr64(K8_FIDVID_STATUS);
641maxcoef = bitfield(msr, 21, 16) / 2 + 4;
642currcoef = bitfield(msr, 5, 0) / 2 + 4;
643break;
644
645case 0x01: /* K10 */
646msr = rdmsr64(K10_COFVID_STATUS);
647do_cpuid2(0x00000006, 0, p->CPU.CPUID[CPUID_6]);
648// EffFreq: effective frequency interface
649if (bitfield(p->CPU.CPUID[CPUID_6][2], 0, 0) == 1)
650{
651//uint64_t mperf = measure_mperf_frequency();
652uint64_t aperf = measure_aperf_frequency();
653cpuFrequency = aperf;
654}
655// NOTE: tsc runs at the maccoeff (non turbo)
656//*not* at the turbo frequency.
657maxcoef = bitfield(msr, 54, 49) / 2 + 4;
658currcoef = bitfield(msr, 5, 0) + 0x10;
659currdiv = 2 << bitfield(msr, 8, 6);
660
661break;
662
663case 0x05: /* K14 */
664msr = rdmsr64(K10_COFVID_STATUS);
665currcoef = (bitfield(msr, 54, 49) + 0x10) << 2;
666currdiv = (bitfield(msr, 8, 4) + 1) << 2;
667currdiv += bitfield(msr, 3, 0);
668
669break;
670
671case 0x02: /* K11 */
672// not implimented
673break;
674}
675
676if (maxcoef)
677{
678if (currdiv)
679{
680if (!currcoef)
681{
682currcoef = maxcoef;
683}
684
685if (!cpuFrequency)
686{
687fsbFrequency = ((tscFrequency * currdiv) / currcoef);
688}
689else
690{
691fsbFrequency = ((cpuFrequency * currdiv) / currcoef);
692}
693DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
694}
695else
696{
697if (!cpuFrequency)
698{
699fsbFrequency = (tscFrequency / maxcoef);
700}
701else
702{
703fsbFrequency = (cpuFrequency / maxcoef);
704}
705DBG("%d\n", currcoef);
706}
707}
708else if (currcoef)
709{
710if (currdiv)
711{
712fsbFrequency = ((tscFrequency * currdiv) / currcoef);
713DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
714}
715else
716{
717fsbFrequency = (tscFrequency / currcoef);
718DBG("%d\n", currcoef);
719}
720}
721if (!cpuFrequency) cpuFrequency = tscFrequency;
722}
723
724#if 0
725if (!fsbFrequency)
726{
727fsbFrequency = (DEFAULT_FSB * 1000);
728cpuFrequency = tscFrequency;
729DBG("0 ! using the default value for FSB !\n");
730}
731
732DBG("cpu freq = 0x%016llxn", timeRDTSC() * 20);
733
734#endif
735
736p->CPU.MaxCoef = maxcoef;
737p->CPU.MaxDiv = maxdiv;
738p->CPU.CurrCoef = currcoef;
739p->CPU.CurrDiv = currdiv;
740p->CPU.TSCFrequency = tscFrequency;
741p->CPU.FSBFrequency = fsbFrequency;
742p->CPU.CPUFrequency = cpuFrequency;
743
744// keep formatted with spaces instead of tabs
745DBG("\n---------------------------------------------\n");
746 DBG("------------------ CPU INFO -----------------\n");
747DBG("---------------------------------------------\n");
748DBG("Brand String: %s\n", p->CPU.BrandString); // Processor name (BIOS)
749DBG("Vendor: 0x%x\n", p->CPU.Vendor); // Vendor ex: GenuineIntel
750DBG("Family: 0x%x\n", p->CPU.Family); // Family ex: 6 (06h)
751DBG("ExtFamily: 0x%x\n", p->CPU.ExtFamily);
752DBG("Signature: %x\n", p->CPU.Signature); // CPUID signature
753DBG("Model: 0x%x\n", p->CPU.Model); // Model ex: 37 (025h)
754DBG("ExtModel: 0x%x\n", p->CPU.ExtModel);
755DBG("Stepping: 0x%x\n", p->CPU.Stepping); // Stepping ex: 5 (05h)
756DBG("MaxCoef: 0x%x\n", p->CPU.MaxCoef);
757DBG("CurrCoef: 0x%x\n", p->CPU.CurrCoef);
758DBG("MaxDiv: 0x%x\n", p->CPU.MaxDiv);
759DBG("CurrDiv: 0x%x\n", p->CPU.CurrDiv);
760DBG("TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
761DBG("FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);
762DBG("CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
763DBG("Cores: %d\n", p->CPU.NoCores); // Cores
764DBG("Logical processor: %d\n", p->CPU.NoThreads); // Logical procesor
765DBG("Features: 0x%08x\n", p->CPU.Features);
766
767DBG("\n---------------------------------------------\n");
768#if DEBUG_CPU
769pause();
770#endif
771}
772

Archive Download this file

Revision: 2456