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

Archive Download this file

Revision: 2547