Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/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_tbus_ratio_min = 0;
242uint8_tcurrdiv = 0;
243uint8_tcurrcoef = 0;
244uint8_tmaxdiv = 0;
245uint8_tmaxcoef = 0;
246
247const char*newratio;
248intlen = 0;
249
250/* get cpuid values */
251do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]);
252do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]);
253do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]);
254do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]);
255do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
256do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
257if (p->CPU.CPUID[CPUID_0][0] >= 0x5) {
258do_cpuid(5, p->CPU.CPUID[CPUID_5]);
259}
260if (p->CPU.CPUID[CPUID_0][0] >= 6) {
261do_cpuid(6, p->CPU.CPUID[CPUID_6]);
262}
263if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8) {
264do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
265do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
266} else if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
267do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
268}
269
270#if DEBUG_CPU
271{
272inti;
273printf("CPUID Raw Values:\n");
274for (i=0; i<CPUID_MAX; i++) {
275printf("%02d: %08x-%08x-%08x-%08x\n", i,
276 p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
277 p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
278}
279}
280#endif
281
282/*
283 EAX (Intel):
284 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
285 +--------+----------------+--------+----+----+--------+--------+--------+
286 |########|Extended family |Extmodel|####|type|familyid| model |stepping|
287 +--------+----------------+--------+----+----+--------+--------+--------+
288
289 EAX (AMD):
290 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
291 +--------+----------------+--------+----+----+--------+--------+--------+
292 |########|Extended family |Extmodel|####|####|familyid| model |stepping|
293 +--------+----------------+--------+----+----+--------+--------+--------+
294*/
295
296p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
297p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
298p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0); // stepping = cpu_feat_eax & 0xF;
299p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4); // model = (cpu_feat_eax >> 4) & 0xF;
300p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8); // family = (cpu_feat_eax >> 8) & 0xF;
301//p->CPU.Type= bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12); // type = (cpu_feat_eax >> 12) & 0x3;
302p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16); // ext_model = (cpu_feat_eax >> 16) & 0xF;
303p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20); // ext_family = (cpu_feat_eax >> 20) & 0xFF;
304p->CPU.Model += (p->CPU.ExtModel << 4);
305
306if (p->CPU.Vendor == CPUID_VENDOR_INTEL &&
307p->CPU.Family == 0x06 &&
308p->CPU.Model >= CPU_MODEL_NEHALEM &&
309p->CPU.Model != CPU_MODEL_ATOM// MSR is *NOT* available on the Intel Atom CPU
310) {
311msr = rdmsr64(MSR_CORE_THREAD_COUNT);// MacMan: Undocumented MSR in Nehalem and newer CPUs
312p->CPU.NoCores= bitfield((uint32_t)msr, 31, 16);// MacMan: Using undocumented MSR to get actual values
313p->CPU.NoThreads= bitfield((uint32_t)msr, 15, 0);// MacMan: Using undocumented MSR to get actual values
314} else if (p->CPU.Vendor == CPUID_VENDOR_AMD) {
315p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
316p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_88][2], 7, 0) + 1;
317} else {
318// Use previous method for Cores and Threads
319p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
320p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;
321}
322
323/* get brand string (if supported) */
324/* Copyright: from Apple's XNU cpuid.c */
325if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {
326uint32_treg[4];
327charstr[128], *s;
328/*
329 * The brand string 48 bytes (max), guaranteed to
330 * be NULL terminated.
331 */
332do_cpuid(0x80000002, reg);
333bcopy((char *)reg, &str[0], 16);
334do_cpuid(0x80000003, reg);
335bcopy((char *)reg, &str[16], 16);
336do_cpuid(0x80000004, reg);
337bcopy((char *)reg, &str[32], 16);
338for (s = str; *s != '\0'; s++) {
339if (*s != ' ') {
340 break;
341 }
342}
343
344strlcpy(p->CPU.BrandString, s, sizeof(p->CPU.BrandString));
345
346if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, MIN(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {
347/*
348 * This string means we have a firmware-programmable brand string,
349 * and the firmware couldn't figure out what sort of CPU we have.
350 */
351p->CPU.BrandString[0] = '\0';
352}
353}
354
355/* setup features */
356if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0) {
357p->CPU.Features |= CPU_FEATURE_MMX;
358}
359if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0) {
360p->CPU.Features |= CPU_FEATURE_SSE;
361}
362if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0) {
363p->CPU.Features |= CPU_FEATURE_SSE2;
364}
365if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0) {
366p->CPU.Features |= CPU_FEATURE_SSE3;
367}
368if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0) {
369p->CPU.Features |= CPU_FEATURE_SSE41;
370}
371if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0) {
372p->CPU.Features |= CPU_FEATURE_SSE42;
373}
374if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0) {
375p->CPU.Features |= CPU_FEATURE_EM64T;
376}
377if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0) {
378p->CPU.Features |= CPU_FEATURE_MSR;
379}
380//if ((bit(28) & p->CPU.CPUID[CPUID_1][3]) != 0) {
381if (p->CPU.NoThreads > p->CPU.NoCores) {
382p->CPU.Features |= CPU_FEATURE_HTT;
383}
384
385tscFrequency = measure_tsc_frequency();
386/* if usual method failed */
387if ( tscFrequency < 1000 ) { //TEST
388tscFrequency = timeRDTSC() * 20;
389}
390fsbFrequency = 0;
391cpuFrequency = 0;
392
393if ((p->CPU.Vendor == CPUID_VENDOR_INTEL) && ((p->CPU.Family == 0x06) || (p->CPU.Family == 0x0f))) {
394int intelCPU = p->CPU.Model;
395if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)) {
396/* Nehalem CPU model */
397if (p->CPU.Family == 0x06 && (
398 p->CPU.Model == CPU_MODEL_NEHALEM ||
399p->CPU.Model == CPU_MODEL_FIELDS ||
400p->CPU.Model == CPU_MODEL_DALES ||
401p->CPU.Model == CPU_MODEL_DALES_32NM ||
402p->CPU.Model == CPU_MODEL_WESTMERE ||
403p->CPU.Model == CPU_MODEL_NEHALEM_EX ||
404p->CPU.Model == CPU_MODEL_WESTMERE_EX ||
405p->CPU.Model == CPU_MODEL_SANDYBRIDGE ||
406p->CPU.Model == CPU_MODEL_JAKETOWN ||
407p->CPU.Model == CPU_MODEL_IVYBRIDGE ||
408 p->CPU.Model == CPU_MODEL_IVYBRIDGE_XEON||
409 p->CPU.Model == CPU_MODEL_HASWELL ||
410p->CPU.Model == CPU_MODEL_HASWELL_SVR ||
411p->CPU.Model == CPU_MODEL_HASWELL_ULT ||
412p->CPU.Model == CPU_MODEL_CRYSTALWELL ||
413p->CPU.Model == CPU_MODEL_BROADWELL )){
414msr = rdmsr64(MSR_PLATFORM_INFO);
415//DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
416bus_ratio_max = bitfield(msr, 15, 8);//MacMan: Changed bitfield to match Apple tsc.c
417 bus_ratio_min = bitfield(msr, 47, 40);//MacMan: Changed bitfield to match Apple tsc.c
418msr = rdmsr64(MSR_FLEX_RATIO);
419//DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
420if (bitfield(msr, 16, 16)) {
421flex_ratio = bitfield(msr, 15, 8);//MacMan: Changed bitfield to match Apple tsc.c
422if (flex_ratio == 0) {
423/* Clear bit 16 (evidently the presence bit) */
424wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
425msr = rdmsr64(MSR_FLEX_RATIO);
426//verbose("Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
427} else {
428if (bus_ratio_max > flex_ratio) {
429bus_ratio_max = flex_ratio;
430}
431}
432}
433
434if (bus_ratio_max) {
435fsbFrequency = (tscFrequency / bus_ratio_max);
436}
437//MacMan: Turbo Ratio Limit
438switch (intelCPU)
439{
440case CPU_MODEL_WESTMERE_EX: // Intel Xeon E7
441case CPU_MODEL_NEHALEM_EX: // Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65xx
442{
443cpuFrequency = tscFrequency;
444DBG("cpu.c (%d)CPU_MODEL_NEHALEM_EX or CPU_MODEL_WESTMERE_EX Found\n", __LINE__);
445break;
446}
447case CPU_MODEL_SANDYBRIDGE: // Intel Core i3, i5, i7 LGA1155 (32nm)
448case CPU_MODEL_IVYBRIDGE: // Intel Core i3, i5, i7 LGA1155 (22nm)
449case CPU_MODEL_JAKETOWN: // Intel Core i7, Xeon E5 LGA2011 (32nm)
450 case CPU_MODEL_IVYBRIDGE_XEON: // Intel Core i7, Xeon E5 LGA2011 (22nm)
451 case CPU_MODEL_HASWELL: // Intel Core i3, i5, i7, Xeon E3 LGA1050 (22nm)
452 case CPU_MODEL_HASWELL_ULT:
453 case CPU_MODEL_CRYSTALWELL:
454 case CPU_MODEL_BROADWELL:
455{
456msr = rdmsr64(MSR_IA32_PERF_STATUS);
457currcoef = bitfield(msr, 15, 8);
458cpuFrequency = currcoef * fsbFrequency;
459maxcoef = bus_ratio_max;
460break;
461}
462default:
463{
464msr = rdmsr64(MSR_IA32_PERF_STATUS);
465currcoef = bitfield(msr, 7, 0);
466cpuFrequency = currcoef * fsbFrequency;
467maxcoef = bus_ratio_max;
468break;
469}
470}
471
472if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4)) {
473max_ratio = atoi(newratio);
474max_ratio = (max_ratio * 10);
475if (len >= 3) {
476 max_ratio = (max_ratio + 5);
477 }
478
479verbose("Bus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
480
481// extreme overclockers may love 320 ;)
482if ((max_ratio >= min_ratio) && (max_ratio <= 320)) {
483cpuFrequency = (fsbFrequency * max_ratio) / 10;
484if (len >= 3) {
485 maxdiv = 1;
486 } else {
487 maxdiv = 0;
488 }
489} else {
490max_ratio = (bus_ratio_max * 10);
491}
492}
493p->CPU.MaxRatio = bus_ratio_max;
494p->CPU.MinRatio = bus_ratio_min;
495} else {
496msr = rdmsr64(MSR_IA32_PERF_STATUS);
497DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
498currcoef = bitfield(msr, 15, 8); //MacMan: Fixed bitfield to Intel documentation
499/* Non-integer bus ratio for the max-multi*/
500maxdiv = bitfield(msr, 46, 46);
501/* Non-integer bus ratio for the current-multi (undocumented)*/
502currdiv = bitfield(msr, 14, 14);
503
504// This will always be model >= 3
505if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f)) {
506/* On these models, maxcoef defines TSC freq */
507maxcoef = bitfield(msr, 44, 40);
508} else {
509/* On lower models, currcoef defines TSC freq */
510/* XXX */
511maxcoef = currcoef;
512}
513
514if (maxcoef) {
515if (maxdiv) {
516fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
517} else {
518fsbFrequency = (tscFrequency / maxcoef);
519}
520if (currdiv) {
521cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
522} else {
523cpuFrequency = (fsbFrequency * currcoef);
524}
525DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
526}
527}
528}
529/* Mobile CPU */
530if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28)) {
531p->CPU.Features |= CPU_FEATURE_MOBILE;
532}
533} else if ((p->CPU.Vendor == CPUID_VENDOR_AMD) && (p->CPU.Family == 0x0f)) {
534switch(p->CPU.ExtFamily) {
535case 0x00: /* K8 */
536msr = rdmsr64(K8_FIDVID_STATUS);
537maxcoef = bitfield(msr, 21, 16) / 2 + 4;
538currcoef = bitfield(msr, 5, 0) / 2 + 4;
539break;
540
541case 0x01: /* K10 */
542msr = rdmsr64(K10_COFVID_STATUS);
543do_cpuid2(0x00000006, 0, p->CPU.CPUID[CPUID_6]);
544// EffFreq: effective frequency interface
545if (bitfield(p->CPU.CPUID[CPUID_6][2], 0, 0) == 1) {
546//uint64_t mperf = measure_mperf_frequency();
547uint64_t aperf = measure_aperf_frequency();
548cpuFrequency = aperf;
549}
550// NOTE: tsc runs at the maccoeff (non turbo)
551//*not* at the turbo frequency.
552maxcoef = bitfield(msr, 54, 49) / 2 + 4;
553currcoef = bitfield(msr, 5, 0) + 0x10;
554currdiv = 2 << bitfield(msr, 8, 6);
555
556break;
557
558case 0x05: /* K14 */
559msr = rdmsr64(K10_COFVID_STATUS);
560currcoef = (bitfield(msr, 54, 49) + 0x10) << 2;
561currdiv = (bitfield(msr, 8, 4) + 1) << 2;
562currdiv += bitfield(msr, 3, 0);
563
564break;
565
566case 0x02: /* K11 */
567// not implimented
568break;
569}
570
571if (maxcoef) {
572if (currdiv) {
573if (!currcoef) {
574 currcoef = maxcoef;
575 }
576
577if (!cpuFrequency) {
578fsbFrequency = ((tscFrequency * currdiv) / currcoef);
579} else {
580fsbFrequency = ((cpuFrequency * currdiv) / currcoef);
581}
582DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
583} else {
584if (!cpuFrequency) {
585fsbFrequency = (tscFrequency / maxcoef);
586} else {
587fsbFrequency = (cpuFrequency / maxcoef);
588 }
589DBG("%d\n", currcoef);
590}
591} else if (currcoef) {
592if (currdiv) {
593fsbFrequency = ((tscFrequency * currdiv) / currcoef);
594DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
595} else {
596fsbFrequency = (tscFrequency / currcoef);
597DBG("%d\n", currcoef);
598}
599}
600if (!cpuFrequency) cpuFrequency = tscFrequency;
601}
602
603#if 0
604if (!fsbFrequency) {
605fsbFrequency = (DEFAULT_FSB * 1000);
606cpuFrequency = tscFrequency;
607DBG("0 ! using the default value for FSB !\n");
608}
609#endif
610
611p->CPU.MaxCoef = maxcoef;
612if (maxdiv == 0){
613p->CPU.MaxDiv = bus_ratio_max;
614} else {
615p->CPU.MaxDiv = maxdiv;
616}
617p->CPU.CurrCoef = currcoef;
618if (currdiv == 0){
619p->CPU.CurrDiv = currcoef;
620} else {
621p->CPU.CurrDiv = currdiv;
622}
623p->CPU.TSCFrequency = tscFrequency;
624p->CPU.FSBFrequency = fsbFrequency;
625p->CPU.CPUFrequency = cpuFrequency;
626
627// keep formatted with spaces instead of tabs
628DBG("CPU: Brand String: %s\n", p->CPU.BrandString);
629DBG("CPU: Vendor: 0x%x\n", p->CPU.Vendor);
630DBG("CPU: Family / ExtFamily: 0x%x / 0x%x\n", p->CPU.Family, p->CPU.ExtFamily);
631DBG("CPU: Model / ExtModel / Stepping: 0x%x / 0x%x / 0x%x\n", p->CPU.Model, p->CPU.ExtModel, p->CPU.Stepping);
632DBG("CPU: Number of Cores / Threads: %d / %d\n", p->CPU.NoCores, p->CPU.NoThreads);
633DBG("CPU: Features: 0x%08x\n", p->CPU.Features);
634DBG("CPU: TSC Frequency: %d MHz\n", p->CPU.TSCFrequency / 1000000);
635DBG("CPU: FSB Frequency: %d MHz\n", p->CPU.FSBFrequency / 1000000);
636DBG("CPU: CPU Frequency: %d MHz\n", p->CPU.CPUFrequency / 1000000);
637DBG("CPU: Minimum Bus Ratio: %d\n", p->CPU.MinRatio);
638DBG("CPU: Maximum Bus Ratio: %d\n", p->CPU.MaxRatio);
639DBG("CPU: Current Bus Ratio: %d\n", p->CPU.CurrCoef);
640//DBG("CPU: Maximum Multiplier: %d\n", p->CPU.MaxCoef);
641//DBG("CPU: Maximum Divider: %d\n", p->CPU.MaxDiv);
642//DBG("CPU: Current Divider: %d\n", p->CPU.CurrDiv);
643
644#if DEBUG_CPU
645pause();
646#endif
647}
648

Archive Download this file

Revision: 2658