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...)
20#endif
21
22
23#define UI_CPUFREQ_ROUNDING_FACTOR10000000
24
25clock_frequency_info_t gPEClockFrequencyInfo;
26
27static inline uint32_t __unused clockspeed_rdtsc(void)
28{
29uint32_t out;
30__asm__ volatile (
31 "rdtsc\n"
32 "shl $32,%%edx\n"
33 "or %%edx,%%eax\n"
34 : "=a" (out)
35 :
36 : "%edx"
37 );
38return out;
39}
40
41/*
42 * timeRDTSC()
43 * This routine sets up PIT counter 2 to count down 1/20 of a second.
44 * It pauses until the value is latched in the counter
45 * and then reads the time stamp counter to return to the caller.
46 */
47static uint64_t timeRDTSC(void)
48{
49intattempts = 0;
50uint64_t latchTime;
51uint64_tsaveTime,intermediate;
52unsigned inttimerValue, lastValue;
53//boolean_tint_enabled;
54/*
55 * Table of correction factors to account for
56 * - timer counter quantization errors, and
57 * - undercounts 0..5
58 */
59#define SAMPLE_CLKS_EXACT(((double) CLKNUM) / 20.0)
60#define SAMPLE_CLKS_INT((int) CLKNUM / 20)
61#define SAMPLE_NSECS(2000000000LL)
62#define SAMPLE_MULTIPLIER(((double)SAMPLE_NSECS)*SAMPLE_CLKS_EXACT)
63#define ROUND64(x)((uint64_t)((x) + 0.5))
64uint64_tscale[6] = {
65ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-0)),
66ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-1)),
67ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-2)),
68ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-3)),
69ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-4)),
70ROUND64(SAMPLE_MULTIPLIER/(double)(SAMPLE_CLKS_INT-5))
71};
72
73//int_enabled = ml_set_interrupts_enabled(false);
74
75restart:
76if (attempts >= 3) // increase to up to 9 attempts.
77{
78// This will flash-reboot. TODO: Use tscPanic instead.
79//printf("Timestamp counter calibation failed with %d attempts\n", attempts);
80}
81attempts++;
82enable_PIT2();// turn on PIT2
83set_PIT2(0);// reset timer 2 to be zero
84latchTime = rdtsc64();// get the time stamp to time
85latchTime = get_PIT2(&timerValue) - latchTime; // time how long this takes
86set_PIT2(SAMPLE_CLKS_INT);// set up the timer for (almost) 1/20th a second
87saveTime = rdtsc64();// now time how long a 20th a second is...
88get_PIT2(&lastValue);
89get_PIT2(&lastValue);// read twice, first value may be unreliable
90do {
91intermediate = get_PIT2(&timerValue);
92if (timerValue > lastValue)
93{
94// Timer wrapped
95set_PIT2(0);
96disable_PIT2();
97goto restart;
98}
99lastValue = timerValue;
100} while (timerValue > 5);
101//printf("timerValue %d\n",timerValue);
102//printf("intermediate 0x%016llX\n",intermediate);
103//printf("saveTime 0x%016llX\n",saveTime);
104
105intermediate -= saveTime;// raw count for about 1/20 second
106intermediate *= scale[timerValue];// rescale measured time spent
107intermediate /= SAMPLE_NSECS;// so its exactly 1/20 a second
108intermediate += latchTime;// add on our save fudge
109
110set_PIT2(0);// reset timer 2 to be zero
111disable_PIT2();// turn off PIT 2
112
113//ml_set_interrupts_enabled(int_enabled);
114return intermediate;
115}
116
117/*
118 * DFE: Measures the TSC frequency in Hz (64-bit) using the ACPI PM timer
119 */
120static uint64_t __unused measure_tsc_frequency(void)
121{
122uint64_t tscStart;
123uint64_t tscEnd;
124uint64_t tscDelta = 0xffffffffffffffffULL;
125unsigned long pollCount;
126uint64_t retval = 0;
127int i;
128
129/* Time how many TSC ticks elapse in 30 msec using the 8254 PIT
130 * counter 2. We run this loop 3 times to make sure the cache
131 * is hot and we take the minimum delta from all of the runs.
132 * That is to say that we're biased towards measuring the minimum
133 * number of TSC ticks that occur while waiting for the timer to
134 * expire. That theoretically helps avoid inconsistencies when
135 * running under a VM if the TSC is not virtualized and the host
136 * steals time. The TSC is normally virtualized for VMware.
137 */
138for(i = 0; i < 10; ++i)
139{
140enable_PIT2();
141set_PIT2_mode0(CALIBRATE_LATCH);
142tscStart = rdtsc64();
143pollCount = poll_PIT2_gate();
144tscEnd = rdtsc64();
145/* The poll loop must have run at least a few times for accuracy */
146if (pollCount <= 1)
147{
148continue;
149}
150/* The TSC must increment at LEAST once every millisecond.
151 * We should have waited exactly 30 msec so the TSC delta should
152 * be >= 30. Anything less and the processor is way too slow.
153 */
154if ((tscEnd - tscStart) <= CALIBRATE_TIME_MSEC)
155{
156continue;
157}
158// tscDelta = MIN(tscDelta, (tscEnd - tscStart))
159if ( (tscEnd - tscStart) < tscDelta )
160{
161tscDelta = tscEnd - tscStart;
162}
163}
164/* tscDelta is now the least number of TSC ticks the processor made in
165 * a timespan of 0.03 s (e.g. 30 milliseconds)
166 * Linux thus divides by 30 which gives the answer in kiloHertz because
167 * 1 / ms = kHz. But we're xnu and most of the rest of the code uses
168 * Hz so we need to convert our milliseconds to seconds. Since we're
169 * dividing by the milliseconds, we simply multiply by 1000.
170 */
171
172/* Unlike linux, we're not limited to 32-bit, but we do need to take care
173 * that we're going to multiply by 1000 first so we do need at least some
174 * arithmetic headroom. For now, 32-bit should be enough.
175 * Also unlike Linux, our compiler can do 64-bit integer arithmetic.
176 */
177if (tscDelta > (1ULL<<32))
178{
179retval = 0;
180}
181else
182{
183retval = tscDelta * 1000 / 30;
184}
185disable_PIT2();
186return retval;
187}
188
189static uint64_trtc_set_cyc_per_sec(uint64_t cycles);
190#define RTC_FAST_DENOM0xFFFFFFFF
191
192inline static uint32_t
193create_mul_quant_GHZ(int shift, uint32_t quant)
194{
195return (uint32_t)((((uint64_t)NSEC_PER_SEC/20) << shift) / quant);
196}
197
198struct{
199mach_timespec_tcalend_offset;
200boolean_tcalend_is_set;
201
202int64_tcalend_adjtotal;
203int32_tcalend_adjdelta;
204
205uint32_tboottime;
206
207mach_timebase_info_data_ttimebase_const;
208
209decl_simple_lock_data(,lock)/* real-time clock device lock */
210} rtclock;
211
212uint32_trtc_quant_shift;/* clock to nanos right shift */
213uint32_trtc_quant_scale;/* clock to nanos multiplier */
214uint64_trtc_cyc_per_sec;/* processor cycles per sec */
215uint64_trtc_cycle_count;/* clocks in 1/20th second */
216//uint64_t cpuFreq;
217
218static uint64_t rtc_set_cyc_per_sec(uint64_t cycles)
219{
220
221if (cycles > (NSEC_PER_SEC/20))
222{
223// we can use just a "fast" multiply to get nanos
224rtc_quant_shift = 32;
225rtc_quant_scale = create_mul_quant_GHZ(rtc_quant_shift, (uint32_t)cycles);
226rtclock.timebase_const.numer = rtc_quant_scale; // timeRDTSC is 1/20
227rtclock.timebase_const.denom = (uint32_t)RTC_FAST_DENOM;
228}
229else
230{
231rtc_quant_shift = 26;
232rtc_quant_scale = create_mul_quant_GHZ(rtc_quant_shift, (uint32_t)cycles);
233rtclock.timebase_const.numer = NSEC_PER_SEC/20; // timeRDTSC is 1/20
234rtclock.timebase_const.denom = (uint32_t)cycles;
235}
236rtc_cyc_per_sec = cycles*20;// multiply it by 20 and we are done..
237// BUT we also want to calculate...
238
239cycles = ((rtc_cyc_per_sec + (UI_CPUFREQ_ROUNDING_FACTOR/2))
240 / UI_CPUFREQ_ROUNDING_FACTOR)
241* UI_CPUFREQ_ROUNDING_FACTOR;
242
243/*
244 * Set current measured speed.
245 */
246if (cycles >= 0x100000000ULL)
247{
248gPEClockFrequencyInfo.cpu_clock_rate_hz = 0xFFFFFFFFUL;
249}
250else
251{
252gPEClockFrequencyInfo.cpu_clock_rate_hz = (unsigned long)cycles;
253}
254gPEClockFrequencyInfo.cpu_frequency_hz = cycles;
255
256//printf("[RTCLOCK_1] frequency %llu (%llu) %llu\n", cycles, rtc_cyc_per_sec,timeRDTSC() * 20);
257return(rtc_cyc_per_sec);
258}
259
260/*
261 * Calculates the FSB and CPU frequencies using specific MSRs for each CPU
262 * - multi. is read from a specific MSR. In the case of Intel, there is:
263 * a max multi. (used to calculate the FSB freq.),
264 * and a current multi. (used to calculate the CPU freq.)
265 * - busFrequency = tscFrequency / multi
266 * - cpuFrequency = busFrequency * multi
267 */
268
269/* Decimal powers: */
270#define kilo (1000ULL)
271#define Mega (kilo * kilo)
272#define Giga (kilo * Mega)
273#define Tera (kilo * Giga)
274#define Peta (kilo * Tera)
275
276#define quad(hi,lo)(((uint64_t)(hi)) << 32 | (lo))
277
278void scan_cpu(PlatformInfo_t *p)
279{
280//scan();
281uint64_tbusFCvtt2n;
282uint64_ttscFCvtt2n;
283uint64_ttscFreq= 0;
284uint64_tbusFrequency= 0;
285uint64_tcpuFrequency= 0;
286uint64_tmsr= 0;
287uint64_tflex_ratio= 0;
288uint64_tcpuid_features;
289
290uint32_tmax_ratio= 0;
291uint32_tmin_ratio= 0;
292uint32_treg[4];
293uint32_tcores_per_package= 0;
294uint32_tlogical_per_package= 1;
295uint32_tthreads_per_core= 1;
296
297uint8_tbus_ratio_max= 0;
298uint8_tbus_ratio_min= 0;
299uint8_tcurrdiv= 0;
300uint8_tcurrcoef= 0;
301uint8_tmaxdiv= 0;
302uint8_tmaxcoef= 0;
303uint8_tcpuMultN2= 0;
304
305const char*newratio;
306charstr[128];
307char*s= 0;
308
309intlen= 0;
310intmyfsb= 0;
311inti= 0;
312
313
314/* http://www.flounder.com/cpuid_explorer2.htm
315 EAX (Intel):
316 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
317 +--------+----------------+--------+----+----+--------+--------+--------+
318 |########|Extended family |Extmodel|####|type|familyid| model |stepping|
319 +--------+----------------+--------+----+----+--------+--------+--------+
320
321 EAX (AMD):
322 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
323 +--------+----------------+--------+----+----+--------+--------+--------+
324 |########|Extended family |Extmodel|####|####|familyid| model |stepping|
325 +--------+----------------+--------+----+----+--------+--------+--------+
326*/
327///////////////////-- MaxFn,Vendor --////////////////////////
328do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]); // MaxFn, Vendor
329p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
330/////////////////////////////////////////////////////////////
331
332///////////////////-- Signature, stepping, features -- //////
333do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]); // Signature, stepping, features
334cpuid_features = quad(p->CPU.CPUID[CPUID_1][ecx],p->CPU.CPUID[CPUID_1][edx]);
335if (bit(28) & p->CPU.CPUID[CPUID_1][edx]) // HTT/Multicore
336{
337logical_per_package = bitfield(p->CPU.CPUID[CPUID_1][ebx], 23, 16);
338}
339else
340{
341logical_per_package = 1;
342}
343//printf("logical %d\n",logical_per_package);
344
345p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
346p->CPU.Stepping= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);// stepping = cpu_feat_eax & 0xF;
347p->CPU.Model= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);// model = (cpu_feat_eax >> 4) & 0xF;
348p->CPU.Family= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);// family = (cpu_feat_eax >> 8) & 0xF;
349//p->CPU.Type= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12);// type = (cpu_feat_eax >> 12) & 0x3;
350p->CPU.ExtModel= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);// ext_model = (cpu_feat_eax >> 16) & 0xF;
351p->CPU.ExtFamily= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);// ext_family = (cpu_feat_eax >> 20) & 0xFF;
352
353if (p->CPU.Family == 0x0f)
354{
355p->CPU.Family += p->CPU.ExtFamily;
356}
357
358if (p->CPU.Family == 0x0f || p->CPU.Family == 0x06)
359{
360p->CPU.Model += (p->CPU.ExtModel << 4);
361}
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//char *vendor = p->CPU.cpuid_vendor;
400switch (p->CPU.Vendor){
401case CPUID_VENDOR_INTEL:
402{
403
404 do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]); // TLB/Cache/Prefetch
405
406 do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]); // S/N
407
408 /* Based on Apple's XNU cpuid.c - Deterministic cache parameters */
409 if ((p->CPU.CPUID[CPUID_0][eax] > 3) && (p->CPU.CPUID[CPUID_0][eax] < 0x80000000))
410 {
411 for (i = 0; i < 0xFF; i++) // safe loop
412 {
413 do_cpuid2(0x00000004, i, reg); // AX=4: Fn, CX=i: cache index
414 if (bitfield(reg[eax], 4, 0) == 0)
415 {
416 break;
417 }
418 cores_per_package = bitfield(reg[eax], 31, 26) + 1;
419 }
420 }
421
422 do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
423
424 if (i > 0)
425 {
426 cores_per_package = bitfield(p->CPU.CPUID[CPUID_4][eax], 31, 26) + 1; // i = cache index
427 threads_per_core = bitfield(p->CPU.CPUID[CPUID_4][eax], 25, 14) + 1;
428 }
429
430 if (cores_per_package == 0)
431 {
432 cores_per_package = 1;
433 }
434
435 if (p->CPU.CPUID[CPUID_0][0] >= 0x5)// Monitor/Mwait
436 {
437 do_cpuid(5, p->CPU.CPUID[CPUID_5]);
438 }
439
440 if (p->CPU.CPUID[CPUID_0][0] >= 6)// Thermal/Power
441 {
442 do_cpuid(6, p->CPU.CPUID[CPUID_6]);
443 }
444
445 do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
446
447 if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8)
448 {
449 do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
450 do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
451 }
452 else if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1)
453 {
454 do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
455 }
456
457switch (p->CPU.Model)
458{
459case CPUID_MODEL_NEHALEM:
460case CPUID_MODEL_FIELDS:
461case CPUID_MODEL_DALES:
462case CPUID_MODEL_NEHALEM_EX:
463case CPUID_MODEL_JAKETOWN:
464case CPUID_MODEL_SANDYBRIDGE:
465case CPUID_MODEL_IVYBRIDGE:
466
467case CPUID_MODEL_HASWELL:
468case CPUID_MODEL_HASWELL_SVR:
469//case CPUID_MODEL_HASWELL_H:
470case CPUID_MODEL_HASWELL_ULT:
471case CPUID_MODEL_CRYSTALWELL:
472//case CPUID_MODEL_:
473msr = rdmsr64(MSR_CORE_THREAD_COUNT);
474p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 31, 16);
475p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
476break;
477
478case CPUID_MODEL_DALES_32NM:
479case CPUID_MODEL_WESTMERE:
480case CPUID_MODEL_WESTMERE_EX:
481msr = rdmsr64(MSR_CORE_THREAD_COUNT);
482p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 19, 16);
483p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
484break;
485}
486
487if (p->CPU.NoCores == 0)
488{
489p->CPU.NoCores= cores_per_package;
490p->CPU.NoThreads= logical_per_package;
491}
492
493// MSR is *NOT* available on the Intel Atom CPU
494//workaround for N270. I don't know why it detected wrong
495if ((p->CPU.Model == CPUID_MODEL_ATOM) && (strstr(p->CPU.BrandString, "270")))
496{
497p->CPU.NoCores= 1;
498p->CPU.NoThreads= 2;
499}
500
501//workaround for Quad
502if ( strstr(p->CPU.BrandString, "Quad") )
503{
504p->CPU.NoCores= 4;
505p->CPU.NoThreads= 4;
506}
507}
508
509break;
510
511case CPUID_VENDOR_AMD:
512{
513do_cpuid(5, p->CPU.CPUID[CPUID_5]); // Monitor/Mwait
514
515do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
516if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8)
517{
518do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
519}
520
521if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1)
522{
523do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
524}
525
526do_cpuid(0x80000005, p->CPU.CPUID[CPUID_85]); // TLB/Cache/Prefetch
527do_cpuid(0x80000006, p->CPU.CPUID[CPUID_86]); // TLB/Cache/Prefetch
528do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
529
530cores_per_package = bitfield(p->CPU.CPUID[CPUID_88][ecx], 7, 0) + 1;
531threads_per_core = cores_per_package;
532
533if (cores_per_package == 0)
534{
535cores_per_package = 1;
536}
537
538p->CPU.NoCores= cores_per_package;
539p->CPU.NoThreads= logical_per_package;
540
541if (p->CPU.NoCores == 0)
542{
543p->CPU.NoCores = 1;
544p->CPU.NoThreads= 1;
545}
546}
547break;
548default :
549stop("Unsupported CPU detected! System halted.");
550}
551
552/* setup features */
553if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0)
554{
555p->CPU.Features |= CPU_FEATURE_MMX;
556}
557
558if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0)
559{
560p->CPU.Features |= CPU_FEATURE_SSE;
561}
562
563if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0)
564{
565p->CPU.Features |= CPU_FEATURE_SSE2;
566}
567
568if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0)
569{
570p->CPU.Features |= CPU_FEATURE_SSE3;
571}
572
573if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0)
574{
575p->CPU.Features |= CPU_FEATURE_SSE41;
576}
577
578if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0)
579{
580p->CPU.Features |= CPU_FEATURE_SSE42;
581}
582
583if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0)
584{
585p->CPU.Features |= CPU_FEATURE_EM64T;
586}
587
588if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0)
589{
590p->CPU.Features |= CPU_FEATURE_MSR;
591}
592
593if ((p->CPU.NoThreads > p->CPU.NoCores))
594{
595p->CPU.Features |= CPU_FEATURE_HTT;
596}
597
598uint64_t cycles;
599cycles = timeRDTSC();
600tscFreq = rtc_set_cyc_per_sec(cycles);
601DBG("cpu freq classic = 0x%016llx\n", tscFreq);
602// if usual method failed
603if ( tscFreq < 1000 )//TEST
604{
605tscFreq = measure_tsc_frequency();//timeRDTSC() * 20;//measure_tsc_frequency();
606// DBG("cpu freq timeRDTSC = 0x%016llx\n", tscFrequency);
607}
608
609if (p->CPU.Vendor==CPUID_VENDOR_INTEL && ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)))
610{
611int intelCPU = p->CPU.Model;
612if (p->CPU.Family == 0x06)
613{
614/* Nehalem CPU model */
615switch (p->CPU.Model)
616{
617case CPUID_MODEL_NEHALEM:
618case CPUID_MODEL_FIELDS:
619case CPUID_MODEL_DALES:
620case CPUID_MODEL_DALES_32NM:
621case CPUID_MODEL_WESTMERE:
622case CPUID_MODEL_NEHALEM_EX:
623case CPUID_MODEL_WESTMERE_EX:
624/* --------------------------------------------------------- */
625case CPUID_MODEL_SANDYBRIDGE:
626case CPUID_MODEL_JAKETOWN:
627case CPUID_MODEL_IVYBRIDGE_XEON:
628case CPUID_MODEL_IVYBRIDGE:
629case CPUID_MODEL_HASWELL:
630case CPUID_MODEL_HASWELL_SVR:
631
632case CPUID_MODEL_HASWELL_ULT:
633case CPUID_MODEL_CRYSTALWELL:
634/* --------------------------------------------------------- */
635msr = rdmsr64(MSR_PLATFORM_INFO);
636DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
637bus_ratio_max = bitfield(msr, 15, 8);
638bus_ratio_min = bitfield(msr, 47, 40); //valv: not sure about this one (Remarq.1)
639msr = rdmsr64(MSR_FLEX_RATIO);
640DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
641if (bitfield(msr, 16, 16))
642{
643flex_ratio = bitfield(msr, 15, 8);
644// bcc9: at least on the gigabyte h67ma-ud2h,
645// where the cpu multipler can't be changed to
646// allow overclocking, the flex_ratio msr has unexpected (to OSX)
647// contents.These contents cause mach_kernel to
648// fail to compute the bus ratio correctly, instead
649// causing the system to crash since tscGranularity
650// is inadvertently set to 0.
651
652if (flex_ratio == 0)
653{
654// Clear bit 16 (evidently the presence bit)
655wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
656msr = rdmsr64(MSR_FLEX_RATIO);
657DBG("CPU: Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
658}
659else
660{
661if (bus_ratio_max > flex_ratio)
662{
663bus_ratio_max = flex_ratio;
664}
665}
666}
667
668if (bus_ratio_max)
669{
670busFrequency = (tscFreq / bus_ratio_max);
671}
672
673//valv: Turbo Ratio Limit
674if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
675{
676msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
677
678cpuFrequency = bus_ratio_max * busFrequency;
679max_ratio = bus_ratio_max * 10;
680}
681else
682{
683cpuFrequency = tscFreq;
684}
685if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4))
686{
687max_ratio = atoi(newratio);
688max_ratio = (max_ratio * 10);
689if (len >= 3)
690{
691max_ratio = (max_ratio + 5);
692}
693
694verbose("Bus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
695
696// extreme overclockers may love 320 ;)
697if ((max_ratio >= min_ratio) && (max_ratio <= 320))
698{
699cpuFrequency = (busFrequency * max_ratio) / 10;
700if (len >= 3)
701{
702maxdiv = 1;
703}
704else
705{
706maxdiv = 0;
707}
708}
709else
710{
711max_ratio = (bus_ratio_max * 10);
712}
713}
714//valv: to be uncommented if Remarq.1 didn't stick
715//if (bus_ratio_max > 0) bus_ratio = flex_ratio;
716p->CPU.MaxRatio = max_ratio;
717p->CPU.MinRatio = min_ratio;
718
719myfsb = busFrequency / 1000000;
720verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio/10); // Bungo: fixed wrong Bus-Ratio readout
721currcoef = bus_ratio_max;
722
723break;
724
725default:
726msr = rdmsr64(MSR_IA32_PERF_STATUS);
727DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
728currcoef = bitfield(msr, 12, 8); // Bungo: reverted to 2263 state because of wrong old CPUs freq. calculating
729// Non-integer bus ratio for the max-multi
730maxdiv = bitfield(msr, 46, 46);
731// Non-integer bus ratio for the current-multi (undocumented)
732currdiv = bitfield(msr, 14, 14);
733
734// This will always be model >= 3
735if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f))
736{
737/* On these models, maxcoef defines TSC freq */
738maxcoef = bitfield(msr, 44, 40);
739}
740else
741{
742// On lower models, currcoef defines TSC freq
743// XXX
744maxcoef = currcoef;
745}
746
747if (!currcoef)
748{
749currcoef = maxcoef;
750}
751
752if (maxcoef)
753{
754if (maxdiv)
755{
756busFrequency = ((tscFreq * 2) / ((maxcoef * 2) + 1));
757}
758else
759{
760busFrequency = (tscFreq / maxcoef);
761}
762
763if (currdiv)
764{
765cpuFrequency = (busFrequency * ((currcoef * 2) + 1) / 2);
766}
767else
768{
769cpuFrequency = (busFrequency * currcoef);
770}
771
772DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
773}
774break;
775}
776}
777// Mobile CPU
778if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28))
779{
780p->CPU.Features |= CPU_FEATURE_MOBILE;
781}
782}
783
784else if (p->CPU.Vendor==CPUID_VENDOR_AMD)
785{
786switch(p->CPU.Family)
787{
788case 0xF: /* K8 */
789{
790uint64_t fidvid = 0;
791uint64_t cpuMult;
792uint64_t fid;
793
794fidvid = rdmsr64(K8_FIDVID_STATUS);
795fid = bitfield(fidvid, 5, 0);
796
797cpuMult = (fid + 8) / 2;
798currcoef = cpuMult;
799
800cpuMultN2 = (fidvid & (uint64_t)bit(0));
801currdiv = cpuMultN2;
802/****** Addon END ******/
803}
804break;
805
806case 0x10: /*** AMD Family 10h ***/
807{
808uint64_t cofvid = 0;
809uint64_t cpuMult;
810uint64_t divisor = 0;
811uint64_t did;
812uint64_t fid;
813
814cofvid = rdmsr64(K10_COFVID_STATUS);
815did = bitfield(cofvid, 8, 6);
816fid = bitfield(cofvid, 5, 0);
817if (did == 0) divisor = 2;
818else if (did == 1) divisor = 4;
819else if (did == 2) divisor = 8;
820else if (did == 3) divisor = 16;
821else if (did == 4) divisor = 32;
822
823cpuMult = (fid + 16) / divisor;
824currcoef = cpuMult;
825
826cpuMultN2 = (cofvid & (uint64_t)bit(0));
827currdiv = cpuMultN2;
828
829/****** Addon END ******/
830}
831break;
832
833case 0x11: /*** AMD Family 11h ***/
834{
835uint64_t cofvid = 0;
836uint64_t cpuMult;
837uint64_t divisor = 0;
838uint64_t did;
839uint64_t fid;
840
841cofvid = rdmsr64(K10_COFVID_STATUS);
842did = bitfield(cofvid, 8, 6);
843fid = bitfield(cofvid, 5, 0);
844if (did == 0) divisor = 2;
845else if (did == 1) divisor = 4;
846else if (did == 2) divisor = 8;
847else if (did == 3) divisor = 16;
848else if (did == 4) divisor = 32;
849
850cpuMult = (fid + 8) / divisor;
851currcoef = cpuMult;
852
853cpuMultN2 = (cofvid & (uint64_t)bit(0));
854currdiv = cpuMultN2;
855
856/****** Addon END ******/
857}
858 break;
859
860case 0x12: /*** AMD Family 12h ***/
861{
862// 8:4 CpuFid: current CPU core frequency ID
863// 3:0 CpuDid: current CPU core divisor ID
864uint64_t prfsts,CpuFid,CpuDid;
865prfsts = rdmsr64(K10_COFVID_STATUS);
866
867CpuDid = bitfield(prfsts, 3, 0) ;
868CpuFid = bitfield(prfsts, 8, 4) ;
869uint64_t divisor;
870switch (CpuDid)
871{
872case 0: divisor = 1; break;
873case 1: divisor = (3/2); break;
874case 2: divisor = 2; break;
875case 3: divisor = 3; break;
876case 4: divisor = 4; break;
877case 5: divisor = 6; break;
878case 6: divisor = 8; break;
879case 7: divisor = 12; break;
880case 8: divisor = 16; break;
881default: divisor = 1; break;
882}
883currcoef = (CpuFid + 0x10) / divisor;
884
885cpuMultN2 = (prfsts & (uint64_t)bit(0));
886currdiv = cpuMultN2;
887
888}
889break;
890
891case 0x14: /* K14 */
892
893{
894// 8:4: current CPU core divisor ID most significant digit
895// 3:0: current CPU core divisor ID least significant digit
896uint64_t prfsts;
897prfsts = rdmsr64(K10_COFVID_STATUS);
898
899uint64_t CpuDidMSD,CpuDidLSD;
900CpuDidMSD = bitfield(prfsts, 8, 4) ;
901CpuDidLSD = bitfield(prfsts, 3, 0) ;
902
903uint64_t frequencyId = 0x10;
904currcoef = (frequencyId + 0x10) /
905(CpuDidMSD + (CpuDidLSD * 0.25) + 1);
906currdiv = ((CpuDidMSD) + 1) << 2;
907currdiv += bitfield(msr, 3, 0);
908
909cpuMultN2 = (prfsts & (uint64_t)bit(0));
910currdiv = cpuMultN2;
911}
912
913break;
914
915case 0x15: /*** AMD Family 15h ***/
916case 0x06: /*** AMD Family 06h ***/
917{
918
919uint64_t cofvid = 0;
920uint64_t cpuMult;
921uint64_t divisor = 0;
922uint64_t did;
923uint64_t fid;
924
925cofvid = rdmsr64(K10_COFVID_STATUS);
926did = bitfield(cofvid, 8, 6);
927fid = bitfield(cofvid, 5, 0);
928if (did == 0) divisor = 2;
929else if (did == 1) divisor = 4;
930else if (did == 2) divisor = 8;
931else if (did == 3) divisor = 16;
932else if (did == 4) divisor = 32;
933
934cpuMult = (fid + 16) / divisor;
935currcoef = cpuMult;
936
937cpuMultN2 = (cofvid & (uint64_t)bit(0));
938currdiv = cpuMultN2;
939}
940break;
941
942case 0x16: /*** AMD Family 16h kabini ***/
943{
944uint64_t cofvid = 0;
945uint64_t cpuMult;
946uint64_t divisor = 0;
947uint64_t did;
948uint64_t fid;
949
950cofvid = rdmsr64(K10_COFVID_STATUS);
951did = bitfield(cofvid, 8, 6);
952fid = bitfield(cofvid, 5, 0);
953if (did == 0) divisor = 1;
954else if (did == 1) divisor = 2;
955else if (did == 2) divisor = 4;
956else if (did == 3) divisor = 8;
957else if (did == 4) divisor = 16;
958
959cpuMult = (fid + 16) / divisor;
960currcoef = cpuMult;
961
962cpuMultN2 = (cofvid & (uint64_t)bit(0));
963currdiv = cpuMultN2;
964/****** Addon END ******/
965}
966break;
967
968default:
969{
970typedef unsigned long long vlong;
971uint64_t prfsts;
972prfsts = rdmsr64(K10_COFVID_STATUS);
973uint64_t r;
974vlong hz;
975r = (prfsts>>6) & 0x07;
976hz = (((prfsts & 0x3f)+0x10)*100000000ll)/(1<<r);
977
978currcoef = hz / (200 * Mega);
979}
980}
981
982if (currcoef)
983{
984if (currdiv)
985{
986busFrequency = ((tscFreq * 2) / ((currcoef * 2) + 1));
987busFCvtt2n = ((1 * Giga) << 32) / busFrequency;
988tscFCvtt2n = busFCvtt2n * 2 / (1 + (2 * currcoef));
989cpuFrequency = ((1 * Giga) << 32) / tscFCvtt2n;
990
991DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
992}
993else
994{
995busFrequency = (tscFreq / currcoef);
996busFCvtt2n = ((1 * Giga) << 32) / busFrequency;
997tscFCvtt2n = busFCvtt2n / currcoef;
998cpuFrequency = ((1 * Giga) << 32) / tscFCvtt2n;
999DBG("%d\n", currcoef);
1000}
1001}
1002else if (!cpuFrequency)
1003{
1004cpuFrequency = tscFreq;
1005}
1006}
1007
1008#if 0
1009if (!busFrequency)
1010{
1011busFrequency = (DEFAULT_FSB * 1000);
1012DBG("CPU: busFrequency = 0! using the default value for FSB!\n");
1013cpuFrequency = tscFreq;
1014}
1015
1016DBG("cpu freq = 0x%016llxn", timeRDTSC() * 20);
1017
1018#endif
1019
1020p->CPU.MaxCoef = maxcoef = currcoef;
1021p->CPU.MaxDiv = maxdiv = currdiv;
1022p->CPU.CurrCoef = currcoef;
1023p->CPU.CurrDiv = currdiv;
1024p->CPU.TSCFrequency = tscFreq;
1025p->CPU.FSBFrequency = busFrequency;
1026p->CPU.CPUFrequency = cpuFrequency;
1027
1028// keep formatted with spaces instead of tabs
1029DBG("\n------------------------------\n");
1030 DBG("\tCPU INFO\n");
1031DBG("------------------------------\n");
1032
1033DBG("CPUID Raw Values:\n");
1034for (i = 0; i < CPUID_MAX; i++)
1035{
1036DBG("%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]);
1037}
1038DBG("\n");
1039DBG("Brand String: %s\n",p->CPU.BrandString);// Processor name (BIOS)
1040DBG("Vendor: 0x%X\n",p->CPU.Vendor);// Vendor ex: GenuineIntel
1041DBG("Family: 0x%X\n",p->CPU.Family);// Family ex: 6 (06h)
1042DBG("ExtFamily: 0x%X\n",p->CPU.ExtFamily);
1043DBG("Signature: 0x%08X\n",p->CPU.Signature);// CPUID signature
1044/*switch (p->CPU.Type) {
1045case PT_OEM:
1046DBG("Processor type: Intel Original OEM Processor\n");
1047break;
1048case PT_OD:
1049DBG("Processor type: Intel Over Drive Processor\n");
1050break;
1051case PT_DUAL:
1052DBG("Processor type: Intel Dual Processor\n");
1053break;
1054case PT_RES:
1055DBG("Processor type: Intel Reserved\n");
1056break;
1057default:
1058break;
1059}*/
1060DBG("Model: 0x%X\n",p->CPU.Model);// Model ex: 37 (025h)
1061DBG("ExtModel: 0x%X\n",p->CPU.ExtModel);
1062DBG("Stepping: 0x%X\n",p->CPU.Stepping);// Stepping ex: 5 (05h)
1063DBG("MaxCoef: %d\n",p->CPU.MaxCoef);
1064DBG("CurrCoef: %d\n",p->CPU.CurrCoef);
1065DBG("MaxDiv: %d\n",p->CPU.MaxDiv);
1066DBG("CurrDiv: %d\n",p->CPU.CurrDiv);
1067DBG("TSCFreq: %dMHz\n",p->CPU.TSCFrequency / 1000000);
1068DBG("FSBFreq: %dMHz\n",p->CPU.FSBFrequency / 1000000);
1069DBG("CPUFreq: %dMHz\n",p->CPU.CPUFrequency / 1000000);
1070DBG("Cores: %d\n",p->CPU.NoCores);// Cores
1071DBG("Logical processor: %d\n",p->CPU.NoThreads);// Logical procesor
1072DBG("Features: 0x%08x\n",p->CPU.Features);
1073
1074DBG("\n---------------------------------------------\n");
1075#if DEBUG_CPU
1076pause();
1077#endif
1078}
1079

Archive Download this file

Revision: 2648