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_ATOM_3700:
490case CPUID_MODEL_HASWELL:
491case CPUID_MODEL_HASWELL_SVR:
492//case CPUID_MODEL_HASWELL_H:
493case CPUID_MODEL_HASWELL_ULT:
494case CPUID_MODEL_HASWELL_ULX:
495//case CPUID_MODEL_:
496msr = rdmsr64(MSR_CORE_THREAD_COUNT); // 0x35
497p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 31, 16);
498p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
499break;
500
501case CPUID_MODEL_DALES:
502case CPUID_MODEL_WESTMERE: // Intel Core i7 LGA1366 (32nm) 6 Core
503case CPUID_MODEL_WESTMERE_EX:
504msr = rdmsr64(MSR_CORE_THREAD_COUNT);
505p->CPU.NoCores= (uint32_t)bitfield((uint32_t)msr, 19, 16);
506p->CPU.NoThreads= (uint32_t)bitfield((uint32_t)msr, 15, 0);
507break;
508}
509
510if (p->CPU.NoCores == 0)
511{
512p->CPU.NoCores= cores_per_package;
513p->CPU.NoThreads= logical_per_package;
514}
515
516// MSR is *NOT* available on the Intel Atom CPU
517//workaround for N270. I don't know why it detected wrong
518if ((p->CPU.Model == CPUID_MODEL_ATOM) && (strstr(p->CPU.BrandString, "270")))
519{
520p->CPU.NoCores= 1;
521p->CPU.NoThreads= 2;
522}
523
524//workaround for Quad
525if ( strstr(p->CPU.BrandString, "Quad") )
526{
527p->CPU.NoCores= 4;
528p->CPU.NoThreads= 4;
529}
530}
531
532break;
533
534case CPUID_VENDOR_AMD:
535{
536
537cores_per_package = bitfield(p->CPU.CPUID[CPUID_88][ecx], 7, 0) + 1;
538threads_per_core = cores_per_package;
539
540if (cores_per_package == 0)
541{
542cores_per_package = 1;
543}
544
545p->CPU.NoCores= cores_per_package;
546p->CPU.NoThreads= logical_per_package;
547
548if (p->CPU.NoCores == 0)
549{
550p->CPU.NoCores = 1;
551p->CPU.NoThreads= 1;
552}
553}
554break;
555
556default :
557stop("Unsupported CPU detected! System halted.");
558}
559
560/* setup features */
561if ((bit(23) & p->CPU.CPUID[CPUID_1][3]) != 0)
562{
563p->CPU.Features |= CPU_FEATURE_MMX;
564}
565
566if ((bit(25) & p->CPU.CPUID[CPUID_1][3]) != 0)
567{
568p->CPU.Features |= CPU_FEATURE_SSE;
569}
570
571if ((bit(26) & p->CPU.CPUID[CPUID_1][3]) != 0)
572{
573p->CPU.Features |= CPU_FEATURE_SSE2;
574}
575
576if ((bit(0) & p->CPU.CPUID[CPUID_1][2]) != 0)
577{
578p->CPU.Features |= CPU_FEATURE_SSE3;
579}
580
581if ((bit(19) & p->CPU.CPUID[CPUID_1][2]) != 0)
582{
583p->CPU.Features |= CPU_FEATURE_SSE41;
584}
585
586if ((bit(20) & p->CPU.CPUID[CPUID_1][2]) != 0)
587{
588p->CPU.Features |= CPU_FEATURE_SSE42;
589}
590
591if ((bit(29) & p->CPU.CPUID[CPUID_81][3]) != 0)
592{
593p->CPU.Features |= CPU_FEATURE_EM64T;
594}
595
596if ((bit(5) & p->CPU.CPUID[CPUID_1][3]) != 0)
597{
598p->CPU.Features |= CPU_FEATURE_MSR;
599}
600
601if ((p->CPU.NoThreads > p->CPU.NoCores))
602{
603p->CPU.Features |= CPU_FEATURE_HTT;
604}
605
606pic0_mask = inb(0x21U);
607outb(0x21U, 0xFFU); // mask PIC0 interrupts for duration of timing tests
608
609uint64_t cycles;
610cycles = timeRDTSC();
611tscFreq = rtc_set_cyc_per_sec(cycles);
612DBG("cpu freq classic = 0x%016llx\n", tscFreq);
613// if usual method failed
614if ( tscFreq < 1000 )//TEST
615{
616tscFreq = measure_tsc_frequency();//timeRDTSC() * 20;//measure_tsc_frequency();
617// DBG("cpu freq timeRDTSC = 0x%016llx\n", tscFrequency);
618}
619
620if (p->CPU.Vendor==CPUID_VENDOR_INTEL && ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03)))
621{
622int intelCPU = p->CPU.Model;
623if (p->CPU.Family == 0x06)
624{
625/* Nehalem CPU model */
626switch (p->CPU.Model)
627{
628case CPUID_MODEL_NEHALEM:
629case CPUID_MODEL_FIELDS:
630case CPUID_MODEL_CLARKDALE:
631case CPUID_MODEL_DALES:
632case CPUID_MODEL_WESTMERE:
633case CPUID_MODEL_NEHALEM_EX:
634case CPUID_MODEL_WESTMERE_EX:
635/* --------------------------------------------------------- */
636case CPUID_MODEL_SANDYBRIDGE:
637case CPUID_MODEL_JAKETOWN:
638case CPUID_MODEL_IVYBRIDGE_XEON:
639case CPUID_MODEL_IVYBRIDGE:
640case CPUID_MODEL_ATOM_3700:
641case CPUID_MODEL_HASWELL:
642case CPUID_MODEL_HASWELL_U5:
643case CPUID_MODEL_HASWELL_SVR:
644
645case CPUID_MODEL_HASWELL_ULT:
646case CPUID_MODEL_HASWELL_ULX:
647/* --------------------------------------------------------- */
648msr = rdmsr64(MSR_PLATFORM_INFO);
649DBG("msr(%d): platform_info %08x\n", __LINE__, bitfield(msr, 31, 0));
650bus_ratio_max = bitfield(msr, 15, 8);
651bus_ratio_min = bitfield(msr, 47, 40); //valv: not sure about this one (Remarq.1)
652msr = rdmsr64(MSR_FLEX_RATIO);
653DBG("msr(%d): flex_ratio %08x\n", __LINE__, bitfield(msr, 31, 0));
654if (bitfield(msr, 16, 16))
655{
656flex_ratio = bitfield(msr, 15, 8);
657// bcc9: at least on the gigabyte h67ma-ud2h,
658// where the cpu multipler can't be changed to
659// allow overclocking, the flex_ratio msr has unexpected (to OSX)
660// contents.These contents cause mach_kernel to
661// fail to compute the bus ratio correctly, instead
662// causing the system to crash since tscGranularity
663// is inadvertently set to 0.
664
665if (flex_ratio == 0)
666{
667// Clear bit 16 (evidently the presence bit)
668wrmsr64(MSR_FLEX_RATIO, (msr & 0xFFFFFFFFFFFEFFFFULL));
669msr = rdmsr64(MSR_FLEX_RATIO);
670DBG("CPU: Unusable flex ratio detected. Patched MSR now %08x\n", bitfield(msr, 31, 0));
671}
672else
673{
674if (bus_ratio_max > flex_ratio)
675{
676bus_ratio_max = flex_ratio;
677}
678}
679}
680
681if (bus_ratio_max)
682{
683busFrequency = (tscFreq / bus_ratio_max);
684}
685
686//valv: Turbo Ratio Limit
687if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
688{
689msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
690
691cpuFrequency = bus_ratio_max * busFrequency;
692max_ratio = bus_ratio_max * 10;
693}
694else
695{
696cpuFrequency = tscFreq;
697}
698
699if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->chameleonConfig)) && (len <= 4))
700{
701max_ratio = atoi(newratio);
702max_ratio = (max_ratio * 10);
703if (len >= 3)
704{
705max_ratio = (max_ratio + 5);
706}
707
708verbose("\tBus-Ratio: min=%d, max=%s\n", bus_ratio_min, newratio);
709
710// extreme overclockers may love 320 ;)
711if ((max_ratio >= min_ratio) && (max_ratio <= 320))
712{
713cpuFrequency = (busFrequency * max_ratio) / 10;
714if (len >= 3)
715{
716maxdiv = 1;
717}
718else
719{
720maxdiv = 0;
721}
722}
723else
724{
725max_ratio = (bus_ratio_max * 10);
726}
727}
728//valv: to be uncommented if Remarq.1 didn't stick
729//if (bus_ratio_max > 0) bus_ratio = flex_ratio;
730p->CPU.MaxRatio = max_ratio;
731p->CPU.MinRatio = min_ratio;
732
733myfsb = busFrequency / 1000000;
734verbose("\tSticking with [BCLK: %dMhz, Bus-Ratio: %d]\n", myfsb, max_ratio/10); // Bungo: fixed wrong Bus-Ratio readout
735currcoef = bus_ratio_max;
736
737break;
738
739default:
740msr = rdmsr64(MSR_IA32_PERF_STATUS);
741DBG("msr(%d): ia32_perf_stat 0x%08x\n", __LINE__, bitfield(msr, 31, 0));
742currcoef = bitfield(msr, 12, 8); // Bungo: reverted to 2263 state because of wrong old CPUs freq. calculating
743// Non-integer bus ratio for the max-multi
744maxdiv = bitfield(msr, 46, 46);
745// Non-integer bus ratio for the current-multi (undocumented)
746currdiv = bitfield(msr, 14, 14);
747
748// This will always be model >= 3
749if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || (p->CPU.Family == 0x0f))
750{
751/* On these models, maxcoef defines TSC freq */
752maxcoef = bitfield(msr, 44, 40);
753}
754else
755{
756// On lower models, currcoef defines TSC freq
757// XXX
758maxcoef = currcoef;
759}
760
761if (!currcoef)
762{
763currcoef = maxcoef;
764}
765
766if (maxcoef)
767{
768if (maxdiv)
769{
770busFrequency = ((tscFreq * 2) / ((maxcoef * 2) + 1));
771}
772else
773{
774busFrequency = (tscFreq / maxcoef);
775}
776
777if (currdiv)
778{
779cpuFrequency = (busFrequency * ((currcoef * 2) + 1) / 2);
780}
781else
782{
783cpuFrequency = (busFrequency * currcoef);
784}
785
786DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");
787}
788break;
789}
790}
791// Mobile CPU
792if (rdmsr64(MSR_IA32_PLATFORM_ID) & (1<<28))
793{
794p->CPU.Features |= CPU_FEATURE_MOBILE;
795}
796}
797
798else if (p->CPU.Vendor==CPUID_VENDOR_AMD)
799{
800switch(p->CPU.Family)
801{
802case 0xF: /* K8 */
803{
804uint64_t fidvid = 0;
805uint64_t cpuMult;
806uint64_t fid;
807
808fidvid = rdmsr64(K8_FIDVID_STATUS);
809fid = bitfield(fidvid, 5, 0);
810
811cpuMult = (fid + 8) / 2;
812currcoef = cpuMult;
813
814cpuMultN2 = (fidvid & (uint64_t)bit(0));
815currdiv = cpuMultN2;
816/****** Addon END ******/
817}
818break;
819
820case 0x10: /*** AMD Family 10h ***/
821{
822uint64_t cofvid = 0;
823uint64_t cpuMult;
824uint64_t divisor = 0;
825uint64_t did;
826uint64_t fid;
827
828cofvid = rdmsr64(K10_COFVID_STATUS);
829did = bitfield(cofvid, 8, 6);
830fid = bitfield(cofvid, 5, 0);
831if (did == 0) divisor = 2;
832else if (did == 1) divisor = 4;
833else if (did == 2) divisor = 8;
834else if (did == 3) divisor = 16;
835else if (did == 4) divisor = 32;
836
837cpuMult = (fid + 16) / divisor;
838currcoef = cpuMult;
839
840cpuMultN2 = (cofvid & (uint64_t)bit(0));
841currdiv = cpuMultN2;
842
843/****** Addon END ******/
844}
845break;
846
847case 0x11: /*** AMD Family 11h ***/
848{
849uint64_t cofvid = 0;
850uint64_t cpuMult;
851uint64_t divisor = 0;
852uint64_t did;
853uint64_t fid;
854
855cofvid = rdmsr64(K10_COFVID_STATUS);
856did = bitfield(cofvid, 8, 6);
857fid = bitfield(cofvid, 5, 0);
858if (did == 0) divisor = 2;
859else if (did == 1) divisor = 4;
860else if (did == 2) divisor = 8;
861else if (did == 3) divisor = 16;
862else if (did == 4) divisor = 32;
863
864cpuMult = (fid + 8) / divisor;
865currcoef = cpuMult;
866
867cpuMultN2 = (cofvid & (uint64_t)bit(0));
868currdiv = cpuMultN2;
869
870/****** Addon END ******/
871}
872 break;
873
874case 0x12: /*** AMD Family 12h ***/
875{
876// 8:4 CpuFid: current CPU core frequency ID
877// 3:0 CpuDid: current CPU core divisor ID
878uint64_t prfsts,CpuFid,CpuDid;
879prfsts = rdmsr64(K10_COFVID_STATUS);
880
881CpuDid = bitfield(prfsts, 3, 0) ;
882CpuFid = bitfield(prfsts, 8, 4) ;
883uint64_t divisor;
884switch (CpuDid)
885{
886case 0: divisor = 1; break;
887case 1: divisor = (3/2); break;
888case 2: divisor = 2; break;
889case 3: divisor = 3; break;
890case 4: divisor = 4; break;
891case 5: divisor = 6; break;
892case 6: divisor = 8; break;
893case 7: divisor = 12; break;
894case 8: divisor = 16; break;
895default: divisor = 1; break;
896}
897currcoef = (CpuFid + 0x10) / divisor;
898
899cpuMultN2 = (prfsts & (uint64_t)bit(0));
900currdiv = cpuMultN2;
901
902}
903break;
904
905case 0x14: /* K14 */
906
907{
908// 8:4: current CPU core divisor ID most significant digit
909// 3:0: current CPU core divisor ID least significant digit
910uint64_t prfsts;
911prfsts = rdmsr64(K10_COFVID_STATUS);
912
913uint64_t CpuDidMSD,CpuDidLSD;
914CpuDidMSD = bitfield(prfsts, 8, 4) ;
915CpuDidLSD = bitfield(prfsts, 3, 0) ;
916
917uint64_t frequencyId = 0x10;
918currcoef = (frequencyId + 0x10) /
919(CpuDidMSD + (CpuDidLSD * 0.25) + 1);
920currdiv = ((CpuDidMSD) + 1) << 2;
921currdiv += bitfield(msr, 3, 0);
922
923cpuMultN2 = (prfsts & (uint64_t)bit(0));
924currdiv = cpuMultN2;
925}
926
927break;
928
929case 0x15: /*** AMD Family 15h ***/
930case 0x06: /*** AMD Family 06h ***/
931{
932
933uint64_t cofvid = 0;
934uint64_t cpuMult;
935uint64_t divisor = 0;
936uint64_t did;
937uint64_t fid;
938
939cofvid = rdmsr64(K10_COFVID_STATUS);
940did = bitfield(cofvid, 8, 6);
941fid = bitfield(cofvid, 5, 0);
942if (did == 0) divisor = 2;
943else if (did == 1) divisor = 4;
944else if (did == 2) divisor = 8;
945else if (did == 3) divisor = 16;
946else if (did == 4) divisor = 32;
947
948cpuMult = (fid + 16) / divisor;
949currcoef = cpuMult;
950
951cpuMultN2 = (cofvid & (uint64_t)bit(0));
952currdiv = cpuMultN2;
953}
954break;
955
956case 0x16: /*** AMD Family 16h kabini ***/
957{
958uint64_t cofvid = 0;
959uint64_t cpuMult;
960uint64_t divisor = 0;
961uint64_t did;
962uint64_t fid;
963
964cofvid = rdmsr64(K10_COFVID_STATUS);
965did = bitfield(cofvid, 8, 6);
966fid = bitfield(cofvid, 5, 0);
967if (did == 0) divisor = 1;
968else if (did == 1) divisor = 2;
969else if (did == 2) divisor = 4;
970else if (did == 3) divisor = 8;
971else if (did == 4) divisor = 16;
972
973cpuMult = (fid + 16) / divisor;
974currcoef = cpuMult;
975
976cpuMultN2 = (cofvid & (uint64_t)bit(0));
977currdiv = cpuMultN2;
978/****** Addon END ******/
979}
980break;
981
982default:
983{
984typedef unsigned long long vlong;
985uint64_t prfsts;
986prfsts = rdmsr64(K10_COFVID_STATUS);
987uint64_t r;
988vlong hz;
989r = (prfsts>>6) & 0x07;
990hz = (((prfsts & 0x3f)+0x10)*100000000ll)/(1<<r);
991
992currcoef = hz / (200 * Mega);
993}
994}
995
996if (currcoef)
997{
998if (currdiv)
999{
1000busFrequency = ((tscFreq * 2) / ((currcoef * 2) + 1));
1001busFCvtt2n = ((1 * Giga) << 32) / busFrequency;
1002tscFCvtt2n = busFCvtt2n * 2 / (1 + (2 * currcoef));
1003cpuFrequency = ((1 * Giga) << 32) / tscFCvtt2n;
1004
1005DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);
1006}
1007else
1008{
1009busFrequency = (tscFreq / currcoef);
1010busFCvtt2n = ((1 * Giga) << 32) / busFrequency;
1011tscFCvtt2n = busFCvtt2n / currcoef;
1012cpuFrequency = ((1 * Giga) << 32) / tscFCvtt2n;
1013DBG("%d\n", currcoef);
1014}
1015}
1016else if (!cpuFrequency)
1017{
1018cpuFrequency = tscFreq;
1019}
1020}
1021
1022#if 0
1023if (!busFrequency)
1024{
1025busFrequency = (DEFAULT_FSB * 1000);
1026DBG("\tCPU: busFrequency = 0! using the default value for FSB!\n");
1027cpuFrequency = tscFreq;
1028}
1029
1030DBG("\tcpu freq = 0x%016llxn", timeRDTSC() * 20);
1031
1032#endif
1033
1034outb(0x21U, pic0_mask); // restore PIC0 interrupts
1035
1036p->CPU.MaxCoef = maxcoef = currcoef;
1037p->CPU.MaxDiv = maxdiv = currdiv;
1038p->CPU.CurrCoef = currcoef;
1039p->CPU.CurrDiv = currdiv;
1040p->CPU.TSCFrequency = tscFreq;
1041p->CPU.FSBFrequency = busFrequency;
1042p->CPU.CPUFrequency = cpuFrequency;
1043
1044// keep formatted with spaces instead of tabs
1045
1046DBG("\tCPUID Raw Values:\n");
1047for (i = 0; i < CPUID_MAX; i++)
1048{
1049DBG("\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]);
1050}
1051DBG("\n");
1052DBG("\tBrand String: %s\n",p->CPU.BrandString);// Processor name (BIOS)
1053DBG("\tVendor: 0x%X\n",p->CPU.Vendor);// Vendor ex: GenuineIntel
1054DBG("\tFamily: 0x%X\n",p->CPU.Family);// Family ex: 6 (06h)
1055DBG("\tExtFamily: 0x%X\n",p->CPU.ExtFamily);
1056DBG("\tSignature: 0x%08X\n",p->CPU.Signature);// CPUID signature
1057/*switch (p->CPU.Type) {
1058case PT_OEM:
1059DBG("\tProcessor type: Intel Original OEM Processor\n");
1060break;
1061case PT_OD:
1062DBG("\tProcessor type: Intel Over Drive Processor\n");
1063break;
1064case PT_DUAL:
1065DBG("\tProcessor type: Intel Dual Processor\n");
1066break;
1067case PT_RES:
1068DBG("\tProcessor type: Intel Reserved\n");
1069break;
1070default:
1071break;
1072}*/
1073DBG("\tModel: 0x%X\n",p->CPU.Model);// Model ex: 37 (025h)
1074DBG("\tExtModel: 0x%X\n",p->CPU.ExtModel);
1075DBG("\tStepping: 0x%X\n",p->CPU.Stepping);// Stepping ex: 5 (05h)
1076DBG("\tMaxCoef: %d\n",p->CPU.MaxCoef);
1077DBG("\tCurrCoef: %d\n",p->CPU.CurrCoef);
1078DBG("\tMaxDiv: %d\n",p->CPU.MaxDiv);
1079DBG("\tCurrDiv: %d\n",p->CPU.CurrDiv);
1080DBG("\tTSCFreq: %dMHz\n",p->CPU.TSCFrequency / 1000000);
1081DBG("\tFSBFreq: %dMHz\n",p->CPU.FSBFrequency / 1000000);
1082DBG("\tCPUFreq: %dMHz\n",p->CPU.CPUFrequency / 1000000);
1083DBG("\tCores: %d\n",p->CPU.NoCores);// Cores
1084DBG("\tLogical processor: %d\n",p->CPU.NoThreads);// Logical procesor
1085DBG("\tFeatures: 0x%08x\n",p->CPU.Features);
1086
1087verbose("\n");
1088#if DEBUG_CPU
1089pause();
1090#endif
1091}
1092

Archive Download this file

Revision: 2687