]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/cpuid.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / i386 / cpuid.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0c530ab8
A
31#include <platforms.h>
32#include <mach_kdb.h>
2d21ac55 33#include <vm/vm_page.h>
91447636
A
34#include <pexpert/pexpert.h>
35
b0d623f7 36#include <i386/cpuid.h>
0c530ab8 37#if MACH_KDB
b0d623f7 38#include <machine/db_machdep.h>
0c530ab8
A
39#include <ddb/db_aout.h>
40#include <ddb/db_access.h>
41#include <ddb/db_sym.h>
42#include <ddb/db_variables.h>
43#include <ddb/db_command.h>
44#include <ddb/db_output.h>
45#include <ddb/db_expr.h>
46#endif
1c79356b 47
55e303ae 48#define min(a,b) ((a) < (b) ? (a) : (b))
0c530ab8
A
49#define quad(hi,lo) (((uint64_t)(hi)) << 32 | (lo))
50
b0d623f7 51/* Only for 32bit values */
7e4a7d39
A
52#define bit32(n) (1U << (n))
53#define bitmask32(h,l) ((bit32(h)|(bit32(h)-1)) & ~(bit32(l)-1))
54#define bitfield32(x,h,l) ((((x) & bitmask32(h,l)) >> l))
b0d623f7
A
55
56/*
57 * Leaf 2 cache descriptor encodings.
58 */
59typedef enum {
60 _NULL_, /* NULL (empty) descriptor */
61 CACHE, /* Cache */
62 TLB, /* TLB */
63 STLB, /* Shared second-level unified TLB */
64 PREFETCH /* Prefetch size */
65} cpuid_leaf2_desc_type_t;
66
67typedef enum {
68 NA, /* Not Applicable */
69 FULLY, /* Fully-associative */
70 TRACE, /* Trace Cache (P4 only) */
71 INST, /* Instruction TLB */
72 DATA, /* Data TLB */
73 DATA0, /* Data TLB, 1st level */
74 DATA1, /* Data TLB, 2nd level */
75 L1, /* L1 (unified) cache */
76 L1_INST, /* L1 Instruction cache */
77 L1_DATA, /* L1 Data cache */
78 L2, /* L2 (unified) cache */
79 L3, /* L3 (unified) cache */
80 L2_2LINESECTOR, /* L2 (unified) cache with 2 lines per sector */
81 L3_2LINESECTOR, /* L3(unified) cache with 2 lines per sector */
82 SMALL, /* Small page TLB */
83 LARGE, /* Large page TLB */
84 BOTH /* Small and Large page TLB */
85} cpuid_leaf2_qualifier_t;
86
87typedef struct cpuid_cache_descriptor {
88 uint8_t value; /* descriptor code */
89 uint8_t type; /* cpuid_leaf2_desc_type_t */
90 uint8_t level; /* level of cache/TLB hierachy */
91 uint8_t ways; /* wayness of cache */
92 uint16_t size; /* cachesize or TLB pagesize */
93 uint16_t entries; /* number of TLB entries or linesize */
94} cpuid_cache_descriptor_t;
95
96/*
97 * These multipliers are used to encode 1*K .. 64*M in a 16 bit size field
98 */
99#define K (1)
100#define M (1024)
101
102/*
103 * Intel cache descriptor table:
104 */
105static cpuid_cache_descriptor_t intel_cpuid_leaf2_descriptor_table[] = {
106// -------------------------------------------------------
107// value type level ways size entries
108// -------------------------------------------------------
109 { 0x00, _NULL_, NA, NA, NA, NA },
110 { 0x01, TLB, INST, 4, SMALL, 32 },
111 { 0x02, TLB, INST, FULLY, LARGE, 2 },
112 { 0x03, TLB, DATA, 4, SMALL, 64 },
113 { 0x04, TLB, DATA, 4, LARGE, 8 },
114 { 0x05, TLB, DATA1, 4, LARGE, 32 },
115 { 0x06, CACHE, L1_INST, 4, 8*K, 32 },
116 { 0x08, CACHE, L1_INST, 4, 16*K, 32 },
117 { 0x09, CACHE, L1_INST, 4, 32*K, 64 },
118 { 0x0A, CACHE, L1_DATA, 2, 8*K, 32 },
119 { 0x0B, TLB, INST, 4, LARGE, 4 },
120 { 0x0C, CACHE, L1_DATA, 4, 16*K, 32 },
121 { 0x0D, CACHE, L1_DATA, 4, 16*K, 64 },
122 { 0x0E, CACHE, L1_DATA, 6, 24*K, 64 },
123 { 0x21, CACHE, L2, 8, 256*K, 64 },
124 { 0x22, CACHE, L3_2LINESECTOR, 4, 512*K, 64 },
125 { 0x23, CACHE, L3_2LINESECTOR, 8, 1*M, 64 },
126 { 0x25, CACHE, L3_2LINESECTOR, 8, 2*M, 64 },
127 { 0x29, CACHE, L3_2LINESECTOR, 8, 4*M, 64 },
128 { 0x2C, CACHE, L1_DATA, 8, 32*K, 64 },
129 { 0x30, CACHE, L1_INST, 8, 32*K, 64 },
130 { 0x40, CACHE, L2, NA, 0, NA },
131 { 0x41, CACHE, L2, 4, 128*K, 32 },
132 { 0x42, CACHE, L2, 4, 256*K, 32 },
133 { 0x43, CACHE, L2, 4, 512*K, 32 },
134 { 0x44, CACHE, L2, 4, 1*M, 32 },
135 { 0x45, CACHE, L2, 4, 2*M, 32 },
136 { 0x46, CACHE, L3, 4, 4*M, 64 },
137 { 0x47, CACHE, L3, 8, 8*M, 64 },
138 { 0x48, CACHE, L2, 12, 3*M, 64 },
139 { 0x49, CACHE, L2, 16, 4*M, 64 },
140 { 0x4A, CACHE, L3, 12, 6*M, 64 },
141 { 0x4B, CACHE, L3, 16, 8*M, 64 },
142 { 0x4C, CACHE, L3, 12, 12*M, 64 },
143 { 0x4D, CACHE, L3, 16, 16*M, 64 },
144 { 0x4E, CACHE, L2, 24, 6*M, 64 },
145 { 0x4F, TLB, INST, NA, SMALL, 32 },
146 { 0x50, TLB, INST, NA, BOTH, 64 },
147 { 0x51, TLB, INST, NA, BOTH, 128 },
148 { 0x52, TLB, INST, NA, BOTH, 256 },
149 { 0x55, TLB, INST, FULLY, BOTH, 7 },
150 { 0x56, TLB, DATA0, 4, LARGE, 16 },
151 { 0x57, TLB, DATA0, 4, SMALL, 16 },
152 { 0x59, TLB, DATA0, FULLY, SMALL, 16 },
153 { 0x5A, TLB, DATA0, 4, LARGE, 32 },
154 { 0x5B, TLB, DATA, NA, BOTH, 64 },
155 { 0x5C, TLB, DATA, NA, BOTH, 128 },
156 { 0x5D, TLB, DATA, NA, BOTH, 256 },
157 { 0x60, CACHE, L1, 16*K, 8, 64 },
158 { 0x61, CACHE, L1, 4, 8*K, 64 },
159 { 0x62, CACHE, L1, 4, 16*K, 64 },
160 { 0x63, CACHE, L1, 4, 32*K, 64 },
161 { 0x70, CACHE, TRACE, 8, 12*K, NA },
162 { 0x71, CACHE, TRACE, 8, 16*K, NA },
163 { 0x72, CACHE, TRACE, 8, 32*K, NA },
164 { 0x78, CACHE, L2, 4, 1*M, 64 },
165 { 0x79, CACHE, L2_2LINESECTOR, 8, 128*K, 64 },
166 { 0x7A, CACHE, L2_2LINESECTOR, 8, 256*K, 64 },
167 { 0x7B, CACHE, L2_2LINESECTOR, 8, 512*K, 64 },
168 { 0x7C, CACHE, L2_2LINESECTOR, 8, 1*M, 64 },
169 { 0x7D, CACHE, L2, 8, 2*M, 64 },
170 { 0x7F, CACHE, L2, 2, 512*K, 64 },
171 { 0x80, CACHE, L2, 8, 512*K, 64 },
172 { 0x82, CACHE, L2, 8, 256*K, 32 },
173 { 0x83, CACHE, L2, 8, 512*K, 32 },
174 { 0x84, CACHE, L2, 8, 1*M, 32 },
175 { 0x85, CACHE, L2, 8, 2*M, 32 },
176 { 0x86, CACHE, L2, 4, 512*K, 64 },
177 { 0x87, CACHE, L2, 8, 1*M, 64 },
178 { 0xB0, TLB, INST, 4, SMALL, 128 },
179 { 0xB1, TLB, INST, 4, LARGE, 8 },
180 { 0xB2, TLB, INST, 4, SMALL, 64 },
181 { 0xB3, TLB, DATA, 4, SMALL, 128 },
182 { 0xB4, TLB, DATA1, 4, SMALL, 256 },
183 { 0xBA, TLB, DATA1, 4, BOTH, 64 },
184 { 0xCA, STLB, DATA1, 4, BOTH, 512 },
185 { 0xD0, CACHE, L3, 4, 512*K, 64 },
186 { 0xD1, CACHE, L3, 4, 1*M, 64 },
187 { 0xD2, CACHE, L3, 4, 2*M, 64 },
7e4a7d39
A
188 { 0xD3, CACHE, L3, 4, 4*M, 64 },
189 { 0xD4, CACHE, L3, 4, 8*M, 64 },
b0d623f7
A
190 { 0xD6, CACHE, L3, 8, 1*M, 64 },
191 { 0xD7, CACHE, L3, 8, 2*M, 64 },
192 { 0xD8, CACHE, L3, 8, 4*M, 64 },
7e4a7d39
A
193 { 0xD9, CACHE, L3, 8, 8*M, 64 },
194 { 0xDA, CACHE, L3, 8, 12*M, 64 },
b0d623f7
A
195 { 0xDC, CACHE, L3, 12, 1536*K, 64 },
196 { 0xDD, CACHE, L3, 12, 3*M, 64 },
197 { 0xDE, CACHE, L3, 12, 6*M, 64 },
7e4a7d39
A
198 { 0xDF, CACHE, L3, 12, 12*M, 64 },
199 { 0xE0, CACHE, L3, 12, 18*M, 64 },
b0d623f7
A
200 { 0xE2, CACHE, L3, 16, 2*M, 64 },
201 { 0xE3, CACHE, L3, 16, 4*M, 64 },
202 { 0xE4, CACHE, L3, 16, 8*M, 64 },
7e4a7d39
A
203 { 0xE5, CACHE, L3, 16, 16*M, 64 },
204 { 0xE6, CACHE, L3, 16, 24*M, 64 },
b0d623f7 205 { 0xF0, PREFETCH, NA, NA, 64, NA },
060df5ea
A
206 { 0xF1, PREFETCH, NA, NA, 128, NA },
207 { 0xFF, CACHE, NA, NA, 0, NA }
b0d623f7
A
208};
209#define INTEL_LEAF2_DESC_NUM (sizeof(intel_cpuid_leaf2_descriptor_table) / \
210 sizeof(cpuid_cache_descriptor_t))
211
212static inline cpuid_cache_descriptor_t *
213cpuid_leaf2_find(uint8_t value)
214{
215 unsigned int i;
216
217 for (i = 0; i < INTEL_LEAF2_DESC_NUM; i++)
218 if (intel_cpuid_leaf2_descriptor_table[i].value == value)
219 return &intel_cpuid_leaf2_descriptor_table[i];
220 return NULL;
221}
1c79356b
A
222
223/*
55e303ae 224 * CPU identification routines.
1c79356b 225 */
1c79356b 226
0c530ab8 227static i386_cpu_info_t *cpuid_cpu_infop = NULL;
55e303ae 228static i386_cpu_info_t cpuid_cpu_info;
d7e50217 229
b0d623f7 230#if defined(__x86_64__)
7e4a7d39 231static void cpuid_fn(uint32_t selector, uint32_t *result)
b0d623f7
A
232{
233 do_cpuid(selector, result);
234}
235#else
7e4a7d39 236static void cpuid_fn(uint32_t selector, uint32_t *result)
b0d623f7
A
237{
238 if (cpu_mode_is64bit()) {
239 asm("call _cpuid64"
240 : "=a" (result[0]),
241 "=b" (result[1]),
242 "=c" (result[2]),
243 "=d" (result[3])
060df5ea
A
244 : "a"(selector),
245 "b" (0),
246 "c" (0),
247 "d" (0));
b0d623f7
A
248 } else {
249 do_cpuid(selector, result);
250 }
251}
252#endif
253
2d21ac55
A
254/* this function is Intel-specific */
255static void
256cpuid_set_cache_info( i386_cpu_info_t * info_p )
91447636
A
257{
258 uint32_t cpuid_result[4];
2d21ac55
A
259 uint32_t reg[4];
260 uint32_t index;
261 uint32_t linesizes[LCACHE_MAX];
91447636
A
262 unsigned int i;
263 unsigned int j;
2d21ac55 264 boolean_t cpuid_deterministic_supported = FALSE;
55e303ae 265
2d21ac55
A
266 bzero( linesizes, sizeof(linesizes) );
267
268 /* Get processor cache descriptor info using leaf 2. We don't use
269 * this internally, but must publish it for KEXTs.
270 */
7e4a7d39 271 cpuid_fn(2, cpuid_result);
55e303ae
A
272 for (j = 0; j < 4; j++) {
273 if ((cpuid_result[j] >> 31) == 1) /* bit31 is validity */
274 continue;
275 ((uint32_t *) info_p->cache_info)[j] = cpuid_result[j];
276 }
277 /* first byte gives number of cpuid calls to get all descriptors */
278 for (i = 1; i < info_p->cache_info[0]; i++) {
279 if (i*16 > sizeof(info_p->cache_info))
280 break;
7e4a7d39 281 cpuid_fn(2, cpuid_result);
55e303ae
A
282 for (j = 0; j < 4; j++) {
283 if ((cpuid_result[j] >> 31) == 1)
284 continue;
285 ((uint32_t *) info_p->cache_info)[4*i+j] =
286 cpuid_result[j];
287 }
288 }
289
0c530ab8 290 /*
2d21ac55
A
291 * Get cache info using leaf 4, the "deterministic cache parameters."
292 * Most processors Mac OS X supports implement this flavor of CPUID.
293 * Loop over each cache on the processor.
0c530ab8 294 */
7e4a7d39 295 cpuid_fn(0, cpuid_result);
2d21ac55
A
296 if (cpuid_result[eax] >= 4)
297 cpuid_deterministic_supported = TRUE;
298
299 for (index = 0; cpuid_deterministic_supported; index++) {
300 cache_type_t type = Lnone;
301 uint32_t cache_type;
302 uint32_t cache_level;
303 uint32_t cache_sharing;
304 uint32_t cache_linesize;
305 uint32_t cache_sets;
306 uint32_t cache_associativity;
307 uint32_t cache_size;
308 uint32_t cache_partitions;
309 uint32_t colors;
310
311 reg[eax] = 4; /* cpuid request 4 */
312 reg[ecx] = index; /* index starting at 0 */
313 cpuid(reg);
0c530ab8 314//kprintf("cpuid(4) index=%d eax=%p\n", index, reg[eax]);
7e4a7d39 315 cache_type = bitfield32(reg[eax], 4, 0);
2d21ac55
A
316 if (cache_type == 0)
317 break; /* no more caches */
7e4a7d39
A
318 cache_level = bitfield32(reg[eax], 7, 5);
319 cache_sharing = bitfield32(reg[eax], 25, 14) + 1;
2d21ac55 320 info_p->cpuid_cores_per_package
7e4a7d39
A
321 = bitfield32(reg[eax], 31, 26) + 1;
322 cache_linesize = bitfield32(reg[ebx], 11, 0) + 1;
323 cache_partitions = bitfield32(reg[ebx], 21, 12) + 1;
324 cache_associativity = bitfield32(reg[ebx], 31, 22) + 1;
325 cache_sets = bitfield32(reg[ecx], 31, 0) + 1;
2d21ac55
A
326
327 /* Map type/levels returned by CPUID into cache_type_t */
328 switch (cache_level) {
329 case 1:
330 type = cache_type == 1 ? L1D :
331 cache_type == 2 ? L1I :
332 Lnone;
333 break;
334 case 2:
335 type = cache_type == 3 ? L2U :
336 Lnone;
337 break;
338 case 3:
339 type = cache_type == 3 ? L3U :
340 Lnone;
341 break;
342 default:
343 type = Lnone;
344 }
345
346 /* The total size of a cache is:
b0d623f7 347 * ( linesize * sets * associativity * partitions )
2d21ac55
A
348 */
349 if (type != Lnone) {
b0d623f7
A
350 cache_size = cache_linesize * cache_sets *
351 cache_associativity * cache_partitions;
2d21ac55
A
352 info_p->cache_size[type] = cache_size;
353 info_p->cache_sharing[type] = cache_sharing;
354 info_p->cache_partitions[type] = cache_partitions;
355 linesizes[type] = cache_linesize;
356
357 /* Compute the number of page colors for this cache,
358 * which is:
359 * ( linesize * sets ) / page_size
360 *
361 * To help visualize this, consider two views of a
362 * physical address. To the cache, it is composed
363 * of a line offset, a set selector, and a tag.
364 * To VM, it is composed of a page offset, a page
365 * color, and other bits in the pageframe number:
366 *
367 * +-----------------+---------+--------+
368 * cache: | tag | set | offset |
369 * +-----------------+---------+--------+
370 *
371 * +-----------------+-------+----------+
372 * VM: | don't care | color | pg offset|
373 * +-----------------+-------+----------+
374 *
375 * The color is those bits in (set+offset) not covered
376 * by the page offset.
377 */
378 colors = ( cache_linesize * cache_sets ) >> 12;
379
380 if ( colors > vm_cache_geometry_colors )
381 vm_cache_geometry_colors = colors;
382 }
383 }
384
385 /*
386 * If deterministic cache parameters are not available, use
387 * something else
388 */
389 if (info_p->cpuid_cores_per_package == 0) {
390 info_p->cpuid_cores_per_package = 1;
91447636 391
2d21ac55
A
392 /* cpuid define in 1024 quantities */
393 info_p->cache_size[L2U] = info_p->cpuid_cache_size * 1024;
394 info_p->cache_sharing[L2U] = 1;
395 info_p->cache_partitions[L2U] = 1;
91447636 396
2d21ac55
A
397 linesizes[L2U] = info_p->cpuid_cache_linesize;
398 }
399
400 /*
401 * What linesize to publish? We use the L2 linesize if any,
402 * else the L1D.
403 */
404 if ( linesizes[L2U] )
405 info_p->cache_linesize = linesizes[L2U];
406 else if (linesizes[L1D])
407 info_p->cache_linesize = linesizes[L1D];
408 else panic("no linesize");
593a1d5f
A
409
410 /*
b0d623f7 411 * Extract and publish TLB information from Leaf 2 descriptors.
593a1d5f
A
412 */
413 for (i = 1; i < sizeof(info_p->cache_info); i++) {
b0d623f7
A
414 cpuid_cache_descriptor_t *descp;
415 int id;
416 int level;
417 int page;
593a1d5f 418
b0d623f7
A
419 descp = cpuid_leaf2_find(info_p->cache_info[i]);
420 if (descp == NULL)
421 continue;
422
423 switch (descp->type) {
424 case TLB:
425 page = (descp->size == SMALL) ? TLB_SMALL : TLB_LARGE;
426 /* determine I or D: */
427 switch (descp->level) {
428 case INST:
429 id = TLB_INST;
430 break;
431 case DATA:
432 case DATA0:
433 case DATA1:
434 id = TLB_DATA;
435 break;
436 default:
437 continue;
438 }
439 /* determine level: */
440 switch (descp->level) {
441 case DATA1:
442 level = 1;
443 break;
444 default:
445 level = 0;
446 }
447 info_p->cpuid_tlb[id][page][level] = descp->entries;
593a1d5f 448 break;
b0d623f7
A
449 case STLB:
450 info_p->cpuid_stlb = descp->entries;
593a1d5f
A
451 }
452 }
91447636
A
453}
454
455static void
2d21ac55 456cpuid_set_generic_info(i386_cpu_info_t *info_p)
91447636 457{
7e4a7d39 458 uint32_t reg[4];
91447636
A
459 char str[128], *p;
460
2d21ac55 461 /* do cpuid 0 to get vendor */
7e4a7d39
A
462 cpuid_fn(0, reg);
463 info_p->cpuid_max_basic = reg[eax];
464 bcopy((char *)&reg[ebx], &info_p->cpuid_vendor[0], 4); /* ug */
465 bcopy((char *)&reg[ecx], &info_p->cpuid_vendor[8], 4);
466 bcopy((char *)&reg[edx], &info_p->cpuid_vendor[4], 4);
2d21ac55
A
467 info_p->cpuid_vendor[12] = 0;
468
91447636 469 /* get extended cpuid results */
7e4a7d39
A
470 cpuid_fn(0x80000000, reg);
471 info_p->cpuid_max_ext = reg[eax];
91447636
A
472
473 /* check to see if we can get brand string */
b0d623f7 474 if (info_p->cpuid_max_ext >= 0x80000004) {
91447636
A
475 /*
476 * The brand string 48 bytes (max), guaranteed to
477 * be NUL terminated.
478 */
7e4a7d39
A
479 cpuid_fn(0x80000002, reg);
480 bcopy((char *)reg, &str[0], 16);
481 cpuid_fn(0x80000003, reg);
482 bcopy((char *)reg, &str[16], 16);
483 cpuid_fn(0x80000004, reg);
484 bcopy((char *)reg, &str[32], 16);
91447636
A
485 for (p = str; *p != '\0'; p++) {
486 if (*p != ' ') break;
487 }
2d21ac55
A
488 strlcpy(info_p->cpuid_brand_string,
489 p, sizeof(info_p->cpuid_brand_string));
91447636 490
2d21ac55
A
491 if (!strncmp(info_p->cpuid_brand_string, CPUID_STRING_UNKNOWN,
492 min(sizeof(info_p->cpuid_brand_string),
493 strlen(CPUID_STRING_UNKNOWN) + 1))) {
91447636 494 /*
2d21ac55
A
495 * This string means we have a firmware-programmable brand string,
496 * and the firmware couldn't figure out what sort of CPU we have.
91447636
A
497 */
498 info_p->cpuid_brand_string[0] = '\0';
499 }
500 }
501
2d21ac55 502 /* Get cache and addressing info. */
b0d623f7 503 if (info_p->cpuid_max_ext >= 0x80000006) {
7e4a7d39
A
504 cpuid_fn(0x80000006, reg);
505 info_p->cpuid_cache_linesize = bitfield32(reg[ecx], 7, 0);
2d21ac55 506 info_p->cpuid_cache_L2_associativity =
7e4a7d39
A
507 bitfield32(reg[ecx],15,12);
508 info_p->cpuid_cache_size = bitfield32(reg[ecx],31,16);
509 cpuid_fn(0x80000008, reg);
2d21ac55 510 info_p->cpuid_address_bits_physical =
7e4a7d39 511 bitfield32(reg[eax], 7, 0);
2d21ac55 512 info_p->cpuid_address_bits_virtual =
7e4a7d39 513 bitfield32(reg[eax],15, 8);
2d21ac55
A
514 }
515
91447636 516 /* get processor signature and decode */
7e4a7d39
A
517 cpuid_fn(1, reg);
518 info_p->cpuid_signature = reg[eax];
519 info_p->cpuid_stepping = bitfield32(reg[eax], 3, 0);
520 info_p->cpuid_model = bitfield32(reg[eax], 7, 4);
521 info_p->cpuid_family = bitfield32(reg[eax], 11, 8);
522 info_p->cpuid_type = bitfield32(reg[eax], 13, 12);
523 info_p->cpuid_extmodel = bitfield32(reg[eax], 19, 16);
524 info_p->cpuid_extfamily = bitfield32(reg[eax], 27, 20);
525 info_p->cpuid_brand = bitfield32(reg[ebx], 7, 0);
526 info_p->cpuid_features = quad(reg[ecx], reg[edx]);
2d21ac55
A
527
528 /* Fold extensions into family/model */
529 if (info_p->cpuid_family == 0x0f)
530 info_p->cpuid_family += info_p->cpuid_extfamily;
593a1d5f 531 if (info_p->cpuid_family == 0x0f || info_p->cpuid_family == 0x06)
2d21ac55
A
532 info_p->cpuid_model += (info_p->cpuid_extmodel << 4);
533
534 if (info_p->cpuid_features & CPUID_FEATURE_HTT)
535 info_p->cpuid_logical_per_package =
7e4a7d39 536 bitfield32(reg[ebx], 23, 16);
2d21ac55
A
537 else
538 info_p->cpuid_logical_per_package = 1;
0c530ab8 539
b0d623f7 540 if (info_p->cpuid_max_ext >= 0x80000001) {
7e4a7d39 541 cpuid_fn(0x80000001, reg);
0c530ab8 542 info_p->cpuid_extfeatures =
7e4a7d39 543 quad(reg[ecx], reg[edx]);
2d21ac55
A
544 }
545
c910b4d9 546 /* Fold in the Invariant TSC feature bit, if present */
b0d623f7 547 if (info_p->cpuid_max_ext >= 0x80000007) {
7e4a7d39 548 cpuid_fn(0x80000007, reg);
c910b4d9 549 info_p->cpuid_extfeatures |=
7e4a7d39 550 reg[edx] & (uint32_t)CPUID_EXTFEATURE_TSCI;
c910b4d9
A
551 }
552
553 /* Find the microcode version number a.k.a. signature a.k.a. BIOS ID */
554 info_p->cpuid_microcode_version =
555 (uint32_t) (rdmsr64(MSR_IA32_BIOS_SIGN_ID) >> 32);
556
b0d623f7 557 if (info_p->cpuid_max_basic >= 0x5) {
7e4a7d39
A
558 cpuid_mwait_leaf_t *cmp = &info_p->cpuid_mwait_leaf;
559
2d21ac55
A
560 /*
561 * Extract the Monitor/Mwait Leaf info:
562 */
7e4a7d39
A
563 cpuid_fn(5, reg);
564 cmp->linesize_min = reg[eax];
565 cmp->linesize_max = reg[ebx];
566 cmp->extensions = reg[ecx];
567 cmp->sub_Cstates = reg[edx];
568 info_p->cpuid_mwait_leafp = cmp;
b0d623f7 569 }
2d21ac55 570
b0d623f7 571 if (info_p->cpuid_max_basic >= 0x6) {
7e4a7d39
A
572 cpuid_thermal_leaf_t *ctp = &info_p->cpuid_thermal_leaf;
573
2d21ac55 574 /*
b0d623f7 575 * The thermal and Power Leaf:
2d21ac55 576 */
7e4a7d39
A
577 cpuid_fn(6, reg);
578 ctp->sensor = bitfield32(reg[eax], 0, 0);
579 ctp->dynamic_acceleration = bitfield32(reg[eax], 1, 1);
b7266188 580 ctp->invariant_APIC_timer = bitfield32(reg[eax], 2, 2);
060df5ea
A
581 ctp->core_power_limits = bitfield32(reg[eax], 3, 3);
582 ctp->fine_grain_clock_mod = bitfield32(reg[eax], 4, 4);
583 ctp->package_thermal_intr = bitfield32(reg[eax], 5, 5);
7e4a7d39
A
584 ctp->thresholds = bitfield32(reg[ebx], 3, 0);
585 ctp->ACNT_MCNT = bitfield32(reg[ecx], 0, 0);
060df5ea
A
586 ctp->hardware_feedback = bitfield32(reg[ecx], 1, 1);
587 ctp->energy_policy = bitfield32(reg[ecx], 2, 2);
7e4a7d39 588 info_p->cpuid_thermal_leafp = ctp;
b0d623f7 589 }
2d21ac55 590
b0d623f7 591 if (info_p->cpuid_max_basic >= 0xa) {
7e4a7d39
A
592 cpuid_arch_perf_leaf_t *capp = &info_p->cpuid_arch_perf_leaf;
593
2d21ac55 594 /*
b0d623f7 595 * Architectural Performance Monitoring Leaf:
2d21ac55 596 */
7e4a7d39
A
597 cpuid_fn(0xa, reg);
598 capp->version = bitfield32(reg[eax], 7, 0);
599 capp->number = bitfield32(reg[eax], 15, 8);
600 capp->width = bitfield32(reg[eax], 23, 16);
601 capp->events_number = bitfield32(reg[eax], 31, 24);
602 capp->events = reg[ebx];
603 capp->fixed_number = bitfield32(reg[edx], 4, 0);
604 capp->fixed_width = bitfield32(reg[edx], 12, 5);
605 info_p->cpuid_arch_perf_leafp = capp;
0c530ab8 606 }
55e303ae 607
060df5ea
A
608 if (info_p->cpuid_max_basic >= 0xd) {
609 cpuid_xsave_leaf_t *xsp = &info_p->cpuid_xsave_leaf;
610 /*
611 * XSAVE Features:
612 */
613 cpuid_fn(0xd, info_p->cpuid_xsave_leaf.extended_state);
614 info_p->cpuid_xsave_leafp = xsp;
615 }
616
55e303ae
A
617 return;
618}
619
7e4a7d39
A
620static uint32_t
621cpuid_set_cpufamily(i386_cpu_info_t *info_p)
622{
623 uint32_t cpufamily = CPUFAMILY_UNKNOWN;
624
625 switch (info_p->cpuid_family) {
626 case 6:
627 switch (info_p->cpuid_model) {
628 case 13:
629 cpufamily = CPUFAMILY_INTEL_6_13;
630 break;
631 case 14:
632 cpufamily = CPUFAMILY_INTEL_YONAH;
633 break;
634 case 15:
635 cpufamily = CPUFAMILY_INTEL_MEROM;
636 break;
637 case 23:
638 cpufamily = CPUFAMILY_INTEL_PENRYN;
639 break;
640 case CPUID_MODEL_NEHALEM:
641 case CPUID_MODEL_FIELDS:
642 case CPUID_MODEL_DALES:
643 case CPUID_MODEL_NEHALEM_EX:
644 cpufamily = CPUFAMILY_INTEL_NEHALEM;
645 break;
d1ecb069
A
646 case CPUID_MODEL_DALES_32NM:
647 case CPUID_MODEL_WESTMERE:
648 case CPUID_MODEL_WESTMERE_EX:
649 cpufamily = CPUFAMILY_INTEL_WESTMERE;
650 break;
060df5ea
A
651 case CPUID_MODEL_SANDYBRIDGE:
652 case CPUID_MODEL_JAKETOWN:
653 cpufamily = CPUFAMILY_INTEL_SANDYBRIDGE;
654 break;
7e4a7d39
A
655 }
656 break;
657 }
658
659 info_p->cpuid_cpufamily = cpufamily;
660 return cpufamily;
661}
060df5ea
A
662/*
663 * Must be invoked either when executing single threaded, or with
664 * independent synchronization.
665 */
2d21ac55
A
666void
667cpuid_set_info(void)
d7e50217 668{
7e4a7d39
A
669 i386_cpu_info_t *info_p = &cpuid_cpu_info;
670
671 bzero((void *)info_p, sizeof(cpuid_cpu_info));
2d21ac55 672
7e4a7d39 673 cpuid_set_generic_info(info_p);
55e303ae 674
2d21ac55 675 /* verify we are running on a supported CPU */
7e4a7d39 676 if ((strncmp(CPUID_VID_INTEL, info_p->cpuid_vendor,
2d21ac55 677 min(strlen(CPUID_STRING_UNKNOWN) + 1,
7e4a7d39
A
678 sizeof(info_p->cpuid_vendor)))) ||
679 (cpuid_set_cpufamily(info_p) == CPUFAMILY_UNKNOWN))
2d21ac55
A
680 panic("Unsupported CPU");
681
7e4a7d39
A
682 info_p->cpuid_cpu_type = CPU_TYPE_X86;
683 info_p->cpuid_cpu_subtype = CPU_SUBTYPE_X86_ARCH1;
2d21ac55
A
684
685 cpuid_set_cache_info(&cpuid_cpu_info);
686
7e4a7d39
A
687 /*
688 * Find the number of enabled cores and threads
689 * (which determines whether SMT/Hyperthreading is active).
690 */
691 switch (info_p->cpuid_cpufamily) {
d1ecb069
A
692 case CPUFAMILY_INTEL_WESTMERE: {
693 uint64_t msr = rdmsr64(MSR_CORE_THREAD_COUNT);
694 info_p->core_count = bitfield32((uint32_t)msr, 19, 16);
695 info_p->thread_count = bitfield32((uint32_t)msr, 15, 0);
696 break;
697 }
060df5ea 698 case CPUFAMILY_INTEL_SANDYBRIDGE:
7e4a7d39
A
699 case CPUFAMILY_INTEL_NEHALEM: {
700 uint64_t msr = rdmsr64(MSR_CORE_THREAD_COUNT);
701 info_p->core_count = bitfield32((uint32_t)msr, 31, 16);
702 info_p->thread_count = bitfield32((uint32_t)msr, 15, 0);
703 break;
704 }
705 }
706 if (info_p->core_count == 0) {
707 info_p->core_count = info_p->cpuid_cores_per_package;
708 info_p->thread_count = info_p->cpuid_logical_per_package;
593a1d5f
A
709 }
710
2d21ac55
A
711 cpuid_cpu_info.cpuid_model_string = ""; /* deprecated */
712}
55e303ae
A
713
714static struct {
0c530ab8 715 uint64_t mask;
91447636 716 const char *name;
0c530ab8 717} feature_map[] = {
060df5ea
A
718 {CPUID_FEATURE_FPU, "FPU"},
719 {CPUID_FEATURE_VME, "VME"},
720 {CPUID_FEATURE_DE, "DE"},
721 {CPUID_FEATURE_PSE, "PSE"},
722 {CPUID_FEATURE_TSC, "TSC"},
723 {CPUID_FEATURE_MSR, "MSR"},
724 {CPUID_FEATURE_PAE, "PAE"},
725 {CPUID_FEATURE_MCE, "MCE"},
726 {CPUID_FEATURE_CX8, "CX8"},
727 {CPUID_FEATURE_APIC, "APIC"},
728 {CPUID_FEATURE_SEP, "SEP"},
729 {CPUID_FEATURE_MTRR, "MTRR"},
730 {CPUID_FEATURE_PGE, "PGE"},
731 {CPUID_FEATURE_MCA, "MCA"},
732 {CPUID_FEATURE_CMOV, "CMOV"},
733 {CPUID_FEATURE_PAT, "PAT"},
734 {CPUID_FEATURE_PSE36, "PSE36"},
735 {CPUID_FEATURE_PSN, "PSN"},
736 {CPUID_FEATURE_CLFSH, "CLFSH"},
737 {CPUID_FEATURE_DS, "DS"},
738 {CPUID_FEATURE_ACPI, "ACPI"},
739 {CPUID_FEATURE_MMX, "MMX"},
740 {CPUID_FEATURE_FXSR, "FXSR"},
741 {CPUID_FEATURE_SSE, "SSE"},
742 {CPUID_FEATURE_SSE2, "SSE2"},
743 {CPUID_FEATURE_SS, "SS"},
744 {CPUID_FEATURE_HTT, "HTT"},
745 {CPUID_FEATURE_TM, "TM"},
746 {CPUID_FEATURE_PBE, "PBE"},
747 {CPUID_FEATURE_SSE3, "SSE3"},
d1ecb069 748 {CPUID_FEATURE_PCLMULQDQ, "PCLMULQDQ"},
060df5ea
A
749 {CPUID_FEATURE_DTES64, "DTES64"},
750 {CPUID_FEATURE_MONITOR, "MON"},
751 {CPUID_FEATURE_DSCPL, "DSCPL"},
752 {CPUID_FEATURE_VMX, "VMX"},
753 {CPUID_FEATURE_SMX, "SMX"},
754 {CPUID_FEATURE_EST, "EST"},
755 {CPUID_FEATURE_TM2, "TM2"},
756 {CPUID_FEATURE_SSSE3, "SSSE3"},
757 {CPUID_FEATURE_CID, "CID"},
758 {CPUID_FEATURE_CX16, "CX16"},
759 {CPUID_FEATURE_xTPR, "TPR"},
760 {CPUID_FEATURE_PDCM, "PDCM"},
761 {CPUID_FEATURE_SSE4_1, "SSE4.1"},
762 {CPUID_FEATURE_SSE4_2, "SSE4.2"},
763 {CPUID_FEATURE_xAPIC, "xAPIC"},
764 {CPUID_FEATURE_MOVBE, "MOVBE"},
765 {CPUID_FEATURE_POPCNT, "POPCNT"},
766 {CPUID_FEATURE_AES, "AES"},
767 {CPUID_FEATURE_XSAVE, "XSAVE"},
768 {CPUID_FEATURE_OSXSAVE, "OSXSAVE"},
769 {CPUID_FEATURE_VMM, "VMM"},
770 {CPUID_FEATURE_SEGLIM64, "SEGLIM64"},
771 {CPUID_FEATURE_PCID, "PCID"},
772 {CPUID_FEATURE_TSCTMR, "TSCTMR"},
773 {CPUID_FEATURE_AVX1_0, "AVX1.0"},
0c530ab8
A
774 {0, 0}
775},
776extfeature_map[] = {
777 {CPUID_EXTFEATURE_SYSCALL, "SYSCALL"},
778 {CPUID_EXTFEATURE_XD, "XD"},
d1ecb069 779 {CPUID_EXTFEATURE_1GBPAGE, "1GBPAGE"},
0c530ab8
A
780 {CPUID_EXTFEATURE_EM64T, "EM64T"},
781 {CPUID_EXTFEATURE_LAHF, "LAHF"},
060df5ea 782 {CPUID_EXTFEATURE_RDTSCP, "RDTSCP"},
c910b4d9 783 {CPUID_EXTFEATURE_TSCI, "TSCI"},
55e303ae
A
784 {0, 0}
785};
786
0c530ab8
A
787i386_cpu_info_t *
788cpuid_info(void)
789{
593a1d5f 790 /* Set-up the cpuid_info stucture lazily */
0c530ab8 791 if (cpuid_cpu_infop == NULL) {
2d21ac55 792 cpuid_set_info();
0c530ab8
A
793 cpuid_cpu_infop = &cpuid_cpu_info;
794 }
795 return cpuid_cpu_infop;
796}
797
55e303ae 798char *
0c530ab8 799cpuid_get_feature_names(uint64_t features, char *buf, unsigned buf_len)
55e303ae 800{
060df5ea 801 size_t len = 0;
0c530ab8 802 char *p = buf;
55e303ae 803 int i;
0c530ab8
A
804
805 for (i = 0; feature_map[i].mask != 0; i++) {
806 if ((features & feature_map[i].mask) == 0)
807 continue;
060df5ea 808 if (len && ((size_t)(p - buf) < (buf_len - 1)))
0c530ab8 809 *p++ = ' ';
060df5ea 810
b0d623f7 811 len = min(strlen(feature_map[i].name), (size_t) ((buf_len-1) - (p-buf)));
0c530ab8
A
812 if (len == 0)
813 break;
814 bcopy(feature_map[i].name, p, len);
815 p += len;
816 }
817 *p = '\0';
818 return buf;
819}
820
821char *
822cpuid_get_extfeature_names(uint64_t extfeatures, char *buf, unsigned buf_len)
823{
060df5ea 824 size_t len = 0;
55e303ae 825 char *p = buf;
0c530ab8 826 int i;
55e303ae 827
0c530ab8
A
828 for (i = 0; extfeature_map[i].mask != 0; i++) {
829 if ((extfeatures & extfeature_map[i].mask) == 0)
55e303ae 830 continue;
060df5ea 831 if (len && ((size_t) (p - buf) < (buf_len - 1)))
55e303ae 832 *p++ = ' ';
b0d623f7 833 len = min(strlen(extfeature_map[i].name), (size_t) ((buf_len-1)-(p-buf)));
55e303ae
A
834 if (len == 0)
835 break;
0c530ab8 836 bcopy(extfeature_map[i].name, p, len);
55e303ae
A
837 p += len;
838 }
839 *p = '\0';
840 return buf;
841}
842
2d21ac55 843
55e303ae
A
844void
845cpuid_feature_display(
0c530ab8
A
846 const char *header)
847{
848 char buf[256];
849
850 kprintf("%s: %s\n", header,
851 cpuid_get_feature_names(cpuid_features(),
852 buf, sizeof(buf)));
853 if (cpuid_features() & CPUID_FEATURE_HTT) {
854#define s_if_plural(n) ((n > 1) ? "s" : "")
855 kprintf(" HTT: %d core%s per package;"
856 " %d logical cpu%s per package\n",
857 cpuid_cpu_info.cpuid_cores_per_package,
858 s_if_plural(cpuid_cpu_info.cpuid_cores_per_package),
859 cpuid_cpu_info.cpuid_logical_per_package,
860 s_if_plural(cpuid_cpu_info.cpuid_logical_per_package));
861 }
862}
863
864void
865cpuid_extfeature_display(
866 const char *header)
c0fea474
A
867{
868 char buf[256];
869
0c530ab8
A
870 kprintf("%s: %s\n", header,
871 cpuid_get_extfeature_names(cpuid_extfeatures(),
872 buf, sizeof(buf)));
1c79356b
A
873}
874
1c79356b
A
875void
876cpuid_cpu_display(
0c530ab8 877 const char *header)
d7e50217 878{
2d21ac55 879 if (cpuid_cpu_info.cpuid_brand_string[0] != '\0') {
0c530ab8 880 kprintf("%s: %s\n", header, cpuid_cpu_info.cpuid_brand_string);
91447636 881 }
d7e50217
A
882}
883
55e303ae
A
884unsigned int
885cpuid_family(void)
886{
0c530ab8 887 return cpuid_info()->cpuid_family;
4452a7af
A
888}
889
7e4a7d39
A
890uint32_t
891cpuid_cpufamily(void)
892{
893 return cpuid_info()->cpuid_cpufamily;
894}
895
0c530ab8
A
896cpu_type_t
897cpuid_cputype(void)
898{
899 return cpuid_info()->cpuid_cpu_type;
900}
901
902cpu_subtype_t
903cpuid_cpusubtype(void)
904{
905 return cpuid_info()->cpuid_cpu_subtype;
906}
907
908uint64_t
55e303ae
A
909cpuid_features(void)
910{
91447636 911 static int checked = 0;
593a1d5f 912 char fpu_arg[20] = { 0 };
0c530ab8
A
913
914 (void) cpuid_info();
91447636
A
915 if (!checked) {
916 /* check for boot-time fpu limitations */
593a1d5f 917 if (PE_parse_boot_argn("_fpu", &fpu_arg[0], sizeof (fpu_arg))) {
91447636 918 printf("limiting fpu features to: %s\n", fpu_arg);
2d21ac55 919 if (!strncmp("387", fpu_arg, sizeof("387")) || !strncmp("mmx", fpu_arg, sizeof("mmx"))) {
91447636
A
920 printf("no sse or sse2\n");
921 cpuid_cpu_info.cpuid_features &= ~(CPUID_FEATURE_SSE | CPUID_FEATURE_SSE2 | CPUID_FEATURE_FXSR);
2d21ac55 922 } else if (!strncmp("sse", fpu_arg, sizeof("sse"))) {
91447636
A
923 printf("no sse2\n");
924 cpuid_cpu_info.cpuid_features &= ~(CPUID_FEATURE_SSE2);
925 }
926 }
927 checked = 1;
928 }
55e303ae
A
929 return cpuid_cpu_info.cpuid_features;
930}
931
0c530ab8
A
932uint64_t
933cpuid_extfeatures(void)
55e303ae 934{
0c530ab8 935 return cpuid_info()->cpuid_extfeatures;
55e303ae 936}
0c530ab8 937
55e303ae 938
0c530ab8
A
939#if MACH_KDB
940
941/*
942 * Display the cpuid
943 * *
944 * cp
945 */
946void
947db_cpuid(__unused db_expr_t addr,
948 __unused int have_addr,
949 __unused db_expr_t count,
950 __unused char *modif)
951{
952
953 uint32_t i, mid;
954 uint32_t cpid[4];
955
956 do_cpuid(0, cpid); /* Get the first cpuid which is the number of
957 * basic ids */
958 db_printf("%08X - %08X %08X %08X %08X\n",
959 0, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
960
961 mid = cpid[eax]; /* Set the number */
962 for (i = 1; i <= mid; i++) { /* Dump 'em out */
963 do_cpuid(i, cpid); /* Get the next */
964 db_printf("%08X - %08X %08X %08X %08X\n",
965 i, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
966 }
967 db_printf("\n");
968
969 do_cpuid(0x80000000, cpid); /* Get the first extended cpuid which
970 * is the number of extended ids */
971 db_printf("%08X - %08X %08X %08X %08X\n",
972 0x80000000, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
973
974 mid = cpid[eax]; /* Set the number */
975 for (i = 0x80000001; i <= mid; i++) { /* Dump 'em out */
976 do_cpuid(i, cpid); /* Get the next */
977 db_printf("%08X - %08X %08X %08X %08X\n",
978 i, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
979 }
980}
981
982#endif