Chameleon

Chameleon Svn Source Tree

Root/branches/valv/i386/libsaio/cpu.c

  • Property svn:executable set to
1/*
2 * Copyright 2008 Islam Ahmed Zaid. All rights reserved. <azismed@gmail.com>
3 * AsereBLN: 2009: cleanup and bugfix
4 * valv: 2010: fine-tuning and additions
5 */
6
7#include "libsaio.h"
8#include "platform.h"
9#include "cpu.h"
10#include "boot.h"
11#include "bootstruct.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...)msglog(x)
21#endif
22
23/*
24 * DFE: Measures the TSC frequency in Hz (64-bit) using the ACPI PM timer
25 */
26static uint64_t measure_tsc_frequency(void)
27{
28 uint64_t tscStart;
29 uint64_t tscEnd;
30 uint64_t tscDelta = 0xffffffffffffffffULL;
31 unsigned long pollCount;
32 uint64_t retval = 0;
33 int i;
34
35 /* Time how many TSC ticks elapse in 30 msec using the 8254 PIT
36 * counter 2. We run this loop 3 times to make sure the cache
37 * is hot and we take the minimum delta from all of the runs.
38 * That is to say that we're biased towards measuring the minimum
39 * number of TSC ticks that occur while waiting for the timer to
40 * expire. That theoretically helps avoid inconsistencies when
41 * running under a VM if the TSC is not virtualized and the host
42 * steals time. The TSC is normally virtualized for VMware.
43 */
44 for(i = 0; i < 10; ++i)
45 {
46 enable_PIT2();
47 set_PIT2_mode0(CALIBRATE_LATCH);
48 tscStart = rdtsc64();
49 pollCount = poll_PIT2_gate();
50 tscEnd = rdtsc64();
51 /* The poll loop must have run at least a few times for accuracy */
52 if(pollCount <= 1)
53 continue;
54 /* The TSC must increment at LEAST once every millisecond. We
55 * should have waited exactly 30 msec so the TSC delta should
56 * be >= 30. Anything less and the processor is way too slow.
57 */
58 if((tscEnd - tscStart) <= CALIBRATE_TIME_MSEC)
59 continue;
60 // tscDelta = min(tscDelta, (tscEnd - tscStart))
61 if( (tscEnd - tscStart) < tscDelta )
62 tscDelta = tscEnd - tscStart;
63 }
64 /* tscDelta is now the least number of TSC ticks the processor made in
65 * a timespan of 0.03 s (e.g. 30 milliseconds)
66 * Linux thus divides by 30 which gives the answer in kiloHertz because
67 * 1 / ms = kHz. But we're xnu and most of the rest of the code uses
68 * Hz so we need to convert our milliseconds to seconds. Since we're
69 * dividing by the milliseconds, we simply multiply by 1000.
70 */
71
72 /* Unlike linux, we're not limited to 32-bit, but we do need to take care
73 * that we're going to multiply by 1000 first so we do need at least some
74 * arithmetic headroom. For now, 32-bit should be enough.
75 * Also unlike Linux, our compiler can do 64-bit integer arithmetic.
76 */
77 if(tscDelta > (1ULL<<32))
78 retval = 0;
79 else
80 {
81 retval = tscDelta * 1000 / 30;
82 }
83 disable_PIT2();
84 return retval;
85}
86
87
88/*
89 * Calculates the FSB and CPU frequencies using specific MSRs for each CPU
90 * - multi. is read from a specific MSR. In the case of Intel, there is:
91 * a max multi. (used to calculate the FSB freq.),
92 * and a current multi. (used to calculate the CPU freq.)
93 * - fsbFrequency = tscFrequency / multi
94 * - cpuFrequency = fsbFrequency * multi
95 */
96
97void scan_cpu(PlatformInfo_t *p)
98{
99const char*newratio, *newfsb;
100intlen, myfsb, i;
101uint64_ttscFrequency, fsbFrequency, cpuFrequency, fsbi;
102uint64_tmsr, flex_ratio = 0;
103uint32_ttms, ida, max_ratio, min_ratio;
104uint8_tbus_ratio_max, maxdiv, bus_ratio_min, currdiv;
105boolfix_fsb, did, core_i, turbo, isatom, fsbad;
106
107max_ratio = min_ratio = myfsb = bus_ratio_max = maxdiv = bus_ratio_min = currdiv = i = 0;
108
109/* get cpuid values */
110for( ; i <= 3; i++)
111{
112do_cpuid(i, p->CPU.CPUID[i]);
113}
114
115do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);
116do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);
117if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {
118do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);
119}
120#if DEBUG_CPU
121{
122inti;
123printf("CPUID Raw Values:\n");
124for (i=0; i<CPUID_MAX; i++) {
125printf("%02d: %08x-%08x-%08x-%08x\n", i,
126p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],
127p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);
128}
129}
130#endif
131p->CPU.Vendor= p->CPU.CPUID[CPUID_0][1];
132p->CPU.Signature= p->CPU.CPUID[CPUID_1][0];
133p->CPU.Stepping= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);
134p->CPU.Model= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);
135p->CPU.Type= bitfield(p->CPU.CPUID[CPUID_1][0], 13, 12);
136p->CPU.Family= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);
137p->CPU.ExtModel= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);
138p->CPU.ExtFamily= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);
139p->CPU.NoThreads= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);
140p->CPU.NoCores= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;
141
142p->CPU.Model += (p->CPU.ExtModel << 4);
143
144/* get brand string (if supported) */
145/* Copyright: from Apple's XNU cpuid.c */
146if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {
147uint32_treg[4];
148 char str[128], *s;
149/*
150 * The brand string 48 bytes (max), guaranteed to
151 * be NUL terminated.
152 */
153do_cpuid(0x80000002, reg);
154bcopy((char *)reg, &str[0], 16);
155do_cpuid(0x80000003, reg);
156bcopy((char *)reg, &str[16], 16);
157do_cpuid(0x80000004, reg);
158bcopy((char *)reg, &str[32], 16);
159for (s = str; *s != '\0'; s++) {
160if (*s != ' ') break;
161}
162
163strlcpy(p->CPU.BrandString,s, sizeof(p->CPU.BrandString));
164
165if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, min(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {
166 /*
167 * This string means we have a firmware-programmable brand string,
168 * and the firmware couldn't figure out what sort of CPU we have.
169 */
170 p->CPU.BrandString[0] = '\0';
171 }
172}
173
174/* setup features */
175p->CPU.Features |= (CPU_FEATURE_MMX | CPU_FEATURE_SSE | CPU_FEATURE_SSE2 | CPU_FEATURE_MSR | CPU_FEATURE_APIC | CPU_FEATURE_TM1 | CPU_FEATURE_ACPI) & p->CPU.CPUID[CPUID_1][3];
176p->CPU.Features |= (CPU_FEATURE_SSE3 | CPU_FEATURE_SSE41 | CPU_FEATURE_SSE42 | CPU_FEATURE_EST | CPU_FEATURE_TM2 | CPU_FEATURE_SSSE3 | CPU_FEATURE_xAPIC) & p->CPU.CPUID[CPUID_1][2];
177p->CPU.Features |= (CPU_FEATURE_EM64T | CPU_FEATURE_XD) & p->CPU.CPUID[CPUID_81][3];
178p->CPU.Features |= (CPU_FEATURE_LAHF) & p->CPU.CPUID[CPUID_81][2];
179
180
181//if ((CPU_FEATURE_HTT & p->CPU.CPUID[CPUID_1][3]) != 0) {
182if (p->CPU.NoThreads > p->CPU.NoCores) {
183p->CPU.Features |= CPU_FEATURE_HTT;
184}
185
186
187tscFrequency = measure_tsc_frequency();
188fsbFrequency = 0;
189cpuFrequency = 0;
190fsbi = 0;
191fix_fsb = false;
192did = false;
193core_i = false;
194turbo = false;
195isatom = false;
196fsbad = false;
197
198if ((p->CPU.Vendor == 0x756E6547 /* Intel */) && ((p->CPU.Family == 0x06) || (p->CPU.Family == 0x0f)))
199{
200verbose("CPU: ");
201int tjmax = 0;
202msr = rdmsr64(MSR_IA32_PLATFORM_ID);
203if((((msr >> 50) & 0x01) == 1) || (rdmsr64(0x17) & (1<<28)))
204{
205p->CPU.Features |= CPU_FEATURE_MOBILE;
206verbose("Mobile ");
207}
208verbose("%s\n", p->CPU.BrandString);
209
210if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03))
211{
212if (p->CPU.Family == 0x06)
213{
214int intelCPU = p->CPU.Model;
215int Stepp = p->CPU.Stepping;
216int bus;
217
218switch (intelCPU)
219{
220case 0xc:// Core i7 & Atom
221if (strstr(p->CPU.BrandString, "Atom")) goto teleport1;
222case 0x1a:// Core i7 LGA1366, Xeon 5500, "Bloomfield", "Gainstown", 45nm
223case 0x1e:// Core i7, i5 LGA1156, "Clarksfield", "Lynnfield", "Jasper", 45nm
224case 0x1f:// Core i7, i5, Nehalem
225case 0x25:// Core i7, i5, i3 LGA1156, "Westmere", "Clarkdale", "Arrandale", 32nm
226case 0x2a:// Sandy Bridge, 32nm
227case 0x2c:// Core i7 LGA1366, Six-core, "Westmere", "Gulftown", 32nm
228case 0x2e:// Core i7, Nehalem-Ex Xeon, "Beckton"
229case 0x2f:// Core i7, Nehalem-Ex Xeon, "Eagleton"
230core_i = true;
231tjmax = (rdmsr64(MSR_THERMAL_TARGET) >> 16) & 0xff;
232msr = rdmsr64(MSR_PLATFORM_INFO);
233bus_ratio_max = (msr >> 8) & 0xff;
234bus_ratio_min = (msr >> 40) & 0xff; //valv: not sure about this one (Remarq.1)
235verbose("CPU: Flex-Ratio = %d ", bus_ratio_max);
236min_ratio = bus_ratio_min * 10;
237msr = rdmsr64(MSR_FLEX_RATIO);
238if ((msr >> 16) & 0x01)
239{
240flex_ratio = (msr >> 8) & 0xff;
241verbose(">> %d", flex_ratio);
242if(bus_ratio_max > flex_ratio) bus_ratio_max = flex_ratio;
243}
244verbose("\n");
245if(bus_ratio_max) fsbFrequency = (tscFrequency / bus_ratio_max);
246
247//valv: Turbo Ratio Limit
248if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
249{
250turbo = true;
251msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
252
253p->CPU.Tone = (msr >> 0) & 0xff;
254p->CPU.Ttwo = (msr >> 8) & 0xff;
255p->CPU.Tthr = (msr >> 16) & 0xff;
256p->CPU.Tfor = (msr >> 24) & 0xff;
257
258cpuFrequency = bus_ratio_max * fsbFrequency;
259max_ratio = bus_ratio_max * 10;
260}
261else cpuFrequency = tscFrequency;
262
263if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->bootConfig)) && (len <= 4))
264{
265max_ratio = atoi(newratio);
266max_ratio = (max_ratio * 10);
267if (len >= 3) max_ratio = (max_ratio + 5);
268
269verbose("Bus-Ratio: min=%d%s, max=%d%s\n", bus_ratio_min, bus_ratio_max);
270
271// extreme overclockers may love 320 ;)
272if ((max_ratio >= min_ratio) && (max_ratio <= 320))
273{
274cpuFrequency = (fsbFrequency * max_ratio) / 10;
275if (len >= 3) maxdiv = 1;
276else maxdiv = 0;
277verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %s]\n", myfsb, newratio);
278}
279else max_ratio = (bus_ratio_max * 10);
280}
281//valv: to be uncommented if Remarq.1 didn't stick
282/*if(bus_ratio_max > 0) bus_ratio = flex_ratio;*/
283p->CPU.MaxRatio = max_ratio;
284p->CPU.MinRatio = min_ratio;
285
286//fsbi = fsbFrequency;
287if(getIntForKey(kForceFSB, &myfsb, &bootInfo->bootConfig)) goto forcefsb;
288break;
289case 0xd:// Pentium M, Dothan, 90nm
290case 0xe:// Core Duo/Solo, Pentium M DC
291teleport1:
292msr = rdmsr64(MSR_IA32_EXT_CONFIG);
293if(msr & (1 << 30)) tjmax = 85;
294goto teleport2;
295case 0xf:// Core Xeon, Core 2 DC, 65nm
296switch (Stepp)
297{
298case 0x2:
299tjmax = 95;
300break;
301case 0x6:
302if (p->CPU.NoCores = 2) tjmax = 80;
303if (p->CPU.NoCores = 4) tjmax = 90;
304else tjmax = 85;
305break;
306case 0xb:
307tjmax = 90;
308break;
309case 0xd:
310default:
311msr = rdmsr64(MSR_IA32_EXT_CONFIG);
312if(msr & (1 << 30)) tjmax = 85;
313break;
314}
315case 0x1c:// Atom :)
316switch (Stepp)
317{
318case 0xa:
319tjmax = 100;
320break;
321case 0x2:
322default:
323tjmax = 90;
324break;
325}
326case 0x17:// Core 2 Duo/Extreme, Xeon, 45nm
327switch (Stepp)
328{
329case 0x6:// Mobile Core2 Duo
330tjmax = 104;
331break;
332case 0xa:// Mobile Centrino 2
333tjmax = 105;
334break;
335default:
336if (platformCPUFeature(CPU_FEATURE_MOBILE)) tjmax = 105;
337break;
338}
339case 0x16:// Celeron, Core 2 SC, 65nm
340case 0x27:// Atom Lincroft, 45nm
341teleport2:
342core_i = false;
343//valv: todo: msr_therm2_ctl (0x19d) bit 16 (mode of automatic thermal monitor): 0=tm1, 1=tm2
344//also, if bit 3 of misc_enable is cleared the above would have no effect
345if (strstr(p->CPU.BrandString, "Atom"))
346isatom = true;
347if(!isatom && (platformCPUFeature(CPU_FEATURE_TM1)))
348{
349msr_t msr32;
350msr32 = rdmsr(MSR_IA32_MISC_ENABLE);
351bool tmfix = false;
352getBoolForKey(kFixTM, &tmfix, &bootInfo->bootConfig);
353if(tmfix)
354{
355//thermally-initiated on-die modulation of the stop-clock duty cycle
356if(!(rdmsr64(MSR_IA32_MISC_ENABLE) & (1 << 3))) msr32.lo |= (1 << 3);
357verbose("CPU: Thermal Monitor: TM, ");
358
359//BIOS must enable this feature if the TM2 feature flag (CPUID.1:ECX[8]) is set
360if(platformCPUFeature(CPU_FEATURE_TM2))
361{
362//thermally-initiated frequency transitions
363msr32.lo |= (1 << 13);
364verbose("TM2, ");
365}
366msr32.lo |= (1 << 17);
367verbose("PROCHOT, ");
368msr32.lo |= (1 << 10);
369verbose("FERR\n");
370}
371bool oem_ssdt, tmpval;
372oem_ssdt = false;
373
374oem_ssdt = getBoolForKey(kOEMSSDT, &tmpval, &bootInfo->bootConfig)&&tmpval;
375if(oem_ssdt)
376{
377bool c2e, c4e, hc4e;
378c2e = c4e = hc4e = false;
379
380getBoolForKey(kC2EEnable, &c2e, &bootInfo->bootConfig);
381if(c2e) msr32.lo |= (1 << 26);
382
383getBoolForKey(kC4EEnable, &c4e, &bootInfo->bootConfig);
384if((c4e) && platformCPUFeature(CPU_FEATURE_MOBILE)) msr32.hi |= (1 << (32 - 32));
385getBoolForKey(kHardC4EEnable, &hc4e, &bootInfo->bootConfig);
386if((hc4e) && platformCPUFeature(CPU_FEATURE_MOBILE)) msr32.hi |= (1 << (33 - 32));
387if(c2e || c4e || hc4e) tmfix = true;
388}
389
390if(tmfix)
391{
392msr32.hi |= (1 << (36 - 32)); // EMTTM
393wrmsr(MSR_IA32_MISC_ENABLE, msr32);
394}
395if(tmfix)
396{
397msr32 = rdmsr(PIC_SENS_CFG);
398msr32.lo |= (1 << 21);
399wrmsr(PIC_SENS_CFG, msr32);
400}
401}
402
403if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
404{
405wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
406delay(1);
407did = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
408}
409getBoolForKey(kFixFSB, &fix_fsb, &bootInfo->bootConfig);
410if(fix_fsb)
411{
412msr = rdmsr64(MSR_FSB_FREQ);
413bus = (msr >> 0) & 0x7;
414if(p->CPU.Model == 0xd && bus == 0)
415{
416fsbFrequency = 100000000;
417myfsb = 100;
418}
419else if(p->CPU.Model == 0xe && p->CPU.ExtModel == 1) goto ratio;
420else
421{
422switch (bus)
423{
424case 0:
425fsbFrequency = 266666667;
426myfsb = 266;
427break;
428case 1:
429fsbFrequency = 133333333;
430myfsb = 133;
431break;
432case 3:
433fsbFrequency = 166666667;
434myfsb = 166;
435break;
436case 4:
437fsbFrequency = 333333333;
438myfsb = 333;
439break;
440case 5:
441fsbFrequency = 100000000;
442myfsb = 100;
443break;
444case 6:
445fsbFrequency = 400000000;
446myfsb = 400;
447break;
448case 2:
449default:
450fsbFrequency = 200000000;
451myfsb = 200;
452break;
453}
454}
455uint64_t minfsb = 182000000, maxfsb = 185000000;
456if(((fsbFrequency > minfsb) && (fsbFrequency < maxfsb)) || !fsbFrequency)
457{
458fsbFrequency = 200000000;
459fsbad = true;
460}
461goto ratio;
462}
463case 0x1d:// Xeon MP MP 7400
464// for 0x2a & 0x2b turbo is true;
465//case 0x2a:// SNB
466//case 0x2b:// SNB Xeon
467default:
468if(getIntForKey(kForceFSB, &myfsb, &bootInfo->bootConfig))
469{
470forcefsb:
471switch(myfsb)
472{
473case 133:
474fsbFrequency = 133333333;
475break;
476case 166:
477fsbFrequency = 166666667;
478break;
479case 233:
480fsbFrequency = 233333333;
481break;
482case 266:
483fsbFrequency = 266666667;
484break;
485case 333:
486fsbFrequency = 333333333;
487break;
488case 100:
489case 200:
490case 400:
491fsbFrequency = (myfsb * 1000000);
492break;
493default:
494getValueForKey(kForceFSB, &newfsb, &len, &bootInfo->bootConfig);
495if((len <= 3) && (myfsb < 400))
496{
497fsbFrequency = (myfsb * 1000000);
498verbose("Specified FSB: %dMhz. Assuming you know what you 're doing !\n", myfsb);
499}
500else if(core_i) fsbFrequency = 133333333;
501else fsbFrequency = 200000000;
502break;
503}
504if(core_i)
505{
506cpuFrequency = (fsbFrequency * max_ratio) / 10;
507verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %s]\n", myfsb, newratio);
508break;
509}
510fix_fsb = true;
511}
512goto ratio;
513break;
514}
515}
516else
517{
518ratio:
519msr = rdmsr64(MSR_IA32_PERF_STATUS);
520maxdiv = (msr >> 46) & 0x01;
521//valv: this seems to be bit 15 instead of 14.
522currdiv = (msr >> 15) & 0x01;
523uint8_t XE = (msr >> 31) & 0x01;
524
525msr_t msr32;
526msr32 = rdmsr(MSR_IA32_PERF_STATUS);
527bus_ratio_min = (msr32.lo >> 24) & 0x1f;
528min_ratio = bus_ratio_min * 10;
529if(currdiv) min_ratio = min_ratio + 5;
530
531if(XE || (p->CPU.Family == 0x0f)) bus_ratio_max = (msr32.hi >> (40-32)) & 0x1f;
532else bus_ratio_max = ((rdmsr64(MSR_IA32_PLATFORM_ID) >> 8) & 0x1f);
533/* On lower models, currcoef defines TSC freq */
534if (((p->CPU.Family == 0x06) && (p->CPU.Model < 0x0e)) && (p->CPU.Family != 0x0f)) bus_ratio_max = bus_ratio_min;
535// bad hack! Could force a value relying on kpstates, but I fail to see its benefits.
536if(bus_ratio_min == 0) bus_ratio_min = bus_ratio_max;
537
538if(p->CPU.Family == 0x0f)
539{
540getBoolForKey(kFixFSB, &fix_fsb, &bootInfo->bootConfig);
541if(fix_fsb)
542{
543msr = rdmsr64(MSR_EBC_FREQUENCY_ID);
544int bus = (msr >> 16) & 0x7;
545switch (bus)
546{
547case 0:
548fsbFrequency = 266666667;
549myfsb = 266;
550break;
551case 1:
552fsbFrequency = 133333333;
553myfsb = 133;
554break;
555case 3:
556fsbFrequency = 166666667;
557myfsb = 166;
558break;
559case 2:
560default:
561fsbFrequency = 200000000;
562myfsb = 200;
563break;
564}
565uint64_t minfsb = 183000000, maxfsb = 185000000;
566if (((fsbFrequency > minfsb) && (fsbFrequency < maxfsb)) || (!fsbFrequency))
567{
568fsbFrequency = 200000000;
569fsbad = true;
570}
571}
572}
573
574if(fix_fsb)
575{
576if (bus_ratio_max)
577{
578if (maxdiv) fsbi = ((tscFrequency * 2) / ((bus_ratio_max * 2) + 1));
579else fsbi = (tscFrequency / bus_ratio_max);
580}
581ratio_gn:
582if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->bootConfig)) && (len <= 4))
583{
584max_ratio = atoi(newratio);
585max_ratio = (max_ratio * 10);
586if (len >= 3) max_ratio = (max_ratio + 5);
587
588verbose("Bus-Ratio defaults: min=%d%s, max=%d%s\n", bus_ratio_min, currdiv ? ".5" : "", bus_ratio_max, maxdiv ? ".5" : "");
589if ((max_ratio >= min_ratio) && (max_ratio < 200))
590{
591cpuFrequency = (fsbFrequency * max_ratio) / 10;
592if (len >= 3) maxdiv = 1;
593else maxdiv = 0;
594verbose("Sticking with [FSB: %dMhz, Bus-Ratio: %s]\n", myfsb, newratio);
595}
596else
597{
598printf("Bus-Ratio: Lowest allowed = %d%s. ", bus_ratio_min, currdiv ? ".5" : "");
599goto ratio_vldt;
600}
601}
602else
603{
604ratio_vldt:
605if (maxdiv)
606{
607cpuFrequency = ((fsbFrequency * ((bus_ratio_max * 2) + 1)) / 2);
608max_ratio = (bus_ratio_max * 10) + 5;
609}
610else
611{
612cpuFrequency = (fsbFrequency * bus_ratio_max);
613max_ratio = bus_ratio_max * 10;
614}
615verbose("CPU: Sticking with: [FSB: %dMhz, Bus-Ratio: %d%s] %s\n", myfsb, bus_ratio_max, maxdiv ? ".5" : "", newratio ? "instead" : "");
616}
617}
618else
619{
620if (bus_ratio_max)
621{
622if (maxdiv)
623{
624fsbFrequency = ((tscFrequency * 2) / ((bus_ratio_max * 2) + 1));
625max_ratio = ((bus_ratio_max * 10) + 5);
626}
627else
628{
629fsbFrequency = (tscFrequency / bus_ratio_max);
630max_ratio = (bus_ratio_max * 10);
631}
632
633myfsb = (fsbFrequency / 1000000);
634if (getValueForKey(kbusratio, &newratio, &len, &bootInfo->bootConfig)) goto ratio_gn;
635else cpuFrequency = ((fsbFrequency * max_ratio) / 10);
636
637DBG("max: %d%s current: %d%s\n", bus_ratio_max, maxdiv ? ".5" : "", bus_ratio_min, currdiv ? ".5" : "");
638}
639}
640p->CPU.MaxRatio = max_ratio;
641p->CPU.MinRatio = min_ratio;
642}
643}
644
645// on-die sensor
646if (p->CPU.CPUID[CPUID_0][0] >= 0x6)
647{
648// Highest Basic Functions Number
649do_cpuid(6, p->CPU.CPUID[CPUID_81]);
650tms = bitfield(p->CPU.CPUID[CPUID_81][0], 0, 0);
651ida = bitfield(p->CPU.CPUID[CPUID_81][0], 1, 1);
652if(tms != 0)
653{
654int temp, utjmax;
655if (tjmax == 0) tjmax = 100;
656if((getIntForKey(kTjmax, &utjmax, &bootInfo->bootConfig)) && ((70 <= utjmax) && (utjmax <= 110))) tjmax = utjmax;
657msr = rdmsr64(MSR_THERMAL_STATUS);
658//if ((msr & 0x3) == 0x3)
659if (((msr >> 31) & 0x1) == 1)
660{
661temp = tjmax - ((msr >> 16) & 0x7F);
662verbose("CPU: Tjmax ~ %d°C Temperature= ~ %d°C\n", tjmax, temp);
663}
664else temp = -1;
665}
666if(ida == 0)
667{
668verbose("CPU: Attempting to enable IDA ");
669msr_t msr;
670msr = rdmsr(MSR_IA32_MISC_ENABLE);
671msr.hi |= (0 << (38-32));
672wrmsr(MSR_IA32_MISC_ENABLE, msr);
673delay(1);
674if(bitfield(p->CPU.CPUID[CPUID_81][0], 1, 1) == 0) verbose("Failed!\n");
675else verbose("Succeded!\n");
676}
677else verbose("CPU: IDA: Enabled!\n");
678}
679}
680//#if 0
681// valv: work in progress. Most of this code is going to be moved when ready
682else if(p->CPU.Vendor == 0x68747541 /* AMD */ && p->CPU.Family == 0x0f)
683{
684verbose("CPU: ");
685// valv: very experimental mobility check
686if (p->CPU.CPUID[0x80000000][0] >= 0x80000007)
687{
688uint32_t amo, const_tsc;
689do_cpuid(0x80000007, p->CPU.CPUID[CPUID_MAX]);
690amo = bitfield(p->CPU.CPUID[CPUID_MAX][0], 6, 6);
691const_tsc = bitfield(p->CPU.CPUID[CPUID_MAX][3], 8, 8);
692// valv: p-state support verification
693//uint32_t pstate_support = bitfield(p->CPU.CPUID[CPUID_MAX][3], 2, 1);
694//if(pstate_support != 0) verbose("supproted p-state transition\n")
695
696if (const_tsc != 0) verbose("Constant TSC!\n");
697if (amo == 1)
698{
699p->CPU.Features |= CPU_FEATURE_MOBILE;
700if (!strstr(p->CPU.BrandString, "obile")) verbose("Mobile ");
701}
702}
703//valv: 2nd attemp; just in case
704if (!platformCPUFeature(CPU_FEATURE_MOBILE))
705{
706if (strstr(p->CPU.BrandString, "obile"))
707{
708p->CPU.Features |= CPU_FEATURE_MOBILE;
709}
710}
711verbose("%s\n", p->CPU.BrandString);
712
713if(p->CPU.ExtFamily == 0x00 /* K8 */)
714{
715// valv: this section needs some work
716msr = rdmsr64(K8_FIDVID_STATUS);
717bus_ratio_max = bitfield(msr, 21, 16);
718//bus_ratio_max = (msr & 0x3f) / 2 + 4;
719bus_ratio_min = bitfield(msr, 13, 8);
720currdiv = (msr & 0x01) * 2;
721if (bus_ratio_max)
722{
723if (currdiv)
724{
725fsbFrequency = ((tscFrequency * currdiv) / bus_ratio_max); // ?
726DBG("%d.%d\n", bus_ratio_max / currdiv, ((bus_ratio_max % currdiv) * 100) / currdiv);
727}
728else
729{
730fsbFrequency = (tscFrequency / bus_ratio_max);
731DBG("%d\n", bus_ratio_max);
732}
733//fsbFrequency = (tscFrequency / bus_ratio_max); // ?
734cpuFrequency = tscFrequency; // ?
735}
736}
737
738else if(p->CPU.ExtFamily >= 0x01 /* K10+ */)
739{
740msr = rdmsr64(K10_COFVID_STATUS);
741currdiv = (2 << ((msr >> 6) & 0x07));
742msr = rdmsr64(AMD_10H_11H_CONFIG);
743bus_ratio_max = ((msr) & 0x3F);
744//verbose("max_multi: %d\n", bus_ratio_max);
745
746/*msr_t divmsr;
747divmsr = rdmsr(AMD_10H_11H_CONFIG);
748maxdiv = (divmsr.hi >> 0x08) & 0x01;
749verbose("maxdiv: %d, currdiv: %d\n", maxdiv, currdiv);*/
750
751if(p->CPU.ExtFamily == 0x01)
752cpuFrequency = 100 * (bus_ratio_max + 0x10);
753else
754cpuFrequency = 100 * (bus_ratio_max + 0x08);
755
756uint32_t minFreq = cpuFrequency / (1 << currdiv);
757
758uint8_t maxrtio = (cpuFrequency / 20);
759p->CPU.MaxRatio = maxrtio;
760
761fsbFrequency = ((tscFrequency / 100000) / maxrtio);
762verbose("fsb: %d\n", fsbFrequency);
763
764if(maxrtio == ((bus_ratio_max * 10) - 5))
765{
766verbose("multi: max:%d.5, ", (bus_ratio_max - 1));
767maxdiv = 1;
768}
769else if(maxrtio == ((bus_ratio_max - 1) * 10))
770{
771verbose("multi: max:%d, min:", (bus_ratio_max - 1));
772maxdiv = 0;
773}
774
775bus_ratio_min = (minFreq / fsbFrequency);
776verbose("%d", bus_ratio_min);
777while(minFreq < 800)
778{
779bus_ratio_min = bus_ratio_min + 1; // bus_ratio_min++; ???
780verbose(" >> %d", bus_ratio_min);
781}
782verbose("\n");
783
784struct hwpstate
785{
786uint32_tfreq;/* CPU clock in Mhz. */
787uint32_tvolts;/* Voltage in mV. */
788uint32_tpower;/* Power consumed in mW. */
789uint8_tlat;/* Transition latency in us. */
790uint8_tpstate_id;/* P-State id */
791};
792
793struct hwpstate state[32];
794int max_state, i,/* did,*/ vid;
795uint8_t fid;
796msr = rdmsr64(MSR_AMD_10H_11H_LIMIT);
797max_state = 1 + (((msr) >> 4) & 0x7);
798
799for(i=0; i<max_state; i++)
800{
801msr = rdmsr64(AMD_10H_11H_CONFIG + i);
802//msr_t didmsr;
803//didmsr = rdmsr(AMD_10H_11H_CONFIG + i);
804if ((msr & ((uint64_t)1 << 63)) != ((uint64_t)1 << 63)) verbose("Invalid MSR!\n");
805else
806{
807//did = (didmsr.hi >> 0x08) & 0x01;
808if(i == 0)
809{
810//maxdiv = did;
811fid = p->CPU.MaxRatio;
812state[i].freq = ((fid * fsbFrequency) / 10);
813fid = (fid / 10);
814}
815else
816{
817fid = bitfield(msr, 5, 0);
818state[i].freq = (fid * fsbFrequency);
819}
820
821if(i == (max_state -1))
822{
823fid = bus_ratio_min;
824state[i].freq = (fid * fsbFrequency);
825}
826
827vid = bitfield(msr, 15, 9);
828
829if(i == 0) verbose("P-State %d: Frequency: %d, Multiplier: %d%s, vid: %d\n", i, state[i].freq, fid, maxdiv ? ".5" : "", vid);
830else if((state[i].freq > state[i+1].freq) || (state[i].freq < 800)) verbose("P-State %d: Removed!", i);
831else verbose("P-State %d: Frequency: %d, Multiplier: %d, vid: %d\n", i, state[i].freq, fid, vid);
832state[i].pstate_id = i;
833// valv: zeroed for now
834state[i].volts = 0;
835state[i].power = 0;
836state[i].lat = 0;
837}
838}
839fsbFrequency = (fsbFrequency * 1000000);
840cpuFrequency = (state[0].freq * 1000000);
841}
842
843p->CPU.MinRatio = bus_ratio_min * 10;
844}
845else if(p->CPU.Vendor == 0x746e6543 /* CENTAUR */ && p->CPU.Family == 6) //valv: partial!
846{
847msr = rdmsr64(MSR_EBL_CR_POWERON);
848int bus = (msr >> 18) & 0x3;
849switch (bus)
850{
851case 1:
852fsbFrequency = 133333333;
853break;
854case 2:
855fsbFrequency = 200000000;
856break;
857case 3:
858fsbFrequency = 166666667;
859break;
860case 0:
861default:
862fsbFrequency = 100000000;
863break;
864}
865msr_t msr;
866msr = rdmsr(MSR_IA32_PERF_STATUS);
867bus_ratio_min = (msr.lo >> 24) & 0x1f;
868min_ratio = bus_ratio_min * 10;
869bus_ratio_max = (msr.hi >> (40-32)) & 0x1f;
870max_ratio = bus_ratio_max * 10;
871cpuFrequency = ((fsbFrequency * max_ratio) / 10);
872}
873
874if (!fsbFrequency)
875{
876fsbFrequency = (DEFAULT_FSB * 1000);
877cpuFrequency = tscFrequency;
878DBG("0 ! using the default value for FSB !\n");
879}
880
881//#endif
882
883p->CPU.MaxDiv = maxdiv;
884p->CPU.CurrDiv = currdiv;
885p->CPU.TSCFrequency = tscFrequency;
886p->CPU.FSBFrequency = fsbFrequency;
887p->CPU.CPUFrequency = cpuFrequency;
888p->CPU.ISerie = false;
889p->CPU.Turbo = false;
890
891if(!fsbad) p->CPU.FSBIFrequency = fsbFrequency;
892else p->CPU.FSBIFrequency = fsbi;
893
894if (platformCPUFeature(CPU_FEATURE_EST))
895{
896msr_t msr32;
897msr32 = rdmsr(MSR_IA32_MISC_ENABLE);
898if (!(rdmsr64(MSR_IA32_MISC_ENABLE) & (1 << 16)))
899{//valv: we can also attempt to enable
900msr32.lo |= (1 << 16);
901// Lock till next reset!
902msr32.lo |= (1 << 20);
903wrmsr(MSR_IA32_MISC_ENABLE, msr32);
904delay(1);
905if(rdmsr64(MSR_IA32_MISC_ENABLE) & (1 << 16))
906{
907p->CPU.EST = 1;
908verbose("CPU: EIST Successfully Enabled!\n");
909}
910else
911{
912p->CPU.EST = 0;
913verbose("CPU: EIST couldn't be enabled!\n");
914}
915}
916
917else p->CPU.EST = 1;
918}
919
920if(core_i) p->CPU.ISerie = true;
921DBG("CPU: Vendor/Family/ExtFamily: 0x%x/0x%x/0x%x\n", p->CPU.Vendor, p->CPU.Family, p->CPU.ExtFamily);
922DBG("CPU: Model/ExtModel/Stepping: 0x%x/0x%x/0x%x\n", p->CPU.Model, p->CPU.ExtModel, p->CPU.Stepping);
923DBG("CPU: Multipliers x10: max=%d, min=%d\n", p->CPU.MaxRatio, p->CPU.MinRatio);
924if(turbo)
925{
926DBG("Turbo Ratio: %d/%d/%d/%d\n", p->CPU.Tone, p->CPU.Ttwo, p->CPU.Tthr, p->CPU.Tfor);
927p->CPU.Turbo = true;
928}
929DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv, p->CPU.CurrDiv);
930DBG("CPU: TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
931DBG("CPU: CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
932DBG("CPU: FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);
933if(did)
934{
935p->CPU.SLFM = did;
936DBG("CPU: SLFM: %d\n", p->CPU.SLFM);
937}
938if(platformCPUFeature(CPU_FEATURE_EST))
939DBG("CPU: Enhanced SpeedStep: %d\n", p->CPU.EST);
940DBG("CPU: NoCores/NoThreads: %d/%d\n", p->CPU.NoCores, p->CPU.NoThreads);
941DBG("CPU: Features: 0x%08x\n", p->CPU.Features);
942}
943

Archive Download this file

Revision: 705