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_tpic0_mask;
304uint8_tcpuMultN2= 0;
305
306const char*newratio;
307charstr[128];
308char*s= 0;
309
310intlen= 0;
311intmyfsb= 0;
312inti= 0;
313
314
315/* http://www.flounder.com/cpuid_explorer2.htm
316 EAX (Intel):
317 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
318 +--------+----------------+--------+----+----+--------+--------+--------+
319 |########|Extended family |Extmodel|####|type|familyid| model |stepping|
320 +--------+----------------+--------+----+----+--------+--------+--------+
321
322 EAX (AMD):
323 31 28 27 20 19 16 1514 1312 11 8 7 4 3 0
324 +--------+----------------+--------+----+----+--------+--------+--------+
325 |########|Extended family |Extmodel|####|####|familyid| model |stepping|
326 +--------+----------------+--------+----+----+--------+--------+--------+
327*/
328///////////////////-- MaxFn,Vendor --////////////////////////
329do_cpuid(0x00000000, p->CPU.CPUID[CPUID_0]); // MaxFn, Vendor
330p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
331/////////////////////////////////////////////////////////////
332
333///////////////////-- Signature, stepping, features -- //////
334do_cpuid(0x00000001, p->CPU.CPUID[CPUID_1]); // Signature, stepping, features
335cpuid_features = quad(p->CPU.CPUID[CPUID_1][ecx],p->CPU.CPUID[CPUID_1][edx]);
336if (bit(28) & p->CPU.CPUID[CPUID_1][edx]) // HTT/Multicore
337{
338logical_per_package = bitfield(p->CPU.CPUID[CPUID_1][ebx], 23, 16);
339}
340else
341{
342logical_per_package = 1;
343}
344//printf("logical %d\n",logical_per_package);
345
346p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
347p->CPU.Stepping= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);// stepping = cpu_feat_eax & 0xF;
348p->CPU.Model= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);// model = (cpu_feat_eax >> 4) & 0xF;
349p->CPU.Family= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);// family = (cpu_feat_eax >> 8) & 0xF;
350//p->CPU.Type= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12);// type = (cpu_feat_eax >> 12) & 0x3;
351p->CPU.ExtModel= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);// ext_model = (cpu_feat_eax >> 16) & 0xF;
352p->CPU.ExtFamily= (uint8_t)bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);// ext_family = (cpu_feat_eax >> 20) & 0xFF;
353
354if (p->CPU.Family == 0x0f)
355{
356p->CPU.Family += p->CPU.ExtFamily;
357}
358
359if (p->CPU.Family == 0x0f || p->CPU.Family == 0x06)
360{
361p->CPU.Model += (p->CPU.ExtModel << 4);
362}
363
364/* get BrandString (if supported) */
365/* Copyright: from Apple's XNU cpuid.c */
366if (p->CPU.CPUID[CPUID_80][0] > 0x80000004)
367{
368bzero(str, 128);
369/*
370 * The BrandString 48 bytes (max), guaranteed to
371 * be NULL terminated.
372 */
373do_cpuid(0x80000002, reg);
374memcpy(&str[0], (char *)reg, 16);
375do_cpuid(0x80000003, reg);
376memcpy(&str[16], (char *)reg, 16);
377do_cpuid(0x80000004, reg);
378memcpy(&str[32], (char *)reg, 16);
379for (s = str; *s != '\0'; s++)
380{
381if (*s != ' ')
382{
383break;
384}
385}
386strlcpy(p->CPU.BrandString, s, 48);
387
388if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, MIN(sizeof(p->CPU.BrandString), (unsigned)strlen(CPU_STRING_UNKNOWN) + 1)))
389{
390/*
391 * This string means we have a firmware-programmable brand string,
392 * and the firmware couldn't figure out what sort of CPU we have.
393 */
394p->CPU.BrandString[0] = '\0';
395}
396p->CPU.BrandString[47] = '\0';
397//DBG("Brandstring = %s\n", p->CPU.BrandString);
398}
399
400//char *vendor = p->CPU.cpuid_vendor;
401switch (p->CPU.Vendor){
402case CPUID_VENDOR_INTEL:
403{
404
405 do_cpuid(0x00000002, p->CPU.CPUID[CPUID_2]); // TLB/Cache/Prefetch
406
407 do_cpuid(0x00000003, p->CPU.CPUID[CPUID_3]); // S/N
408
409 /* Based on Apple's XNU cpuid.c - Deterministic cache parameters */
410 if ((p->CPU.CPUID[CPUID_0][eax] > 3) && (p->CPU.CPUID[CPUID_0][eax] < 0x80000000))
411 {
412 for (i = 0; i < 0xFF; i++) // safe loop
413 {
414 do_cpuid2(0x00000004, i, reg); // AX=4: Fn, CX=i: cache index
415 if (bitfield(reg[eax], 4, 0) == 0)
416 {
417 break;
418 }
419 cores_per_package = bitfield(reg[eax], 31, 26) + 1;
420 }
421 }
422
423 do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
424
425 if (i > 0)
426 {
427 cores_per_package = bitfield(p->CPU.CPUID[CPUID_4][eax], 31, 26) + 1; // i = cache index
428 threads_per_core = bitfield(p->CPU.CPUID[CPUID_4][eax], 25, 14) + 1;
429 }
430
431 if (cores_per_package == 0)
432 {
433 cores_per_package = 1;
434 }
435
436 if (p->CPU.CPUID[CPUID_0][0] >= 0x5)// Monitor/Mwait
437 {
438 do_cpuid(5, p->CPU.CPUID[CPUID_5]);
439 }
440
441 if (p->CPU.CPUID[CPUID_0][0] >= 6)// Thermal/Power
442 {
443 do_cpuid(6, p->CPU.CPUID[CPUID_6]);
444 }
445
446 do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
447
448 if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8)
449 {
450 do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
451 do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
452 }
453 else if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1)
454 {
455 do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
456 }
457
458switch (p->CPU.Model)
459{
460case CPUID_MODEL_NEHALEM:
461case CPUID_MODEL_FIELDS:
462case CPUID_MODEL_DALES:
463case CPUID_MODEL_NEHALEM_EX:
464case CPUID_MODEL_JAKETOWN:
465case CPUID_MODEL_SANDYBRIDGE:
466case CPUID_MODEL_IVYBRIDGE:
467
468case CPUID_MODEL_HASWELL:
469case CPUID_MODEL_HASWELL_SVR:
470//case CPUID_MODEL_HASWELL_H:
471case CPUID_MODEL_HASWELL_ULT:
472case CPUID_MODEL_CRYSTALWELL:
473//case CPUID_MODEL_:
474msr = rdmsr64(MSR_CORE_THREAD_COUNT);
475p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 31, 16);
476p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
477break;
478
479case CPUID_MODEL_DALES_32NM:
480case CPUID_MODEL_WESTMERE:
481case CPUID_MODEL_WESTMERE_EX:
482msr = rdmsr64(MSR_CORE_THREAD_COUNT);
483p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 19, 16);
484p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
485break;
486}
487
488if (p->CPU.NoCores == 0)
489{
490p->CPU.NoCores= cores_per_package;
491p->CPU.NoThreads= logical_per_package;
492}
493
494// MSR is *NOT* available on the Intel Atom CPU
495//workaround for N270. I don't know why it detected wrong
496if ((p->CPU.Model == CPUID_MODEL_ATOM) && (strstr(p->CPU.BrandString, "270")))
497{
498p->CPU.NoCores= 1;
499p->CPU.NoThreads= 2;
500}
501
502//workaround for Quad
503if ( strstr(p->CPU.BrandString, "Quad") )
504{
505p->CPU.NoCores= 4;
506p->CPU.NoThreads= 4;
507}
508}
509
510break;
511
512case CPUID_VENDOR_AMD:
513{
514do_cpuid(5, p->CPU.CPUID[CPUID_5]); // Monitor/Mwait
515
516do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
517if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 8)
518{
519do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
520}
521
522if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1)
523{
524do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
525}
526
527do_cpuid(0x80000005, p->CPU.CPUID[CPUID_85]); // TLB/Cache/Prefetch
528do_cpuid(0x80000006, p->CPU.CPUID[CPUID_86]); // TLB/Cache/Prefetch
529do_cpuid(0x80000008, p->CPU.CPUID[CPUID_88]);
530
531cores_per_package = bitfield(p->CPU.CPUID[CPUID_88][ecx], 7, 0) + 1;
532threads_per_core = cores_per_package;
533
534if (cores_per_package == 0)
535{
536cores_per_package = 1;
537}
538
539p->CPU.NoCores= cores_per_package;
540p->CPU.NoThreads= logical_per_package;
541
542if (p->CPU.NoCores == 0)
543{
544p->CPU.NoCores = 1;
545p->CPU.NoThreads= 1;
546}
547}
548break;
549default :
550stop("Unsupported CPU detected! System halted.");
551}
552
553/* setup features */
554if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0)
555{
556p->CPU.Features |= CPU_FEATURE_MMX;
557}
558
559if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0)
560{
561p->CPU.Features |= CPU_FEATURE_SSE;
562}
563
564if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0)
565{
566p->CPU.Features |= CPU_FEATURE_SSE2;
567}
568
569if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0)
570{
571p->CPU.Features |= CPU_FEATURE_SSE3;
572}
573
574if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0)
575{
576p->CPU.Features |= CPU_FEATURE_SSE41;
577}
578
579if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0)
580{
581p->CPU.Features |= CPU_FEATURE_SSE42;
582}
583
584if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0)
585{
586p->CPU.Features |= CPU_FEATURE_EM64T;
587}
588
589if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0)
590{
591p->CPU.Features |= CPU_FEATURE_MSR;
592}
593
594if ((p->CPU.NoThreads > p->CPU.NoCores))
595{
596p->CPU.Features |= CPU_FEATURE_HTT;
597}
598
599pic0_mask = inb(0x21U);
600outb(0x21U, 0xFFU); // mask PIC0 interrupts for duration of timing tests
601
602uint64_t cycles;
603cycles = timeRDTSC();
604tscFreq = rtc_set_cyc_per_sec(cycles);
605DBG("cpu freq classic = 0x%016llx\n", tscFreq);
606// if usual method failed
607if ( tscFreq < 1000 )//TEST
608{
609tscFreq = measure_tsc_frequency();//timeRDTSC() * 20;//measure_tsc_frequency();
610// DBG("cpu freq timeRDTSC = 0x%016llx\n", tscFrequency);
611}
612
613if (p->CPU.Vendor==CPUID_VENDOR_INTEL && ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)))
614{
615int intelCPU = p->CPU.Model;
616if (p->CPU.Family == 0x06)
617{
618/* Nehalem CPU model */
619switch (p->CPU.Model)
620{
621case CPUID_MODEL_NEHALEM:
622case CPUID_MODEL_FIELDS:
623case CPUID_MODEL_DALES:
624case CPUID_MODEL_DALES_32NM:
625case CPUID_MODEL_WESTMERE:
626case CPUID_MODEL_NEHALEM_EX:
627case CPUID_MODEL_WESTMERE_EX:
628/* --------------------------------------------------------- */
629case CPUID_MODEL_SANDYBRIDGE:
630case CPUID_MODEL_JAKETOWN:
631case CPUID_MODEL_IVYBRIDGE_XEON:
632case CPUID_MODEL_IVYBRIDGE:
633case CPUID_MODEL_HASWELL:
634case CPUID_MODEL_HASWELL_SVR:
635
636case CPUID_MODEL_HASWELL_ULT:
637case CPUID_MODEL_CRYSTALWELL:
638/* --------------------------------------------------------- */
639msr = rdmsr64(MSR_PLATFORM_INFO);
640DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
641bus_ratio_max = bitfield(msr, 15, 8);
642bus_ratio_min = bitfield(msr, 47, 40); //valv: not sure about this one (Remarq.1)
643msr = rdmsr64(MSR_FLEX_RATIO);
644DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
645if (bitfield(msr, 16, 16))
646{
647flex_ratio = bitfield(msr, 15, 8);
648// bcc9: at least on the gigabyte h67ma-ud2h,
649// where the cpu multipler can't be changed to
650// allow overclocking, the flex_ratio msr has unexpected (to OSX)
651// contents.These contents cause mach_kernel to
652// fail to compute the bus ratio correctly, instead
653// causing the system to crash since tscGranularity
654// is inadvertently set to 0.
655
656if (flex_ratio == 0)
657{
658// Clear bit 16 (evidently the presence bit)
659wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
660msr = rdmsr64(MSR_FLEX_RATIO);
661DBG("CPU: Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
662}
663else
664{
665if (bus_ratio_max > flex_ratio)
666{
667bus_ratio_max = flex_ratio;
668}
669}
670}
671
672if (bus_ratio_max)
673{
674busFrequency = (tscFreq / bus_ratio_max);
675}
676
677//valv: Turbo Ratio Limit
678if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
679{
680msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
681
682cpuFrequency = bus_ratio_max * busFrequency;
683max_ratio = bus_ratio_max * 10;
684}
685else
686{
687cpuFrequency = tscFreq;
688}
689if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4))
690{
691max_ratio = atoi(newratio);
692max_ratio = (max_ratio * 10);
693if (len >= 3)
694{
695max_ratio = (max_ratio + 5);
696}
697
698verbose("Bus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
699
700// extreme overclockers may love 320 ;)
701if ((max_ratio >= min_ratio) && (max_ratio <= 320))
702{
703cpuFrequency = (busFrequency * max_ratio) / 10;
704if (len >= 3)
705{
706maxdiv = 1;
707}
708else
709{
710maxdiv = 0;
711}
712}
713else
714{
715max_ratio = (bus_ratio_max * 10);
716}
717}
718//valv: to be uncommented if Remarq.1 didn't stick
719//if (bus_ratio_max > 0) bus_ratio = flex_ratio;
720p->CPU.MaxRatio = max_ratio;
721p->CPU.MinRatio = min_ratio;
722
723myfsb = busFrequency / 1000000;
724verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio/10); // Bungo: fixed wrong Bus-Ratio readout
725currcoef = bus_ratio_max;
726
727break;
728
729default:
730msr = rdmsr64(MSR_IA32_PERF_STATUS);
731DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
732currcoef = bitfield(msr, 12, 8); // Bungo: reverted to 2263 state because of wrong old CPUs freq. calculating
733// Non-integer bus ratio for the max-multi
734maxdiv = bitfield(msr, 46, 46);
735// Non-integer bus ratio for the current-multi (undocumented)
736currdiv = bitfield(msr, 14, 14);
737
738// This will always be model >= 3
739if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f))
740{
741/* On these models, maxcoef defines TSC freq */
742maxcoef = bitfield(msr, 44, 40);
743}
744else
745{
746// On lower models, currcoef defines TSC freq
747// XXX
748maxcoef = currcoef;
749}
750
751if (!currcoef)
752{
753currcoef = maxcoef;
754}
755
756if (maxcoef)
757{
758if (maxdiv)
759{
760busFrequency = ((tscFreq * 2) / ((maxcoef * 2) + 1));
761}
762else
763{
764busFrequency = (tscFreq / maxcoef);
765}
766
767if (currdiv)
768{
769cpuFrequency = (busFrequency * ((currcoef * 2) + 1) / 2);
770}
771else
772{
773cpuFrequency = (busFrequency * currcoef);
774}
775
776DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
777}
778break;
779}
780}
781// Mobile CPU
782if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28))
783{
784p->CPU.Features |= CPU_FEATURE_MOBILE;
785}
786}
787
788else if (p->CPU.Vendor==CPUID_VENDOR_AMD)
789{
790switch(p->CPU.Family)
791{
792case 0xF: /* K8 */
793{
794uint64_t fidvid = 0;
795uint64_t cpuMult;
796uint64_t fid;
797
798fidvid = rdmsr64(K8_FIDVID_STATUS);
799fid = bitfield(fidvid, 5, 0);
800
801cpuMult = (fid + 8) / 2;
802currcoef = cpuMult;
803
804cpuMultN2 = (fidvid & (uint64_t)bit(0));
805currdiv = cpuMultN2;
806/****** Addon END ******/
807}
808break;
809
810case 0x10: /*** AMD Family 10h ***/
811{
812uint64_t cofvid = 0;
813uint64_t cpuMult;
814uint64_t divisor = 0;
815uint64_t did;
816uint64_t fid;
817
818cofvid = rdmsr64(K10_COFVID_STATUS);
819did = bitfield(cofvid, 8, 6);
820fid = bitfield(cofvid, 5, 0);
821if (did == 0) divisor = 2;
822else if (did == 1) divisor = 4;
823else if (did == 2) divisor = 8;
824else if (did == 3) divisor = 16;
825else if (did == 4) divisor = 32;
826
827cpuMult = (fid + 16) / divisor;
828currcoef = cpuMult;
829
830cpuMultN2 = (cofvid & (uint64_t)bit(0));
831currdiv = cpuMultN2;
832
833/****** Addon END ******/
834}
835break;
836
837case 0x11: /*** AMD Family 11h ***/
838{
839uint64_t cofvid = 0;
840uint64_t cpuMult;
841uint64_t divisor = 0;
842uint64_t did;
843uint64_t fid;
844
845cofvid = rdmsr64(K10_COFVID_STATUS);
846did = bitfield(cofvid, 8, 6);
847fid = bitfield(cofvid, 5, 0);
848if (did == 0) divisor = 2;
849else if (did == 1) divisor = 4;
850else if (did == 2) divisor = 8;
851else if (did == 3) divisor = 16;
852else if (did == 4) divisor = 32;
853
854cpuMult = (fid + 8) / divisor;
855currcoef = cpuMult;
856
857cpuMultN2 = (cofvid & (uint64_t)bit(0));
858currdiv = cpuMultN2;
859
860/****** Addon END ******/
861}
862 break;
863
864case 0x12: /*** AMD Family 12h ***/
865{
866// 8:4 CpuFid: current CPU core frequency ID
867// 3:0 CpuDid: current CPU core divisor ID
868uint64_t prfsts,CpuFid,CpuDid;
869prfsts = rdmsr64(K10_COFVID_STATUS);
870
871CpuDid = bitfield(prfsts, 3, 0) ;
872CpuFid = bitfield(prfsts, 8, 4) ;
873uint64_t divisor;
874switch (CpuDid)
875{
876case 0: divisor = 1; break;
877case 1: divisor = (3/2); break;
878case 2: divisor = 2; break;
879case 3: divisor = 3; break;
880case 4: divisor = 4; break;
881case 5: divisor = 6; break;
882case 6: divisor = 8; break;
883case 7: divisor = 12; break;
884case 8: divisor = 16; break;
885default: divisor = 1; break;
886}
887currcoef = (CpuFid + 0x10) / divisor;
888
889cpuMultN2 = (prfsts & (uint64_t)bit(0));
890currdiv = cpuMultN2;
891
892}
893break;
894
895case 0x14: /* K14 */
896
897{
898// 8:4: current CPU core divisor ID most significant digit
899// 3:0: current CPU core divisor ID least significant digit
900uint64_t prfsts;
901prfsts = rdmsr64(K10_COFVID_STATUS);
902
903uint64_t CpuDidMSD,CpuDidLSD;
904CpuDidMSD = bitfield(prfsts, 8, 4) ;
905CpuDidLSD = bitfield(prfsts, 3, 0) ;
906
907uint64_t frequencyId = 0x10;
908currcoef = (frequencyId + 0x10) /
909(CpuDidMSD + (CpuDidLSD * 0.25) + 1);
910currdiv = ((CpuDidMSD) + 1) << 2;
911currdiv += bitfield(msr, 3, 0);
912
913cpuMultN2 = (prfsts & (uint64_t)bit(0));
914currdiv = cpuMultN2;
915}
916
917break;
918
919case 0x15: /*** AMD Family 15h ***/
920case 0x06: /*** AMD Family 06h ***/
921{
922
923uint64_t cofvid = 0;
924uint64_t cpuMult;
925uint64_t divisor = 0;
926uint64_t did;
927uint64_t fid;
928
929cofvid = rdmsr64(K10_COFVID_STATUS);
930did = bitfield(cofvid, 8, 6);
931fid = bitfield(cofvid, 5, 0);
932if (did == 0) divisor = 2;
933else if (did == 1) divisor = 4;
934else if (did == 2) divisor = 8;
935else if (did == 3) divisor = 16;
936else if (did == 4) divisor = 32;
937
938cpuMult = (fid + 16) / divisor;
939currcoef = cpuMult;
940
941cpuMultN2 = (cofvid & (uint64_t)bit(0));
942currdiv = cpuMultN2;
943}
944break;
945
946case 0x16: /*** AMD Family 16h kabini ***/
947{
948uint64_t cofvid = 0;
949uint64_t cpuMult;
950uint64_t divisor = 0;
951uint64_t did;
952uint64_t fid;
953
954cofvid = rdmsr64(K10_COFVID_STATUS);
955did = bitfield(cofvid, 8, 6);
956fid = bitfield(cofvid, 5, 0);
957if (did == 0) divisor = 1;
958else if (did == 1) divisor = 2;
959else if (did == 2) divisor = 4;
960else if (did == 3) divisor = 8;
961else if (did == 4) divisor = 16;
962
963cpuMult = (fid + 16) / divisor;
964currcoef = cpuMult;
965
966cpuMultN2 = (cofvid & (uint64_t)bit(0));
967currdiv = cpuMultN2;
968/****** Addon END ******/
969}
970break;
971
972default:
973{
974typedef unsigned long long vlong;
975uint64_t prfsts;
976prfsts = rdmsr64(K10_COFVID_STATUS);
977uint64_t r;
978vlong hz;
979r = (prfsts>>6) & 0x07;
980hz = (((prfsts & 0x3f)+0x10)*100000000ll)/(1<<r);
981
982currcoef = hz / (200 * Mega);
983}
984}
985
986if (currcoef)
987{
988if (currdiv)
989{
990busFrequency = ((tscFreq * 2) / ((currcoef * 2) + 1));
991busFCvtt2n = ((1 * Giga) << 32) / busFrequency;
992tscFCvtt2n = busFCvtt2n * 2 / (1 + (2 * currcoef));
993cpuFrequency = ((1 * Giga) << 32) / tscFCvtt2n;
994
995DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
996}
997else
998{
999busFrequency = (tscFreq / currcoef);
1000busFCvtt2n = ((1 * Giga) << 32) / busFrequency;
1001tscFCvtt2n = busFCvtt2n / currcoef;
1002cpuFrequency = ((1 * Giga) << 32) / tscFCvtt2n;
1003DBG("%d\n", currcoef);
1004}
1005}
1006else if (!cpuFrequency)
1007{
1008cpuFrequency = tscFreq;
1009}
1010}
1011
1012#if 0
1013if (!busFrequency)
1014{
1015busFrequency = (DEFAULT_FSB * 1000);
1016DBG("CPU: busFrequency = 0! using the default value for FSB!\n");
1017cpuFrequency = tscFreq;
1018}
1019
1020DBG("cpu freq = 0x%016llxn", timeRDTSC() * 20);
1021
1022#endif
1023
1024outb(0x21U, pic0_mask); // restore PIC0 interrupts
1025
1026p->CPU.MaxCoef = maxcoef = currcoef;
1027p->CPU.MaxDiv = maxdiv = currdiv;
1028p->CPU.CurrCoef = currcoef;
1029p->CPU.CurrDiv = currdiv;
1030p->CPU.TSCFrequency = tscFreq;
1031p->CPU.FSBFrequency = busFrequency;
1032p->CPU.CPUFrequency = cpuFrequency;
1033
1034// keep formatted with spaces instead of tabs
1035DBG("\n------------------------------\n");
1036 DBG("\tCPU INFO\n");
1037DBG("------------------------------\n");
1038
1039DBG("CPUID Raw Values:\n");
1040for (i = 0; i < CPUID_MAX; i++)
1041{
1042DBG("%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]);
1043}
1044DBG("\n");
1045DBG("Brand String: %s\n",p->CPU.BrandString);// Processor name (BIOS)
1046DBG("Vendor: 0x%X\n",p->CPU.Vendor);// Vendor ex: GenuineIntel
1047DBG("Family: 0x%X\n",p->CPU.Family);// Family ex: 6 (06h)
1048DBG("ExtFamily: 0x%X\n",p->CPU.ExtFamily);
1049DBG("Signature: 0x%08X\n",p->CPU.Signature);// CPUID signature
1050/*switch (p->CPU.Type) {
1051case PT_OEM:
1052DBG("Processor type: Intel Original OEM Processor\n");
1053break;
1054case PT_OD:
1055DBG("Processor type: Intel Over Drive Processor\n");
1056break;
1057case PT_DUAL:
1058DBG("Processor type: Intel Dual Processor\n");
1059break;
1060case PT_RES:
1061DBG("Processor type: Intel Reserved\n");
1062break;
1063default:
1064break;
1065}*/
1066DBG("Model: 0x%X\n",p->CPU.Model);// Model ex: 37 (025h)
1067DBG("ExtModel: 0x%X\n",p->CPU.ExtModel);
1068DBG("Stepping: 0x%X\n",p->CPU.Stepping);// Stepping ex: 5 (05h)
1069DBG("MaxCoef: %d\n",p->CPU.MaxCoef);
1070DBG("CurrCoef: %d\n",p->CPU.CurrCoef);
1071DBG("MaxDiv: %d\n",p->CPU.MaxDiv);
1072DBG("CurrDiv: %d\n",p->CPU.CurrDiv);
1073DBG("TSCFreq: %dMHz\n",p->CPU.TSCFrequency / 1000000);
1074DBG("FSBFreq: %dMHz\n",p->CPU.FSBFrequency / 1000000);
1075DBG("CPUFreq: %dMHz\n",p->CPU.CPUFrequency / 1000000);
1076DBG("Cores: %d\n",p->CPU.NoCores);// Cores
1077DBG("Logical processor: %d\n",p->CPU.NoThreads);// Logical procesor
1078DBG("Features: 0x%08x\n",p->CPU.Features);
1079
1080DBG("\n---------------------------------------------\n");
1081#if DEBUG_CPU
1082pause();
1083#endif
1084}
1085

Archive Download this file

Revision: 2654