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

Archive Download this file

Revision: 2826