Chameleon

Chameleon Svn Source Tree

Root/branches/zenith432/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...)
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 */
28static uint64_t timeRDTSC(void)
29{
30intattempts = 0;
31uint64_t latchTime;
32uint64_tsaveTime,intermediate;
33unsigned inttimerValue, 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)
128{
129continue;
130}
131/* The TSC must increment at LEAST once every millisecond.
132 * We should have waited exactly 30 msec so the TSC delta should
133 * be >= 30. Anything less and the processor is way too slow.
134 */
135if ((tscEnd - tscStart) <= CALIBRATE_TIME_MSEC)
136{
137continue;
138}
139// tscDelta = MIN(tscDelta, (tscEnd - tscStart))
140if ( (tscEnd - tscStart) < tscDelta )
141{
142tscDelta = tscEnd - tscStart;
143}
144}
145/* tscDelta is now the least number of TSC ticks the processor made in
146 * a timespan of 0.03 s (e.g. 30 milliseconds)
147 * Linux thus divides by 30 which gives the answer in kiloHertz because
148 * 1 / ms = kHz. But we're xnu and most of the rest of the code uses
149 * Hz so we need to convert our milliseconds to seconds. Since we're
150 * dividing by the milliseconds, we simply multiply by 1000.
151 */
152
153/* Unlike linux, we're not limited to 32-bit, but we do need to take care
154 * that we're going to multiply by 1000 first so we do need at least some
155 * arithmetic headroom. For now, 32-bit should be enough.
156 * Also unlike Linux, our compiler can do 64-bit integer arithmetic.
157 */
158if (tscDelta > (1ULL<<32))
159{
160retval = 0;
161}
162else
163{
164retval = tscDelta * 1000 / 30;
165}
166disable_PIT2();
167return retval;
168}
169
170/*
171 * Original comment/code:
172 * "DFE: Measures the Max Performance Frequency in Hz (64-bit)"
173 *
174 * Measures the Actual Performance Frequency in Hz (64-bit)
175 * (just a naming change, mperf --> aperf )
176 */
177static uint64_t measure_aperf_frequency(void)
178{
179uint64_t aperfStart;
180uint64_t aperfEnd;
181uint64_t aperfDelta = 0xffffffffffffffffULL;
182unsigned long pollCount;
183uint64_t retval = 0;
184int i;
185
186/* Time how many APERF ticks elapse in 30 msec using the 8254 PIT
187 * counter 2. We run this loop 3 times to make sure the cache
188 * is hot and we take the minimum delta from all of the runs.
189 * That is to say that we're biased towards measuring the minimum
190 * number of APERF ticks that occur while waiting for the timer to
191 * expire.
192 */
193for(i = 0; i < 10; ++i)
194{
195enable_PIT2();
196set_PIT2_mode0(CALIBRATE_LATCH);
197aperfStart = rdmsr64(MSR_AMD_APERF);
198pollCount = poll_PIT2_gate();
199aperfEnd = rdmsr64(MSR_AMD_APERF);
200/* The poll loop must have run at least a few times for accuracy */
201if (pollCount <= 1)
202{
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)
210{
211continue;
212}
213// tscDelta = MIN(tscDelta, (tscEnd - tscStart))
214if ( (aperfEnd - aperfStart) < aperfDelta )
215{
216aperfDelta = aperfEnd - aperfStart;
217}
218}
219/* mperfDelta is now the least number of MPERF ticks the processor made in
220 * a timespan of 0.03 s (e.g. 30 milliseconds)
221 */
222
223if (aperfDelta > (1ULL<<32))
224{
225retval = 0;
226}
227else
228{
229retval = aperfDelta * 1000 / 30;
230}
231disable_PIT2();
232return retval;
233}
234
235/*
236 * Calculates the FSB and CPU frequencies using specific MSRs for each CPU
237 * - multi. is read from a specific MSR. In the case of Intel, there is:
238 * a max multi. (used to calculate the FSB freq.),
239 * and a current multi. (used to calculate the CPU freq.)
240 * - fsbFrequency = tscFrequency / multi
241 * - cpuFrequency = fsbFrequency * multi
242 */
243void scan_cpu(PlatformInfo_t *p)
244{
245uint64_ttscFrequency= 0;
246uint64_tfsbFrequency= 0;
247uint64_tcpuFrequency= 0;
248uint64_tmsr= 0;
249uint64_tflex_ratio= 0;
250
251uint32_tmax_ratio= 0;
252uint32_tmin_ratio= 0;
253uint32_treg[4]; //= {0, 0, 0, 0};
254uint32_tcores_per_package= 0;
255uint32_tlogical_per_package= 1;
256uint32_tthreads_per_core= 1;
257
258uint8_tbus_ratio_max= 0;
259uint8_tbus_ratio_min= 0;
260uint8_tcurrdiv= 0;
261uint8_tcurrcoef= 0;
262uint8_tmaxdiv= 0;
263uint8_tmaxcoef= 0;
264uint8_tpic0_mask;
265
266const char*newratio;
267charstr[128];
268char*s= 0;
269
270intlen= 0;
271intmyfsb= 0;
272inti= 0;
273
274/* get cpuid values */
275do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]); // MaxFn, Vendor
276p->CPU.Vendor = p->CPU.CPUID[CPUID_0][ebx];
277
278do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]); // Signature, stepping, features
279
280if ((p->CPU.Vendor == CPUID_VENDOR_INTEL) && ((bit(28) & p->CPU.CPUID[CPUID_1][edx]) != 0)) // Intel && HTT/Multicore
281{
282logical_per_package = bitfield(p->CPU.CPUID[CPUID_1][ebx], 23, 16);
283}
284
285do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]); // TLB/Cache/Prefetch
286
287do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]); // S/N
288
289/* Based on Apple's XNU cpuid.c - Deterministic cache parameters */
290if ((p->CPU.CPUID[CPUID_0][eax] > 3) && (p->CPU.CPUID[CPUID_0][eax] < 0x80000000))
291{
292for (i = 0; i < 0xFF; i++) // safe loop
293{
294do_cpuid2(0x00000004, i, reg); // AX=4: Fn, CX=i: cache index
295if (bitfield(reg[eax], 4, 0) == 0)
296{
297break;
298}
299//cores_per_package = bitfield(reg[eax], 31, 26) + 1;
300}
301}
302
303do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
304
305if (i > 0)
306{
307cores_per_package = bitfield(p->CPU.CPUID[CPUID_4][eax], 31, 26) + 1; // i = cache index
308threads_per_core = bitfield(p->CPU.CPUID[CPUID_4][eax], 25, 14) + 1;
309}
310
311if (cores_per_package == 0)
312{
313cores_per_package = 1;
314}
315
316if (p->CPU.CPUID[CPUID_0][0] >= 0x5)// Monitor/Mwait
317{
318do_cpuid(5, p->CPU.CPUID[CPUID_5]);
319}
320
321if (p->CPU.CPUID[CPUID_0][0] >= 6)// Thermal/Power
322{
323do_cpuid(6, p->CPU.CPUID[CPUID_6]);
324}
325
326do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
327
328if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8)
329{
330do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
331do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
332}
333else if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1)
334{
335do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
336}
337
338/* http://www.flounder.com/cpuid_explorer2.htm
339 EAX (Intel):
340 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
341 +--------+----------------+--------+----+----+--------+--------+--------+
342 |########|Extended family |Extmodel|####|type|familyid| model |stepping|
343 +--------+----------------+--------+----+----+--------+--------+--------+
344
345 EAX (AMD):
346 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
347 +--------+----------------+--------+----+----+--------+--------+--------+
348 |########|Extended family |Extmodel|####|####|familyid| model |stepping|
349 +--------+----------------+--------+----+----+--------+--------+--------+
350*/
351
352p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
353p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
354p->CPU.Stepping= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);// stepping = cpu_feat_eax & 0xF;
355p->CPU.Model= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);// model = (cpu_feat_eax >> 4) & 0xF;
356p->CPU.Family= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);// family = (cpu_feat_eax >> 8) & 0xF;
357//p->CPU.Type= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12);// type = (cpu_feat_eax >> 12) & 0x3;
358p->CPU.ExtModel= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);// ext_model = (cpu_feat_eax >> 16) & 0xF;
359p->CPU.ExtFamily= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);// ext_family = (cpu_feat_eax >> 20) & 0xFF;
360
361p->CPU.Model += (p->CPU.ExtModel << 4);
362
363/* get BrandString (if supported) */
364/* Copyright: from Apple's XNU cpuid.c */
365if (p->CPU.CPUID[CPUID_80][0] > 0x80000004)
366{
367bzero(str, 128);
368/*
369 * The BrandString 48 bytes (max), guaranteed to
370 * be NULL terminated.
371 */
372do_cpuid(0x80000002, reg);
373memcpy(&str[0], (char *)reg, 16);
374do_cpuid(0x80000003, reg);
375memcpy(&str[16], (char *)reg, 16);
376do_cpuid(0x80000004, reg);
377memcpy(&str[32], (char *)reg, 16);
378for (s = str; *s != '\0'; s++)
379{
380if (*s != ' ')
381{
382break;
383}
384}
385strlcpy(p->CPU.BrandString, s, 48);
386
387if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, MIN(sizeof(p->CPU.BrandString), (unsigned)strlen(CPU_STRING_UNKNOWN) + 1)))
388{
389/*
390 * This string means we have a firmware-programmable brand string,
391 * and the firmware couldn't figure out what sort of CPU we have.
392 */
393p->CPU.BrandString[0] = '\0';
394}
395p->CPU.BrandString[47] = '\0';
396//DBG("Brandstring = %s\n", p->CPU.BrandString);
397}
398
399/*
400 * Find the number of enabled cores and threads
401 * (which determines whether SMT/Hyperthreading is active).
402 */
403switch (p->CPU.Vendor)
404{
405case CPUID_VENDOR_INTEL:
406switch (p->CPU.Model)
407{
408case CPUID_MODEL_NEHALEM:
409case CPUID_MODEL_FIELDS:
410case CPUID_MODEL_DALES:
411case CPUID_MODEL_NEHALEM_EX:
412case CPUID_MODEL_JAKETOWN:
413case CPUID_MODEL_SANDYBRIDGE:
414case CPUID_MODEL_IVYBRIDGE:
415
416case CPUID_MODEL_HASWELL:
417case CPUID_MODEL_HASWELL_SVR:
418//case CPUID_MODEL_HASWELL_H:
419case CPUID_MODEL_HASWELL_ULT:
420case CPUID_MODEL_CRYSTALWELL:
421//case CPUID_MODEL_:
422msr = rdmsr64(MSR_CORE_THREAD_COUNT);
423p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 31, 16);
424p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
425break;
426
427case CPUID_MODEL_DALES_32NM:
428case CPUID_MODEL_WESTMERE:
429case CPUID_MODEL_WESTMERE_EX:
430msr = rdmsr64(MSR_CORE_THREAD_COUNT);
431p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 19, 16);
432p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
433break;
434}
435
436if (p->CPU.NoCores == 0)
437{
438p->CPU.NoCores= cores_per_package;
439p->CPU.NoThreads= logical_per_package;
440}
441break;
442
443case CPUID_VENDOR_AMD:
444p->CPU.NoCores= (uint32_t)bitfield(p->CPU.CPUID[CPUID_88][2], 7, 0) + 1;
445p->CPU.NoThreads= (uint32_t)bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
446if (p->CPU.NoCores == 0)
447{
448p->CPU.NoCores = 1;
449}
450
451if (p->CPU.NoThreads < p->CPU.NoCores)
452{
453p->CPU.NoThreads = p->CPU.NoCores;
454}
455
456break;
457
458default:
459stop("Unsupported CPU detected! System halted.");
460}
461
462//workaround for N270. I don't know why it detected wrong
463// MSR is *NOT* available on the Intel Atom CPU
464if ((p->CPU.Model == CPUID_MODEL_ATOM) && (strstr(p->CPU.BrandString, "270")))
465{
466p->CPU.NoCores= 1;
467p->CPU.NoThreads= 2;
468}
469
470/* setup features */
471if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0)
472{
473p->CPU.Features |= CPU_FEATURE_MMX;
474}
475
476if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0)
477{
478p->CPU.Features |= CPU_FEATURE_SSE;
479}
480
481if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0)
482{
483p->CPU.Features |= CPU_FEATURE_SSE2;
484}
485
486if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0)
487{
488p->CPU.Features |= CPU_FEATURE_SSE3;
489}
490
491if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0)
492{
493p->CPU.Features |= CPU_FEATURE_SSE41;
494}
495
496if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0)
497{
498p->CPU.Features |= CPU_FEATURE_SSE42;
499}
500
501if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0)
502{
503p->CPU.Features |= CPU_FEATURE_EM64T;
504}
505
506if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0)
507{
508p->CPU.Features |= CPU_FEATURE_MSR;
509}
510
511if ((p->CPU.Vendor == CPUID_VENDOR_INTEL) && (p->CPU.NoThreads > p->CPU.NoCores))
512{
513p->CPU.Features |= CPU_FEATURE_HTT;
514}
515
516pic0_mask = inb(0x21U);
517outb(0x21U, 0xFFU); // mask PIC0 interrupts for duration of timing tests
518
519tscFrequency = measure_tsc_frequency();
520DBG("cpu freq classic = 0x%016llx\n", tscFrequency);
521// if usual method failed
522if ( tscFrequency < 1000 )//TEST
523{
524tscFrequency = timeRDTSC() * 20;//measure_tsc_frequency();
525// DBG("cpu freq timeRDTSC = 0x%016llx\n", tscFrequency);
526}
527else
528{
529// DBG("cpu freq timeRDTSC = 0x%016llxn", timeRDTSC() * 20);
530}
531
532fsbFrequency = 0;
533cpuFrequency = 0;
534
535if (p->CPU.Vendor == CPUID_VENDOR_INTEL && ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)))
536{
537int intelCPU = p->CPU.Model;
538if (p->CPU.Family == 0x06)
539{
540/* Nehalem CPU model */
541switch (p->CPU.Model)
542{
543case CPUID_MODEL_NEHALEM:
544case CPUID_MODEL_FIELDS:
545case CPUID_MODEL_DALES:
546case CPUID_MODEL_DALES_32NM:
547case CPUID_MODEL_WESTMERE:
548case CPUID_MODEL_NEHALEM_EX:
549case CPUID_MODEL_WESTMERE_EX:
550/* --------------------------------------------------------- */
551case CPUID_MODEL_SANDYBRIDGE:
552case CPUID_MODEL_JAKETOWN:
553case CPUID_MODEL_IVYBRIDGE_XEON:
554case CPUID_MODEL_IVYBRIDGE:
555case CPUID_MODEL_HASWELL:
556case CPUID_MODEL_HASWELL_SVR:
557
558case CPUID_MODEL_HASWELL_ULT:
559case CPUID_MODEL_CRYSTALWELL:
560/* --------------------------------------------------------- */
561msr = rdmsr64(MSR_PLATFORM_INFO);
562DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
563bus_ratio_max = bitfield(msr, 15, 8);
564bus_ratio_min = bitfield(msr, 47, 40); //valv: not sure about this one (Remarq.1)
565msr = rdmsr64(MSR_FLEX_RATIO);
566DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
567if (bitfield(msr, 16, 16))
568{
569flex_ratio = bitfield(msr, 15, 8);
570// bcc9: at least on the gigabyte h67ma-ud2h,
571// where the cpu multipler can't be changed to
572// allow overclocking, the flex_ratio msr has unexpected (to OSX)
573// contents.These contents cause mach_kernel to
574// fail to compute the bus ratio correctly, instead
575// causing the system to crash since tscGranularity
576// is inadvertently set to 0.
577
578if (flex_ratio == 0)
579{
580// Clear bit 16 (evidently the presence bit)
581wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
582msr = rdmsr64(MSR_FLEX_RATIO);
583DBG("CPU: Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
584}
585else
586{
587if (bus_ratio_max > flex_ratio)
588{
589bus_ratio_max = flex_ratio;
590}
591}
592}
593
594if (bus_ratio_max)
595{
596fsbFrequency = (tscFrequency / bus_ratio_max);
597}
598
599//valv: Turbo Ratio Limit
600if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
601{
602msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
603
604cpuFrequency = bus_ratio_max * fsbFrequency;
605max_ratio = bus_ratio_max * 10;
606}
607else
608{
609cpuFrequency = tscFrequency;
610}
611if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4))
612{
613max_ratio = atoi(newratio);
614max_ratio = (max_ratio * 10);
615if (len >= 3)
616{
617max_ratio = (max_ratio + 5);
618}
619
620verbose("Bus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
621
622// extreme overclockers may love 320 ;)
623if ((max_ratio >= min_ratio) && (max_ratio <= 320))
624{
625cpuFrequency = (fsbFrequency * max_ratio) / 10;
626if (len >= 3)
627{
628maxdiv = 1;
629}
630else
631{
632maxdiv = 0;
633}
634}
635else
636{
637max_ratio = (bus_ratio_max * 10);
638}
639}
640//valv: to be uncommented if Remarq.1 didn't stick
641//if (bus_ratio_max > 0) bus_ratio = flex_ratio;
642p->CPU.MaxRatio = max_ratio;
643p->CPU.MinRatio = min_ratio;
644
645myfsb = fsbFrequency / 1000000;
646verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio/10); // Bungo: fixed wrong Bus-Ratio readout
647currcoef = bus_ratio_max;
648
649break;
650
651default:
652msr = rdmsr64(MSR_IA32_PERF_STATUS);
653DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
654currcoef = bitfield(msr, 12, 8); // Bungo: reverted to 2263 state because of wrong old CPUs freq. calculating
655// Non-integer bus ratio for the max-multi
656maxdiv = bitfield(msr, 46, 46);
657// Non-integer bus ratio for the current-multi (undocumented)
658currdiv = bitfield(msr, 14, 14);
659
660// This will always be model >= 3
661if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f))
662{
663/* On these models, maxcoef defines TSC freq */
664maxcoef = bitfield(msr, 44, 40);
665}
666else
667{
668// On lower models, currcoef defines TSC freq
669// XXX
670maxcoef = currcoef;
671}
672
673if (!currcoef)
674{
675currcoef = maxcoef;
676}
677
678if (maxcoef)
679{
680if (maxdiv)
681{
682fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));
683}
684else
685{
686fsbFrequency = (tscFrequency / maxcoef);
687}
688
689if (currdiv)
690{
691cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);
692}
693else
694{
695cpuFrequency = (fsbFrequency * currcoef);
696}
697
698DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
699}
700break;
701}
702}
703// Mobile CPU
704if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28))
705{
706p->CPU.Features |= CPU_FEATURE_MOBILE;
707}
708}
709else if ((p->CPU.Vendor == CPUID_VENDOR_AMD) && (p->CPU.Family == 0x0f))
710{
711switch(p->CPU.ExtFamily)
712{
713case 0x00: //* K8 *//
714msr = rdmsr64(K8_FIDVID_STATUS);
715maxcoef = bitfield(msr, 21, 16) / 2 + 4;
716currcoef = bitfield(msr, 5, 0) / 2 + 4;
717break;
718
719case 0x01: //* K10 *//
720msr = rdmsr64(K10_COFVID_STATUS);
721do_cpuid2(0x00000006, 0, p->CPU.CPUID[CPUID_6]);
722// EffFreq: effective frequency interface
723if (bitfield(p->CPU.CPUID[CPUID_6][2], 0, 0) == 1)
724{
725//uint64_t mperf = measure_mperf_frequency();
726uint64_t aperf = measure_aperf_frequency();
727cpuFrequency = aperf;
728}
729// NOTE: tsc runs at the maccoeff (non turbo)
730//*not* at the turbo frequency.
731maxcoef = bitfield(msr, 54, 49) / 2 + 4;
732currcoef = bitfield(msr, 5, 0) + 0x10;
733currdiv = 2 << bitfield(msr, 8, 6);
734
735break;
736
737case 0x05: //* K14 *//
738msr = rdmsr64(K10_COFVID_STATUS);
739currcoef = (bitfield(msr, 54, 49) + 0x10) << 2;
740currdiv = (bitfield(msr, 8, 4) + 1) << 2;
741currdiv += bitfield(msr, 3, 0);
742
743break;
744
745case 0x02: //* K11 *//
746// not implimented
747break;
748}
749
750if (maxcoef)
751{
752if (currdiv)
753{
754if (!currcoef)
755{
756currcoef = maxcoef;
757}
758
759if (!cpuFrequency)
760{
761fsbFrequency = ((tscFrequency * currdiv) / currcoef);
762}
763else
764{
765fsbFrequency = ((cpuFrequency * currdiv) / currcoef);
766}
767DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
768}
769else
770{
771if (!cpuFrequency)
772{
773fsbFrequency = (tscFrequency / maxcoef);
774}
775else
776{
777fsbFrequency = (cpuFrequency / maxcoef);
778}
779DBG("%d\n", currcoef);
780}
781}
782else if (currcoef)
783{
784if (currdiv)
785{
786fsbFrequency = ((tscFrequency * currdiv) / currcoef);
787DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
788}
789else
790{
791fsbFrequency = (tscFrequency / currcoef);
792DBG("%d\n", currcoef);
793}
794}
795if (!cpuFrequency)
796{
797cpuFrequency = tscFrequency;
798}
799}
800
801#if 0
802if (!fsbFrequency)
803{
804fsbFrequency = (DEFAULT_FSB * 1000);
805DBG("CPU: fsbFrequency = 0! using the default value for FSB!\n");
806cpuFrequency = tscFrequency;
807}
808
809DBG("cpu freq = 0x%016llxn", timeRDTSC() * 20);
810
811#endif
812
813outb(0x21U, pic0_mask); // restore PIC0 interrupts
814
815p->CPU.MaxCoef = maxcoef;
816p->CPU.MaxDiv = maxdiv;
817p->CPU.CurrCoef = currcoef;
818p->CPU.CurrDiv = currdiv;
819p->CPU.TSCFrequency = tscFrequency;
820p->CPU.FSBFrequency = fsbFrequency;
821p->CPU.CPUFrequency = cpuFrequency;
822
823// keep formatted with spaces instead of tabs
824DBG("\n------------------------------\n");
825 DBG("\tCPU INFO\n");
826DBG("------------------------------\n");
827
828DBG("CPUID Raw Values:\n");
829for (i = 0; i < CPUID_MAX; i++)
830{
831DBG("%02d: %08X-%08X-%08X-%08X\n", i, p->CPU.CPUID[i][eax], p->CPU.CPUID[i][ebx], p->CPU.CPUID[i][ecx], p->CPU.CPUID[i][edx]);
832}
833DBG("\n");
834DBG("Brand String: %s\n",p->CPU.BrandString);// Processor name (BIOS)
835DBG("Vendor: 0x%X\n",p->CPU.Vendor);// Vendor ex: GenuineIntel
836DBG("Family: 0x%X\n",p->CPU.Family);// Family ex: 6 (06h)
837DBG("ExtFamily: 0x%X\n",p->CPU.ExtFamily);
838DBG("Signature: 0x%08X\n",p->CPU.Signature);// CPUID signature
839/*switch (p->CPU.Type) {
840case PT_OEM:
841DBG("Processor type: Intel Original OEM Processor\n");
842break;
843case PT_OD:
844DBG("Processor type: Intel Over Drive Processor\n");
845break;
846case PT_DUAL:
847DBG("Processor type: Intel Dual Processor\n");
848break;
849case PT_RES:
850DBG("Processor type: Intel Reserved\n");
851break;
852default:
853break;
854}*/
855DBG("Model: 0x%X\n",p->CPU.Model);// Model ex: 37 (025h)
856DBG("ExtModel: 0x%X\n",p->CPU.ExtModel);
857DBG("Stepping: 0x%X\n",p->CPU.Stepping);// Stepping ex: 5 (05h)
858DBG("MaxCoef: %d\n",p->CPU.MaxCoef);
859DBG("CurrCoef: %d\n",p->CPU.CurrCoef);
860DBG("MaxDiv: %d\n",p->CPU.MaxDiv);
861DBG("CurrDiv: %d\n",p->CPU.CurrDiv);
862DBG("TSCFreq: %dMHz\n",p->CPU.TSCFrequency / 1000000);
863DBG("FSBFreq: %dMHz\n",p->CPU.FSBFrequency / 1000000);
864DBG("CPUFreq: %dMHz\n",p->CPU.CPUFrequency / 1000000);
865DBG("Cores: %d\n",p->CPU.NoCores);// Cores
866DBG("Logical processor: %d\n",p->CPU.NoThreads);// Logical procesor
867DBG("Features: 0x%08x\n",p->CPU.Features);
868
869DBG("\n---------------------------------------------\n");
870#if DEBUG_CPU
871pause();
872#endif
873}
874

Archive Download this file

Revision: 2632