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

Archive Download this file

Revision: 2775