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

Archive Download this file

Revision: 2671