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

Archive Download this file

Revision: 2676