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

Archive Download this file

Revision: 2783