Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2803