1 | /*␊ |
2 | * Copyright 2008 Islam Ahmed Zaid. All rights reserved. <azismed@gmail.com>␊ |
3 | * AsereBLN: 2009: cleanup and bugfix␊ |
4 | */␊ |
5 | ␊ |
6 | #include "libsaio.h"␊ |
7 | #include "platform.h"␊ |
8 | //#include "mem.h"␊ |
9 | #include "smbios_patcher.h"␊ |
10 | #include "cpu.h"␊ |
11 | ␊ |
12 | #ifndef DEBUG_CPU␊ |
13 | #define DEBUG_CPU 0␊ |
14 | #endif␊ |
15 | ␊ |
16 | #if DEBUG_CPU␊ |
17 | #define DBG(x...)␉␉printf(x)␊ |
18 | #else␊ |
19 | #define DBG(x...)␉␉msglog(x)␊ |
20 | #endif␊ |
21 | ␊ |
22 | ␊ |
23 | /*␊ |
24 | * DFE: Measures the TSC frequency in Hz (64-bit) using the ACPI PM timer␊ |
25 | */␊ |
26 | static 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 | * Calculates the FSB and CPU frequencies using specific MSRs for each CPU␊ |
89 | * - multi. is read from a specific MSR. In the case of Intel, there is:␊ |
90 | * a max multi. (used to calculate the FSB freq.),␊ |
91 | * and a current multi. (used to calculate the CPU freq.)␊ |
92 | * - fsbFrequency = tscFrequency / multi␊ |
93 | * - cpuFrequency = fsbFrequency * multi␊ |
94 | */␊ |
95 | ␊ |
96 | void scan_cpu() //PlatformInfo_t *p)␊ |
97 | {␊ |
98 | ␉PlatformInfo_t *p = Platform;␊ |
99 | ␉int i = 0;␊ |
100 | ␉uint64_t␉tscFrequency, fsbFrequency, cpuFrequency;␊ |
101 | ␉uint64_t␉msr, flex_ratio;␊ |
102 | ␉uint8_t␉␉maxcoef, maxdiv, currcoef, currdiv, mindiv;␊ |
103 | ␊ |
104 | ␉maxcoef = maxdiv = currcoef = currdiv = mindiv = 0;␊ |
105 | ␊ |
106 | ␉/* get cpuid values */␊ |
107 | ␉for( ; i <= 3; i++)␊ |
108 | ␉{␊ |
109 | ␉␉do_cpuid(i, p->CPU.CPUID[i]);␊ |
110 | ␉}␊ |
111 | ␉␊ |
112 | ␉do_cpuid2(0x00000004, 0, p->CPU.CPUID[CPUID_4]);␊ |
113 | ␉do_cpuid(0x80000000, p->CPU.CPUID[CPUID_80]);␊ |
114 | ␉if ((p->CPU.CPUID[CPUID_80][0] & 0x0000000f) >= 1) {␊ |
115 | ␉␉do_cpuid(0x80000001, p->CPU.CPUID[CPUID_81]);␊ |
116 | ␉}␊ |
117 | #if DEBUG_CPU␊ |
118 | ␉{␊ |
119 | ␉␉int␉␉i;␊ |
120 | ␉␉DBG("CPUID Raw Values:\n");␊ |
121 | ␉␉for (i=0; i<CPUID_MAX; i++) {␊ |
122 | ␉␉␉DBG("%02d: %08x-%08x-%08x-%08x\n", i,␊ |
123 | ␉␉␉␉p->CPU.CPUID[i][0], p->CPU.CPUID[i][1],␊ |
124 | ␉␉␉␉p->CPU.CPUID[i][2], p->CPU.CPUID[i][3]);␊ |
125 | ␉␉}␊ |
126 | ␉}␊ |
127 | #endif␊ |
128 | ␉p->CPU.Vendor␉␉= p->CPU.CPUID[CPUID_0][1];␊ |
129 | ␉p->CPU.Signature␉= p->CPU.CPUID[CPUID_1][0];␊ |
130 | ␉p->CPU.Stepping␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);␊ |
131 | ␉p->CPU.Model␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);␊ |
132 | ␉p->CPU.Family␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);␊ |
133 | ␉p->CPU.ExtModel␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);␊ |
134 | ␉p->CPU.ExtFamily␉= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);␊ |
135 | ␉p->CPU.NoThreads␉= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);␊ |
136 | ␉p->CPU.NoCores␉␉= bitfield(p->CPU.CPUID[CPUID_4][0], 31, 26) + 1;␊ |
137 | ␊ |
138 | ␉p->CPU.Model += (p->CPU.ExtModel << 4);␊ |
139 | ␉␊ |
140 | ␉/* get brand string (if supported) */␊ |
141 | ␉/* Copyright: from Apple's XNU cpuid.c */␊ |
142 | ␉if (p->CPU.CPUID[CPUID_80][0] > 0x80000004) {␊ |
143 | ␉␉uint32_t␉reg[4];␊ |
144 | char str[128], *s;␊ |
145 | ␉␉/*␊ |
146 | ␉␉ * The brand string 48 bytes (max), guaranteed to␊ |
147 | ␉␉ * be NUL terminated.␊ |
148 | ␉␉ */␊ |
149 | ␉␉do_cpuid(0x80000002, reg);␊ |
150 | ␉␉bcopy((char *)reg, &str[0], 16);␊ |
151 | ␉␉do_cpuid(0x80000003, reg);␊ |
152 | ␉␉bcopy((char *)reg, &str[16], 16);␊ |
153 | ␉␉do_cpuid(0x80000004, reg);␊ |
154 | ␉␉bcopy((char *)reg, &str[32], 16);␊ |
155 | ␉␉for (s = str; *s != '\0'; s++) {␊ |
156 | ␉␉␉if (*s != ' ') break;␊ |
157 | ␉␉}␊ |
158 | ␉␉␊ |
159 | ␉␉strlcpy(p->CPU.BrandString,␉s, sizeof(p->CPU.BrandString));␊ |
160 | ␉␉␊ |
161 | ␉␉if (!strncmp(p->CPU.BrandString, CPU_STRING_UNKNOWN, min(sizeof(p->CPU.BrandString), strlen(CPU_STRING_UNKNOWN) + 1))) {␊ |
162 | ␉␉␉ /*␊ |
163 | ␉␉␉ * This string means we have a firmware-programmable brand string,␊ |
164 | ␉␉␉ * and the firmware couldn't figure out what sort of CPU we have.␊ |
165 | ␉␉␉ */␊ |
166 | ␉␉␉ p->CPU.BrandString[0] = '\0';␊ |
167 | ␉␉ }␊ |
168 | ␉}␊ |
169 | ␉␊ |
170 | ␉/* setup features */␊ |
171 | ␉p->CPU.Features |= (CPU_FEATURE_MMX | CPU_FEATURE_SSE | CPU_FEATURE_SSE2 | CPU_FEATURE_MSR) & p->CPU.CPUID[CPUID_1][3];␊ |
172 | ␉p->CPU.Features |= (CPU_FEATURE_SSE3 | CPU_FEATURE_SSE41 | CPU_FEATURE_SSE42) & p->CPU.CPUID[CPUID_1][2];␉␊ |
173 | ␉p->CPU.Features |= (CPU_FEATURE_EM64T) & p->CPU.CPUID[CPUID_81][3];␊ |
174 | ␊ |
175 | ␊ |
176 | ␉//if ((CPU_FEATURE_HTT & p->CPU.CPUID[CPUID_1][3]) != 0) {␊ |
177 | ␉if (p->CPU.NoThreads > p->CPU.NoCores) {␊ |
178 | ␉␉p->CPU.Features |= CPU_FEATURE_HTT;␊ |
179 | ␉}␊ |
180 | ␉ ␊ |
181 | ␊ |
182 | ␉tscFrequency = measure_tsc_frequency();␊ |
183 | ␉DBG("measure_tsc_frequency = %dMHz\n", tscFrequency / MEGA);␊ |
184 | ␉fsbFrequency = 0;␊ |
185 | ␉cpuFrequency = 0;␊ |
186 | ␊ |
187 | ␉if ((p->CPU.Vendor == 0x756E6547 /* Intel */) && ␊ |
188 | ␉␉((p->CPU.Family == 0x06) || ␊ |
189 | ␉␉ (p->CPU.Family == 0x0f)))␊ |
190 | ␉{␊ |
191 | ␉␉if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0c) || ␊ |
192 | ␉␉␉(p->CPU.Family == 0x0f && p->CPU.Model >= 0x03))␊ |
193 | ␉␉{␊ |
194 | ␉␉␉/* Nehalem CPU model */␊ |
195 | ␉␉␉if (p->CPU.Family == 0x06 && (p->CPU.Model == 0x1a || p->CPU.Model == 0x1e ||␊ |
196 | ␉␉␉␉␉␉␉␉␉␉ p->CPU.Model == 0x1f || p->CPU.Model == 0x25 ||␊ |
197 | ␉␉␉␉␉␉␉␉␉␉ p->CPU.Model == 0x19 || p->CPU.Model == 0x2c)) ␊ |
198 | ␉␉␉{␊ |
199 | ␉␉␉␉msr = rdmsr64(MSR_PLATFORM_INFO);␊ |
200 | ␉␉␉␉DBG("msr(0x%04x): platform_info %08x-%08x\n", MSR_PLATFORM_INFO,␊ |
201 | ␉␉␉␉(msr >> 32) & 0xffffffff, msr & 0xffffffff);␊ |
202 | ␉␉␉␉mindiv = (msr >> 40) & 0xff;␊ |
203 | ␉␉␉␉flex_ratio = (msr >> 8) & 0xff;␊ |
204 | ␉␉␉␉//Slice - doesn't work␊ |
205 | ␉␉␉␉/*␊ |
206 | ␉␉␉␉msr = rdmsr64(MSR_FLEX_RATIO);␊ |
207 | ␉␉␉␉DBG("msr(0x%04x): flex_ratio %08x\n", MSR_FLEX_RATIO, msr & 0xffffffff);␊ |
208 | ␉␉␉␉if ((msr >> 16) & 0x01) {␊ |
209 | ␉␉␉␉␉flex_ratio = (msr >> 8) & 0xff;␊ |
210 | ␉␉␉␉␉if (currcoef > flex_ratio) {␊ |
211 | ␉␉␉␉␉␉currcoef = flex_ratio;␊ |
212 | ␉␉␉␉␉}␊ |
213 | ␉␉␉␉}*/␊ |
214 | ␉␉␉␉msr = rdmsr64(MSR_IA32_PERF_STATUS);␊ |
215 | ␉␉␉␉if (msr) {␊ |
216 | ␉␉␉␉␉currcoef = msr & 0x1f;␊ |
217 | ␉␉␉␉}␊ |
218 | ␉␉␉␉if (currcoef < flex_ratio) {␊ |
219 | ␉␉␉␉␉currcoef = flex_ratio;␊ |
220 | ␉␉␉␉}␊ |
221 | ␉␉␉␉if (currcoef < mindiv) {␊ |
222 | ␉␉␉␉␉currcoef = mindiv;␊ |
223 | ␉␉␉␉}␊ |
224 | ␉␉␉␉␊ |
225 | ␉␉␉␉if (currcoef) {␊ |
226 | ␉␉␉␉␉fsbFrequency = (tscFrequency / currcoef);␊ |
227 | ␉␉␉␉}␊ |
228 | ␉␉␉␉cpuFrequency = tscFrequency;␊ |
229 | ␉␉␉} ␊ |
230 | ␉␉␉else //not nehalem␊ |
231 | ␉␉␉{␊ |
232 | ␉␉␉␉//Slice - it is not FSB frequency. It is System Bus Speed: FSB = SBS * 4;␉␊ |
233 | ␉␉␉␉//crash with i7?␊ |
234 | #if 1 //NOTI7␉␊ |
235 | ␉␉␉␉if (p->CPU.Family != 0x0d){␊ |
236 | ␉␉␉␉␉msr = rdmsr64(MSR_FSB_FREQ);␊ |
237 | ␉␉␉␉␉switch (msr & 7) {␊ |
238 | ␉␉␉␉␉␉case 0:␊ |
239 | ␉␉␉␉␉␉␉fsbFrequency = 266670 * 1000;␊ |
240 | ␉␉␉␉␉␉␉break;␊ |
241 | ␉␉␉␉␉␉case 1:␊ |
242 | ␉␉␉␉␉␉␉fsbFrequency = 133330 * 1000;␊ |
243 | ␉␉␉␉␉␉␉break;␊ |
244 | ␉␉␉␉␉␉case 2:␊ |
245 | ␉␉␉␉␉␉␉fsbFrequency = 200000 * 1000;␊ |
246 | ␉␉␉␉␉␉␉break;␊ |
247 | ␉␉␉␉␉␉case 3:␊ |
248 | ␉␉␉␉␉␉␉fsbFrequency = 166670 * 1000;␊ |
249 | ␉␉␉␉␉␉␉break;␊ |
250 | ␉␉␉␉␉␉case 4:␊ |
251 | ␉␉␉␉␉␉␉fsbFrequency = 333330 * 1000;␊ |
252 | ␉␉␉␉␉␉␉break;␊ |
253 | ␉␉␉␉␉␉case 5:␊ |
254 | ␉␉␉␉␉␉␉fsbFrequency = 200000 * 1000;␊ |
255 | ␉␉␉␉␉␉␉break;␊ |
256 | ␉␉␉␉␉␉case 6:␊ |
257 | ␉␉␉␉␉␉␉fsbFrequency = 400000 * 1000;␊ |
258 | ␉␉␉␉␉␉␉break;␊ |
259 | ␉␉␉␉␉␉default:␊ |
260 | ␉␉␉␉␉␉␉fsbFrequency = 0;␊ |
261 | ␉␉␉␉␉␉␉break;␊ |
262 | ␉␉␉␉␉}␊ |
263 | ␉␉␉␉␉DBG("msr(0x%04x): MSR_FSB_FREQ %dMHz\n", MSR_FSB_FREQ, fsbFrequency/MEGA);␊ |
264 | ␉␉␉␉}␊ |
265 | #endif␉␊ |
266 | ␉␉␉␉␊ |
267 | ␉␉␉␉msr = rdmsr64(MSR_PLATFORM_INFO);␊ |
268 | ␉␉␉␉uint32_t m2 = msr & 0xffffffff;␊ |
269 | ␉␉␉␉DBG("msr(0x%04x): platform_info %08x-%08x\n", MSR_PLATFORM_INFO,␊ |
270 | ␉␉␉␉␉(msr >> 32) & 0xffffffff, m2);␊ |
271 | ␉␉␉␉currcoef = (msr >> 40) & 0xff;␊ |
272 | ␉␉␉␉msr = rdmsr64(MSR_IA32_PERF_STATUS);␊ |
273 | ␉␉␉␉mindiv = (msr >> 24) & 0xf;␊ |
274 | ␉␉␉␉if (currcoef < mindiv) {␊ |
275 | ␉␉␉␉␉currcoef = mindiv;␊ |
276 | ␉␉␉␉}␊ |
277 | ␉␉␉␉␊ |
278 | ␉␉␉␉/* Non-integer bus ratio for the max-multi*/␊ |
279 | ␉␉␉␉maxdiv = (msr >> 46) & 0x01;␊ |
280 | ␉␉␉␉/* Non-integer bus ratio for the current-multi (undocumented)*/␊ |
281 | ␉␉␉␉currdiv = (msr >> 14) & 0x01;␊ |
282 | ␊ |
283 | ␉␉␉␉if ((p->CPU.Family == 0x06 && p->CPU.Model >= 0x0e) || ␊ |
284 | ␉␉␉␉␉(p->CPU.Family == 0x0f)) // This will always be model >= 3␊ |
285 | ␉␉␉␉{␊ |
286 | ␉␉␉␉␉/* On these models, maxcoef defines TSC freq */␊ |
287 | ␉␉␉␉␉maxcoef = (msr >> 40) & 0x1f;␊ |
288 | ␉␉␉␉} ␊ |
289 | ␉␉␉␉else ␊ |
290 | ␉␉␉␉{␊ |
291 | ␉␉␉␉␉/* On lower models, currcoef defines TSC freq */␊ |
292 | ␉␉␉␉␉/* XXX */␊ |
293 | ␉␉␉␉␉maxcoef = currcoef;␊ |
294 | ␉␉␉␉}␊ |
295 | ␊ |
296 | ␉␉␉␉if (maxcoef) ␊ |
297 | ␉␉␉␉{␊ |
298 | ␉␉␉␉␉if (!fsbFrequency) {␊ |
299 | ␉␉␉␉␉␉if (maxdiv)␊ |
300 | ␉␉␉␉␉␉{␊ |
301 | ␉␉␉␉␉␉␉fsbFrequency = ((tscFrequency * 2) / ((maxcoef * 2) + 1));␊ |
302 | ␉␉␉␉␉␉}␊ |
303 | ␉␉␉␉␉␉else ␊ |
304 | ␉␉␉␉␉␉{␊ |
305 | ␉␉␉␉␉␉␉fsbFrequency = (tscFrequency / maxcoef);␊ |
306 | ␉␉␉␉␉␉}␊ |
307 | ␉␉␉␉␉␉␊ |
308 | ␉␉␉␉␉}␊ |
309 | ␉␉␉␉␉␊ |
310 | ␉␉␉␉␉if (currdiv) ␊ |
311 | ␉␉␉␉␉{␊ |
312 | ␉␉␉␉␉␉cpuFrequency = (fsbFrequency * ((currcoef * 2) + 1) / 2);␊ |
313 | ␉␉␉␉␉}␊ |
314 | ␉␉␉␉␉else ␊ |
315 | ␉␉␉␉␉{␊ |
316 | ␉␉␉␉␉␉cpuFrequency = (fsbFrequency * currcoef);␊ |
317 | ␉␉␉␉␉}␊ |
318 | ␉␉␉␉␉DBG("max: %d%s current: %d%s\n", maxcoef, maxdiv ? ".5" : "",currcoef, currdiv ? ".5" : "");␊ |
319 | ␉␉␉␉}␊ |
320 | ␉␉␉}␊ |
321 | ␉␉}␊ |
322 | ␉␉/* Mobile CPU ? */␊ |
323 | //Slice ␊ |
324 | ␉␉msr = rdmsr64(MSR_IA32_PLATFORM_ID);␊ |
325 | ␉␉DBG("msr(0x%04x): MSR_IA32_PLATFORM_ID 0x%08x\n", MSR_IA32_PLATFORM_ID, msr & 0xffffffff); //__LINE__ - source line number :)␊ |
326 | ␉␉if (!scanDMI() && msr) {␊ |
327 | ␉␉␉p->CPU.Mobile = FALSE;␊ |
328 | ␉␉␉switch (p->CPU.Model) {␊ |
329 | ␉␉␉␉case 0x0D:␊ |
330 | ␉␉␉␉␉p->CPU.Mobile = TRUE; // CPU_FEATURE_MOBILE;␊ |
331 | ␉␉␉␉␉break;␊ |
332 | ␉␉␉␉case 0x0F:␊ |
333 | ␉␉␉␉␉p->CPU.Mobile = FALSE; // CPU_FEATURE_MOBILE;␊ |
334 | ␉␉␉␉␉break;␊ |
335 | ␉␉␉␉case 0x02:␊ |
336 | ␉␉␉␉case 0x03:␊ |
337 | ␉␉␉␉case 0x04:␊ |
338 | ␉␉␉␉case 0x06:␉␊ |
339 | ␉␉␉␉␉p->CPU.Mobile = (rdmsr64(MSR_P4_EBC_FREQUENCY_ID) && (1 << 21));␊ |
340 | ␉␉␉␉␉break;␊ |
341 | ␉␉␉␉default:␊ |
342 | ␉␉␉␉␉p->CPU.Mobile = (rdmsr64(MSR_IA32_PLATFORM_ID) && (1<<28));␊ |
343 | ␉␉␉␉␉break;␊ |
344 | ␉␉␉}␊ |
345 | ␉␉␉if (p->CPU.Mobile) {␊ |
346 | ␉␉␉␉p->CPU.Features |= CPU_FEATURE_MOBILE;␊ |
347 | ␉␉␉}␊ |
348 | ␉␉}␊ |
349 | ␉␉DBG("CPU is %s\n", p->CPU.Mobile?"Mobile":"Desktop");␊ |
350 | ␉␉␉␊ |
351 | ␉}␊ |
352 | #if 0␊ |
353 | ␉else if((p->CPU.Vendor == 0x68747541 /* AMD */) && (p->CPU.Family == 0x0f))␊ |
354 | ␉{␊ |
355 | ␉␉if(p->CPU.ExtFamily == 0x00 /* K8 */)␊ |
356 | ␉␉{␊ |
357 | ␉␉␉msr = rdmsr64(K8_FIDVID_STATUS);␊ |
358 | ␉␉␉currcoef = (msr & 0x3f) / 2 + 4;␊ |
359 | ␉␉␉currdiv = (msr & 0x01) * 2;␊ |
360 | ␉␉} ␊ |
361 | ␉␉else if(p->CPU.ExtFamily >= 0x01 /* K10+ */)␊ |
362 | ␉␉{␊ |
363 | ␉␉␉msr = rdmsr64(K10_COFVID_STATUS);␊ |
364 | ␉␉␉if(p->CPU.ExtFamily == 0x01 /* K10 */)␊ |
365 | ␉␉␉␉currcoef = (msr & 0x3f) + 0x10;␊ |
366 | ␉␉␉else /* K11+ */␊ |
367 | ␉␉␉␉currcoef = (msr & 0x3f) + 0x08;␊ |
368 | ␉␉␉currdiv = (2 << ((msr >> 6) & 0x07));␊ |
369 | ␉␉}␊ |
370 | ␊ |
371 | ␉␉if (currcoef) ␊ |
372 | ␉␉{␊ |
373 | ␉␉␉if (currdiv) ␊ |
374 | ␉␉␉{␊ |
375 | ␉␉␉␉fsbFrequency = ((tscFrequency * currdiv) / currcoef);␊ |
376 | ␉␉␉␉DBG("%d.%d\n", currcoef / currdiv, ((currcoef % currdiv) * 100) / currdiv);␊ |
377 | ␉␉␉}␊ |
378 | ␉␉␉else␊ |
379 | ␉␉␉{␊ |
380 | ␉␉␉␉fsbFrequency = (tscFrequency / currcoef);␊ |
381 | ␉␉␉␉DBG("%d\n", currcoef);␊ |
382 | ␉␉␉}␊ |
383 | ␉␉␉fsbFrequency = (tscFrequency / currcoef);␊ |
384 | ␉␉␉cpuFrequency = tscFrequency;␊ |
385 | ␉␉}␊ |
386 | ␉}␊ |
387 | #endif␊ |
388 | ␉else if(p->CPU.Vendor == 0x746e6543 && p->CPU.Family == 6)␊ |
389 | ␉{␊ |
390 | ␉␉switch (p->CPU.Model) {␊ |
391 | ␉␉␉case CPU_VIA_NANO:␊ |
392 | ␉␉␉␉// NOTE: TSC is constant, irrelevent of speed steping ␊ |
393 | ␉␉␉␉break;␊ |
394 | ␉␉␉default:␊ |
395 | ␉␉␉␉break;␊ |
396 | ␉␉}␊ |
397 | ␉␉␊ |
398 | ␉␉msr = rdmsr64(MSR_NANO_FCR2);␊ |
399 | ␉␉printf("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);␊ |
400 | ␉␉␊ |
401 | ␉␉//msr = msr >> 32;␊ |
402 | ␉␉msr |= VIA_ALTERNATIVE_VENDOR_BIT;␊ |
403 | ␉␉//msr = msr << 32;␊ |
404 | ␉␉␊ |
405 | ␉␉printf("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);␊ |
406 | ␉␉wrmsr64(MSR_NANO_FCR2, msr);␊ |
407 | ␉␉msr = rdmsr64(MSR_NANO_FCR2);␊ |
408 | ␉␉printf("MSR_IA32_EBL_CR_POWERON Returns 0x%X 0x%X\n", msr >> 32, msr & 0xffffffff);␊ |
409 | ␉␉␊ |
410 | ␉␉␊ |
411 | ␉␉/* get cpuid values */␊ |
412 | ␉␉for( ; i <= 3; i++)␊ |
413 | ␉␉{␊ |
414 | ␉␉␉do_cpuid(i, p->CPU.CPUID[i]);␊ |
415 | ␉␉}␊ |
416 | ␉␉//int numcpuid_supported = p->CPU.CPUID[CPUID_0][0];␉// max number cpuid call␊ |
417 | ␉␉//int numextcpuid = p->CPU.CPUID[CPUID_80][0];␊ |
418 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉//p->CPU.Features = 0;␊ |
419 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉//␉␉bitfield(p->CPU.CPUID[CPUID_1][1], 0, 0) FEATURE_C␊ |
420 | ␉␉␊ |
421 | ␉␉// CPUID_0 -> largest cpuid val in EAX␊ |
422 | ␉␉// CPUID_0 -> rem = vendor string␊ |
423 | ␉␉/*␊ |
424 | ␉␉CPUID_1 EDX:␊ |
425 | ␉␉ 0 -> FPU␊ |
426 | ␉␉ 1 -> VME␊ |
427 | ␉␉ 2 -> DE␊ |
428 | ␉␉ 3 -> PSE␊ |
429 | ␉␉ 4 -> TSC␊ |
430 | ␉␉ 5 -> MSR␊ |
431 | ␉␉ 6 -> PAE␊ |
432 | ␉␉ 7 -> MCE␊ |
433 | ␉␉ 8 -> CX8␊ |
434 | ␉␉ 9 -> APIC␊ |
435 | ␉␉ 10 -> Reserved␊ |
436 | ␉␉ 11 -> Fast Call␊ |
437 | ␉␉ 12 -> MTTR␊ |
438 | ␉␉ 13 -> PGE␊ |
439 | ␉␉ 14 -> MCA␊ |
440 | ␉␉ 15 -> CMOV␊ |
441 | ␉␉ 16 -> PAT␊ |
442 | ␉␉ 17 -> PSE36␊ |
443 | ␉␉ 18 -> Serial Number ␊ |
444 | ␉␉ 23 -> MMX␊ |
445 | ␉␉ 24 -> FXSR␊ |
446 | ␉␉ 25 -> SSE␊ |
447 | ␉␉ */␊ |
448 | ␉␉␊ |
449 | ␉␉//CPUID_80 -> largest excpuid value in EAX␊ |
450 | ␉␉//CPUID_81,EAX -> Signature␊ |
451 | ␉␉//CPUID_80,EDX -> Ext Features␊ |
452 | ␉␉//CPUID_82 -> CPU String␊ |
453 | ␉␉//CPUID_83 -> CPU String␊ |
454 | ␉␉//CPUID_84 -> CPU String␊ |
455 | ␉␉p->CPU.NoThreads = p->CPU.NoCores;␊ |
456 | ␉␉␊ |
457 | ␉}␊ |
458 | ␉␊ |
459 | ␉if (!fsbFrequency) {␊ |
460 | ␉␉fsbFrequency = (DEFAULT_FSB * 1000);␊ |
461 | ␉␉cpuFrequency = tscFrequency;␊ |
462 | ␉␉msglog("CPU: fsb=0 ! using the default value 100MHz !\n");␊ |
463 | ␉}␊ |
464 | ␉␊ |
465 | ␉␊ |
466 | ␉p->CPU.Vendor␉␉= p->CPU.CPUID[CPUID_0][1];␊ |
467 | ␉p->CPU.Signature␉= p->CPU.CPUID[CPUID_1][0];␊ |
468 | ␉p->CPU.Stepping␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 3, 0);␊ |
469 | ␉p->CPU.Model␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 7, 4);␊ |
470 | ␉p->CPU.Family␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 11, 8);␊ |
471 | ␉p->CPU.ExtModel␉␉= bitfield(p->CPU.CPUID[CPUID_1][0], 19, 16);␊ |
472 | ␉p->CPU.ExtFamily␉= bitfield(p->CPU.CPUID[CPUID_1][0], 27, 20);␊ |
473 | ␉p->CPU.NoThreads␉= bitfield(p->CPU.CPUID[CPUID_1][1], 23, 16);␊ |
474 | ␉␊ |
475 | ␊ |
476 | ␊ |
477 | ␉p->CPU.MaxCoef = maxcoef;␊ |
478 | ␉p->CPU.MaxDiv = maxdiv;␊ |
479 | ␉p->CPU.MinCoef = mindiv;␊ |
480 | ␉p->CPU.CurrCoef = currcoef;␊ |
481 | ␉p->CPU.CurrDiv = currdiv;␊ |
482 | ␉p->CPU.TSCFrequency = tscFrequency;␊ |
483 | ␉p->CPU.FSBFrequency = fsbFrequency;␊ |
484 | ␉p->CPU.CPUFrequency = cpuFrequency;␊ |
485 | ␊ |
486 | ␉DBG("CPU: Vendor/Model/ExtModel: 0x%x/0x%x/0x%x\n", p->CPU.Vendor, p->CPU.Model, p->CPU.ExtModel);␊ |
487 | ␉DBG("CPU: Family/ExtFamily: 0x%x/0x%x\n", p->CPU.Family, p->CPU.ExtFamily);␊ |
488 | ␉DBG("CPU: MaxCoef/CurrCoef: 0x%x/0x%x\n", p->CPU.MaxCoef, p->CPU.CurrCoef);␊ |
489 | ␉DBG("CPU: MaxDiv/CurrDiv: 0x%x/0x%x\n", p->CPU.MaxDiv?2:1, p->CPU.CurrDiv?2:1);␊ |
490 | ␉DBG("CPU: TSCFreq: %dMHz\n", p->CPU.TSCFrequency / 1000000);␊ |
491 | ␉DBG("CPU: FSBFreq: %dMHz\n", p->CPU.FSBFrequency / 1000000);␊ |
492 | ␉DBG("CPU: CPUFreq: %dMHz\n", p->CPU.CPUFrequency / 1000000);␊ |
493 | ␉DBG("CPU: NoCores/NoThreads: %d/%d\n", p->CPU.NoCores, p->CPU.NoThreads);␊ |
494 | ␉DBG("CPU: Features: 0x%08x\n", p->CPU.Features);␊ |
495 | #if DEBUG_CPU␊ |
496 | ␉pause();␊ |
497 | #endif␊ |
498 | }␊ |
499 | |