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, amo, max_ratio, min_ratio;
104uint8_tbus_ratio_max, maxdiv, bus_ratio_min, currdiv;
105boolfix_fsb, did, core_i, turbo;
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;
195
196if ((p->CPU.Vendor == 0x756E6547 /* Intel */) && ((p->CPU.Family == 0x06) || (p->CPU.Family == 0x0f)))
197{
198verbose("CPU: ");
199int tjmax = 0;
200msr = rdmsr64(MSR_IA32_PLATFORM_ID);
201if((((msr >> 50) & 0x01) == 1) || (rdmsr64(0x17) & (1<<28)))
202{
203p->CPU.Features |= CPU_FEATURE_MOBILE;
204verbose("Mobile ");
205}
206verbose("%s\n", p->CPU.BrandString);
207
208if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || (p->CPU.Family == 0x0f && p->CPU.Model >= 0x03))
209{
210if (p->CPU.Family == 0x06)
211{
212int intelCPU = p->CPU.Model;
213int Stepp = p->CPU.Stepping;
214int bus;
215
216switch (intelCPU)
217{
218case 0x1a:// Core i7 LGA1366, Xeon 5500, "Bloomfield", "Gainstown", 45nm
219case 0x1e:// Core i7, i5 LGA1156, "Clarksfield", "Lynnfield", "Jasper", 45nm
220case 0x1f:// Core i7, i5, Nehalem
221case 0x25:// Core i7, i5, i3 LGA1156, "Westmere", "Clarkdale", "Arrandale", 32nm
222case 0x2c:// Core i7 LGA1366, Six-core, "Westmere", "Gulftown", 32nm
223case 0x2e:// Core i7, Nehalem-Ex Xeon, "Beckton"
224case 0x2f:// Core i7, Nehalem-Ex Xeon, "Eagleton"
225core_i = true;
226tjmax = (rdmsr64(MSR_THERMAL_TARGET) >> 16) & 0xff;
227msr = rdmsr64(MSR_PLATFORM_INFO);
228bus_ratio_max = (msr >> 8) & 0xff;
229bus_ratio_min = (msr >> 40) & 0xff; //valv: not sure about this one (Remarq.1)
230verbose("CPU: Flex-Ratio = %d ", bus_ratio_max);
231min_ratio = bus_ratio_min * 10;
232msr = rdmsr64(MSR_FLEX_RATIO);
233if ((msr >> 16) & 0x01)
234{
235flex_ratio = (msr >> 8) & 0xff;
236verbose(">> %d", flex_ratio);
237if(bus_ratio_max > flex_ratio) bus_ratio_max = flex_ratio;
238}
239verbose("\n");
240if(bus_ratio_max) fsbFrequency = (tscFrequency / bus_ratio_max);
241
242//valv: Turbo Ratio Limit
243if ((intelCPU != 0x2e) && (intelCPU != 0x2f))
244{
245turbo = true;
246msr = rdmsr64(MSR_TURBO_RATIO_LIMIT);
247
248p->CPU.Tone = (msr >> 0) & 0xff;
249p->CPU.Ttwo = (msr >> 8) & 0xff;
250p->CPU.Tthr = (msr >> 16) & 0xff;
251p->CPU.Tfor = (msr >> 24) & 0xff;
252
253cpuFrequency = bus_ratio_max * fsbFrequency;
254max_ratio = bus_ratio_max * 10;
255}
256else cpuFrequency = tscFrequency;
257
258if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->bootConfig)) && (len <= 4))
259{
260max_ratio = atoi(newratio);
261max_ratio = (max_ratio * 10);
262if (len >= 3) max_ratio = (max_ratio + 5);
263
264verbose("Bus-Ratio: min=%d%s, max=%d%s\n", bus_ratio_min, bus_ratio_max);
265
266// extreme overclockers may love 320 ;)
267if ((max_ratio >= min_ratio) && (max_ratio <= 320))
268{
269cpuFrequency = (fsbFrequency * max_ratio) / 10;
270if (len >= 3) maxdiv = 1;
271else maxdiv = 0;
272verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %s]\n", myfsb, newratio);
273}
274else max_ratio = (bus_ratio_max * 10);
275}
276//valv: to be uncommented if Remarq.1 didn't stick
277/*if(bus_ratio_max > 0) bus_ratio = flex_ratio;*/
278p->CPU.MaxRatio = max_ratio;
279p->CPU.MinRatio = min_ratio;
280
281fsbi = fsbFrequency;
282if(getIntForKey(kForceFSB, &myfsb, &bootInfo->bootConfig)) goto forcefsb;
283break;
284case 0xd:// Pentium D; valv: is this the right place ?
285case 0xe:// Core Duo/Solo, Pentium M DC
286goto teleport;
287case 0xf:// Core Xeon, Core 2 DC, 65nm
288switch (Stepp)
289{
290case 0x2:
291tjmax = 95;
292break;
293case 0x6:
294if (p->CPU.NoCores = 2) tjmax = 80;
295if (p->CPU.NoCores = 4) tjmax = 90;
296else tjmax = 85;
297break;
298case 0xb:
299tjmax = 90;
300break;
301case 0xd:
302default:
303teleport:
304msr = rdmsr64(MSR_IA32_EXT_CONFIG);
305if(msr & (1 << 30)) tjmax = 85;
306break;
307}
308case 0x1c:// Atom :)
309switch (Stepp)
310{
311case 0xa:
312tjmax = 100;
313break;
314case 0x2:
315default:
316tjmax = 90;
317break;
318}
319case 0x17:// Core 2 Duo/Extreme, Xeon, 45nm
320switch (Stepp)
321{
322case 0x6:// Mobile Core2 Duo
323tjmax = 104;
324break;
325case 0xa:// Mobile Centrino 2
326tjmax = 105;
327break;
328default:
329if (platformCPUFeature(CPU_FEATURE_MOBILE)) tjmax = 105;
330break;
331}
332case 0x16:// Celeron, Core 2 SC, 65nm
333case 0x27:// Atom Lincroft, 45nm
334core_i = false;
335//valv: todo: msr_therm2_ctl (0x19d) bit 16 (mode of automatic thermal monitor): 0=tm1, 1=tm2
336//also, if bit 3 of misc_enable is cleared the above would have no effect
337if(platformCPUFeature(CPU_FEATURE_TM1))
338{
339msr_t msr32;
340msr32 = rdmsr(MSR_IA32_MISC_ENABLE);
341
342//thermally-initiated on-die modulation of the stop-clock duty cycle
343if(!(rdmsr64(MSR_IA32_MISC_ENABLE) & (1 << 3))) msr32.lo |= (1 << 3);
344verbose("CPU: Thermal Monitor: TM, ");
345
346//BIOS must enable this feature if the TM2 feature flag (CPUID.1:ECX[8]) is set
347if(platformCPUFeature(CPU_FEATURE_TM2))
348{
349//thermally-initiated frequency transitions
350msr32.lo |= (1 << 13);
351verbose("TM2, ");
352}
353msr32.lo |= (1 << 17);
354verbose("PROCHOT, ");
355msr32.lo |= (1 << 10);
356verbose("FERR\n");
357
358bool oem_ssdt, tmpval;
359oem_ssdt = false;
360
361oem_ssdt = getBoolForKey(kOEMSSDT, &tmpval, &bootInfo->bootConfig)&&tmpval;
362if(oem_ssdt)
363{
364bool c2e, c4e, hc4e;
365c2e = c4e = hc4e = false;
366
367getBoolForKey(kC2EEnable, &c2e, &bootInfo->bootConfig);
368if(c2e) msr32.lo |= (1 << 26);
369
370getBoolForKey(kC4EEnable, &c4e, &bootInfo->bootConfig);
371if((c4e) && platformCPUFeature(CPU_FEATURE_MOBILE)) msr32.hi |= (1 << (32 - 32));
372getBoolForKey(kHardC4EEnable, &hc4e, &bootInfo->bootConfig);
373if((hc4e) && platformCPUFeature(CPU_FEATURE_MOBILE)) msr32.hi |= (1 << (33 - 32));
374}
375
376msr32.hi |= (1 << (36 - 32)); // EMTTM
377
378wrmsr(MSR_IA32_MISC_ENABLE, msr32);
379
380msr32 = rdmsr(PIC_SENS_CFG);
381msr32.lo |= (1 << 21);
382wrmsr(PIC_SENS_CFG, msr32);
383}
384
385if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
386{
387wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
388delay(1);
389did = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
390}
391getBoolForKey(kFixFSB, &fix_fsb, &bootInfo->bootConfig);
392if(fix_fsb)
393{
394msr = rdmsr64(MSR_FSB_FREQ);
395bus = (msr >> 0) & 0x7;
396switch (bus)
397{
398case 0:
399fsbFrequency = 266666667;
400myfsb = 266;
401break;
402case 1:
403fsbFrequency = 133333333;
404myfsb = 133;
405break;
406case 3:
407fsbFrequency = 166666667;
408myfsb = 166;
409break;
410case 4:
411fsbFrequency = 333333333;
412myfsb = 333;
413break;
414case 5:
415fsbFrequency = 100000000;
416myfsb = 100;
417break;
418case 6:
419fsbFrequency = 400000000;
420myfsb = 400;
421break;
422case 2:
423default:
424fsbFrequency = 200000000;
425myfsb = 200;
426break;
427}
428uint64_t minfsb = 183000000, maxfsb = 185000000;
429if (((fsbFrequency > minfsb) && (fsbFrequency < maxfsb)) || (!fsbFrequency)) fsbFrequency = 200000000;
430goto ratio;
431}
432case 0x1d:// Xeon MP MP 7400
433// for 0x2a & 0x2b turbo is true;
434//case 0x2a:// SNB
435//case 0x2b:// SNB Xeon
436default:
437if(getIntForKey(kForceFSB, &myfsb, &bootInfo->bootConfig))
438{
439forcefsb:
440switch(myfsb)
441{
442case 133:
443fsbFrequency = 133333333;
444break;
445case 166:
446fsbFrequency = 166666667;
447break;
448case 233:
449fsbFrequency = 233333333;
450break;
451case 266:
452fsbFrequency = 266666667;
453break;
454case 333:
455fsbFrequency = 333333333;
456break;
457case 100:
458case 200:
459case 400:
460fsbFrequency = (myfsb * 1000000);
461break;
462default:
463getValueForKey(kForceFSB, &newfsb, &len, &bootInfo->bootConfig);
464if((len <= 3) && (myfsb < 400))
465{
466fsbFrequency = (myfsb * 1000000);
467verbose("Specified FSB: %dMhz. Assuming you know what you 're doing !\n", myfsb);
468}
469else if(core_i) fsbFrequency = 133333333;
470else fsbFrequency = 200000000;
471break;
472}
473if(core_i)
474{
475cpuFrequency = (fsbFrequency * max_ratio) / 10;
476verbose("Sticking with [BCLK: %dMhz, Bus-Ratio: %s]\n", myfsb, newratio);
477break;
478}
479fix_fsb = true;
480}
481goto ratio;
482break;
483}
484}
485else
486{
487ratio:
488msr = rdmsr64(MSR_IA32_PERF_STATUS);
489maxdiv = (msr >> 46) & 0x01;
490//valv: this seems to be bit 15 instead of 14.
491currdiv = (msr >> 15) & 0x01;
492uint8_t XE = (msr >> 31) & 0x01;
493
494msr_t msr;
495msr = rdmsr(MSR_IA32_PERF_STATUS);
496bus_ratio_min = (msr.lo >> 24) & 0x1f;
497min_ratio = bus_ratio_min * 10;
498if(currdiv) min_ratio = min_ratio + 5;
499
500if(XE) bus_ratio_max = (msr.hi >> (40-32)) & 0x1f;
501else bus_ratio_max = ((rdmsr64(MSR_IA32_PLATFORM_ID) >> 8) & 0x1f);
502
503if(fix_fsb)
504{
505if (bus_ratio_max)
506{
507if (maxdiv) fsbi = ((tscFrequency * 2) / ((bus_ratio_max * 2) + 1));
508else fsbi = (tscFrequency / bus_ratio_max);
509}
510ratio_gn:
511if ((getValueForKey(kbusratio, &newratio, &len, &bootInfo->bootConfig)) && (len <= 4))
512{
513max_ratio = atoi(newratio);
514max_ratio = (max_ratio * 10);
515if (len >= 3) max_ratio = (max_ratio + 5);
516
517verbose("Bus-Ratio defaults: min=%d%s, max=%d%s\n", bus_ratio_min, currdiv ? ".5" : "", bus_ratio_max, maxdiv ? ".5" : "");
518if ((max_ratio >= min_ratio) && (max_ratio < 200))
519{
520cpuFrequency = (fsbFrequency * max_ratio) / 10;
521if (len >= 3) maxdiv = 1;
522else maxdiv = 0;
523verbose("Sticking with [FSB: %dMhz, Bus-Ratio: %s]\n", myfsb, newratio);
524}
525else
526{
527printf("Bus-Ratio: Lowest allowed = %d%s. ", bus_ratio_min, currdiv ? ".5" : "");
528goto ratio_vldt;
529}
530}
531else
532{
533ratio_vldt:
534if (maxdiv)
535{
536cpuFrequency = ((fsbFrequency * ((bus_ratio_max * 2) + 1)) / 2);
537max_ratio = (bus_ratio_max * 10) + 5;
538}
539else
540{
541cpuFrequency = (fsbFrequency * bus_ratio_max);
542max_ratio = bus_ratio_max * 10;
543}
544verbose("CPU: Sticking with: [FSB: %dMhz, Bus-Ratio: %d%s] %s\n", myfsb, bus_ratio_max, maxdiv ? ".5" : "", newratio ? "instead" : "");
545}
546}
547else
548{
549/* On lower models, currcoef defines TSC freq */
550if (((p->CPU.Family == 0x06) && (p->CPU.Model < 0x0e)) && (p->CPU.Family != 0x0f)) bus_ratio_max = bus_ratio_min;
551
552if (bus_ratio_max)
553{
554if (maxdiv)
555{
556fsbFrequency = ((tscFrequency * 2) / ((bus_ratio_max * 2) + 1));
557max_ratio = ((bus_ratio_max * 10) + 5);
558}
559else
560{
561fsbFrequency = (tscFrequency / bus_ratio_max);
562max_ratio = (bus_ratio_max * 10);
563}
564
565myfsb = (fsbFrequency / 1000000);
566if (getValueForKey(kbusratio, &newratio, &len, &bootInfo->bootConfig)) goto ratio_gn;
567else cpuFrequency = ((fsbFrequency * max_ratio) / 10);
568
569DBG("max: %d%s current: %d%s\n", bus_ratio_max, maxdiv ? ".5" : "", bus_ratio_min, currdiv ? ".5" : "");
570}
571}
572p->CPU.MaxRatio = max_ratio;
573p->CPU.MinRatio = min_ratio;
574}
575}
576
577// on-die sensor
578if (p->CPU.CPUID[CPUID_0][0] >= 0x6)
579{
580// Highest Basic Functions Number
581do_cpuid(6, p->CPU.CPUID[CPUID_81]);
582tms = bitfield(p->CPU.CPUID[CPUID_81][0], 0, 0);
583ida = bitfield(p->CPU.CPUID[CPUID_81][0], 1, 1);
584if(tms != 0)
585{
586int temp, utjmax;
587if (tjmax == 0) tjmax = 100;
588if((getIntForKey(kTjmax, &utjmax, &bootInfo->bootConfig)) && ((70 <= utjmax) && (utjmax <= 110))) tjmax = utjmax;
589msr = rdmsr64(MSR_THERMAL_STATUS);
590//if ((msr & 0x3) == 0x3)
591if (((msr >> 31) & 0x1) == 1)
592{
593temp = tjmax - ((msr >> 16) & 0x7F);
594verbose("CPU: Tjmax ~ %d°C Temperature= ~ %d°C\n", tjmax, temp);
595}
596else temp = -1;
597}
598if(ida == 0)
599{
600verbose("CPU: Attempting to enable IDA ");
601msr_t msr;
602msr = rdmsr(MSR_IA32_MISC_ENABLE);
603msr.hi |= (0 << (38-32));
604wrmsr(MSR_IA32_MISC_ENABLE, msr);
605delay(1);
606if(bitfield(p->CPU.CPUID[CPUID_81][0], 1, 1) == 0) verbose("Failed!\n");
607else verbose("Succeded!\n");
608}
609else verbose("CPU: IDA: Enabled!\n");
610}
611}
612//#if 0
613else if(p->CPU.Vendor == 0x68747541 /* AMD */) // valv: work in progress
614{
615verbose("CPU: ");
616// valv: very experimental mobility check
617if (p->CPU.CPUID[0x80000000][0] >= 0x80000007)
618{
619do_cpuid(0x80000007, p->CPU.CPUID[CPUID_MAX]);
620amo = bitfield(p->CPU.CPUID[CPUID_MAX][0], 6, 6);
621if (amo == 1)
622{
623p->CPU.Features |= CPU_FEATURE_MOBILE;
624if (!strstr(p->CPU.BrandString, "obile")) verbose("Mobile ");
625}
626}
627//valv: 2nd attemp; just in case
628if (!platformCPUFeature(CPU_FEATURE_MOBILE))
629{
630if (strstr(p->CPU.BrandString, "obile"))
631{
632p->CPU.Features |= CPU_FEATURE_MOBILE;
633}
634}
635verbose("%s\n", p->CPU.BrandString);
636int amdCPU = p->CPU.Family;
637
638switch (amdCPU)
639{
640case 0x0f:
641if(p->CPU.ExtFamily == 0x00 /* K8 */)
642{
643msr = rdmsr64(K8_FIDVID_STATUS);
644bus_ratio_max = (msr & 0x3f) / 2 + 4;
645currdiv = (msr & 0x01) * 2;
646}
647else if(p->CPU.ExtFamily >= 0x01 /* K10+ */)
648{
649msr = rdmsr64(K10_COFVID_STATUS);
650if(p->CPU.ExtFamily == 0x01 /* K10 */)
651bus_ratio_max = (msr & 0x3f) + 0x10;
652else /* K11+ */
653bus_ratio_max = (msr & 0x3f) + 0x08;
654currdiv = (2 << ((msr >> 6) & 0x07));
655}
656
657p->CPU.MaxRatio = bus_ratio_max * 10;
658
659if (bus_ratio_max)
660{
661if (currdiv)
662{
663fsbFrequency = ((tscFrequency * currdiv) / bus_ratio_max); // ?
664DBG("%d.%d\n", bus_ratio_max / currdiv, ((bus_ratio_max % currdiv) * 100) / currdiv);
665}
666else
667{
668fsbFrequency = (tscFrequency / bus_ratio_max);
669DBG("%d\n", bus_ratio_max);
670}
671fsbFrequency = (tscFrequency / bus_ratio_max); // ?
672cpuFrequency = tscFrequency; // ?
673}
674break;
675case 0x10:// phenom
676msr = rdmsr64(AMD_10H_11H_CONFIG);
677bus_ratio_max = ((msr) & 0x3F);
678currdiv = (((msr) >> 6) & 0x07);
679cpuFrequency = 100 * (bus_ratio_max + 0x08) / (1 << currdiv);
680break;
681case 0x11:// shangai
682msr = rdmsr64(AMD_10H_11H_CONFIG);
683bus_ratio_max = ((msr) & 0x3F);
684currdiv = (((msr) >> 6) & 0x07);
685cpuFrequency = 100 * (bus_ratio_max + 0x10) / (1 << currdiv);
686break;
687}
688}
689
690if (!fsbFrequency)
691{
692fsbFrequency = (DEFAULT_FSB * 1000);
693cpuFrequency = tscFrequency;
694DBG("0 ! using the default value for FSB !\n");
695}
696
697//#endif
698
699p->CPU.MaxDiv = maxdiv;
700p->CPU.CurrDiv = currdiv;
701p->CPU.TSCFrequency = tscFrequency;
702p->CPU.FSBFrequency = fsbFrequency;
703p->CPU.CPUFrequency = cpuFrequency;
704p->CPU.ISerie = false;
705p->CPU.Turbo = false;
706
707if(fsbi == 0) p->CPU.FSBIFrequency = fsbFrequency;
708else p->CPU.FSBIFrequency = fsbi;
709
710if (platformCPUFeature(CPU_FEATURE_EST))
711{
712msr_t msr32;
713msr32 = rdmsr(MSR_IA32_MISC_ENABLE);
714if (!(rdmsr64(MSR_IA32_MISC_ENABLE) & (1 << 16)))
715{//valv: we can also attempt to enable
716msr32.lo |= (1 << 16);
717// Lock till next reset!
718msr32.lo |= (1 << 20);
719wrmsr(MSR_IA32_MISC_ENABLE, msr32);
720delay(1);
721if(rdmsr64(MSR_IA32_MISC_ENABLE) & (1 << 16))
722{
723p->CPU.EST = 1;
724verbose("CPU: EIST Successfully Enabled!\n");
725}
726else
727{
728p->CPU.EST = 0;
729verbose("CPU: EIST couldn't be enabled!\n");
730}
731}
732
733else p->CPU.EST = 1;
734}
735
736if(core_i) p->CPU.ISerie = true;
737DBG("CPU: Vendor/Family/ExtFamily: 0x%x/0x%x/0x%x\n", p->CPU.Vendor, p->CPU.Family, p->CPU.ExtFamily);
738DBG("CPU: Model/ExtModel/Stepping: 0x%x/0x%x/0x%x\n", p->CPU.Model, p->CPU.ExtModel, p->CPU.Stepping);
739DBG("CPU: Multipliers x10: max=%d, min=%d\n", p->CPU.MaxRatio, p->CPU.MinRatio);
740if(turbo)
741{
742DBG("Turbo Ratio: %d/%d/%d/%d\n", p->CPU.Tone, p->CPU.Ttwo, p->CPU.Tthr, p->CPU.Tfor);
743p->CPU.Turbo = true;
744}
745DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv, p->CPU.CurrDiv);
746DBG("CPU: TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);
747DBG("CPU: CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);
748DBG("CPU: FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);
749if(did)
750{
751p->CPU.SLFM = did;
752DBG("CPU: SLFM: %d\n", p->CPU.SLFM);
753}
754if(platformCPUFeature(CPU_FEATURE_EST))
755DBG("CPU: Enhanced SpeedStep: %d\n", p->CPU.EST);
756DBG("CPU: NoCores/NoThreads: %d/%d\n", p->CPU.NoCores, p->CPU.NoThreads);
757DBG("CPU: Features: 0x%08x\n", p->CPU.Features);
758}
759

Archive Download this file

Revision: 698