]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/i386/cpuid.c
xnu-1486.2.11.tar.gz
[apple/xnu.git] / osfmk / i386 / cpuid.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31#include <platforms.h>
32#include <mach_kdb.h>
33#include <vm/vm_page.h>
34#include <pexpert/pexpert.h>
35
36#include <i386/cpuid.h>
37#if MACH_KDB
38#include <machine/db_machdep.h>
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
47
48#define min(a,b) ((a) < (b) ? (a) : (b))
49#define quad(hi,lo) (((uint64_t)(hi)) << 32 | (lo))
50
51/* Only for 32bit values */
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))
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 },
188 { 0xD3, CACHE, L3, 4, 4*M, 64 },
189 { 0xD4, CACHE, L3, 4, 8*M, 64 },
190 { 0xD6, CACHE, L3, 8, 1*M, 64 },
191 { 0xD7, CACHE, L3, 8, 2*M, 64 },
192 { 0xD8, CACHE, L3, 8, 4*M, 64 },
193 { 0xD9, CACHE, L3, 8, 8*M, 64 },
194 { 0xDA, CACHE, L3, 8, 12*M, 64 },
195 { 0xDC, CACHE, L3, 12, 1536*K, 64 },
196 { 0xDD, CACHE, L3, 12, 3*M, 64 },
197 { 0xDE, CACHE, L3, 12, 6*M, 64 },
198 { 0xDF, CACHE, L3, 12, 12*M, 64 },
199 { 0xE0, CACHE, L3, 12, 18*M, 64 },
200 { 0xE2, CACHE, L3, 16, 2*M, 64 },
201 { 0xE3, CACHE, L3, 16, 4*M, 64 },
202 { 0xE4, CACHE, L3, 16, 8*M, 64 },
203 { 0xE5, CACHE, L3, 16, 16*M, 64 },
204 { 0xE6, CACHE, L3, 16, 24*M, 64 },
205 { 0xF0, PREFETCH, NA, NA, 64, NA },
206 { 0xF1, PREFETCH, NA, NA, 128, NA }
207};
208#define INTEL_LEAF2_DESC_NUM (sizeof(intel_cpuid_leaf2_descriptor_table) / \
209 sizeof(cpuid_cache_descriptor_t))
210
211static inline cpuid_cache_descriptor_t *
212cpuid_leaf2_find(uint8_t value)
213{
214 unsigned int i;
215
216 for (i = 0; i < INTEL_LEAF2_DESC_NUM; i++)
217 if (intel_cpuid_leaf2_descriptor_table[i].value == value)
218 return &intel_cpuid_leaf2_descriptor_table[i];
219 return NULL;
220}
221
222/*
223 * CPU identification routines.
224 */
225
226static i386_cpu_info_t *cpuid_cpu_infop = NULL;
227static i386_cpu_info_t cpuid_cpu_info;
228
229#if defined(__x86_64__)
230static void cpuid_fn(uint32_t selector, uint32_t *result)
231{
232 do_cpuid(selector, result);
233}
234#else
235static void cpuid_fn(uint32_t selector, uint32_t *result)
236{
237 if (cpu_mode_is64bit()) {
238 asm("call _cpuid64"
239 : "=a" (result[0]),
240 "=b" (result[1]),
241 "=c" (result[2]),
242 "=d" (result[3])
243 : "a"(selector));
244 } else {
245 do_cpuid(selector, result);
246 }
247}
248#endif
249
250/* this function is Intel-specific */
251static void
252cpuid_set_cache_info( i386_cpu_info_t * info_p )
253{
254 uint32_t cpuid_result[4];
255 uint32_t reg[4];
256 uint32_t index;
257 uint32_t linesizes[LCACHE_MAX];
258 unsigned int i;
259 unsigned int j;
260 boolean_t cpuid_deterministic_supported = FALSE;
261
262 bzero( linesizes, sizeof(linesizes) );
263
264 /* Get processor cache descriptor info using leaf 2. We don't use
265 * this internally, but must publish it for KEXTs.
266 */
267 cpuid_fn(2, cpuid_result);
268 for (j = 0; j < 4; j++) {
269 if ((cpuid_result[j] >> 31) == 1) /* bit31 is validity */
270 continue;
271 ((uint32_t *) info_p->cache_info)[j] = cpuid_result[j];
272 }
273 /* first byte gives number of cpuid calls to get all descriptors */
274 for (i = 1; i < info_p->cache_info[0]; i++) {
275 if (i*16 > sizeof(info_p->cache_info))
276 break;
277 cpuid_fn(2, cpuid_result);
278 for (j = 0; j < 4; j++) {
279 if ((cpuid_result[j] >> 31) == 1)
280 continue;
281 ((uint32_t *) info_p->cache_info)[4*i+j] =
282 cpuid_result[j];
283 }
284 }
285
286 /*
287 * Get cache info using leaf 4, the "deterministic cache parameters."
288 * Most processors Mac OS X supports implement this flavor of CPUID.
289 * Loop over each cache on the processor.
290 */
291 cpuid_fn(0, cpuid_result);
292 if (cpuid_result[eax] >= 4)
293 cpuid_deterministic_supported = TRUE;
294
295 for (index = 0; cpuid_deterministic_supported; index++) {
296 cache_type_t type = Lnone;
297 uint32_t cache_type;
298 uint32_t cache_level;
299 uint32_t cache_sharing;
300 uint32_t cache_linesize;
301 uint32_t cache_sets;
302 uint32_t cache_associativity;
303 uint32_t cache_size;
304 uint32_t cache_partitions;
305 uint32_t colors;
306
307 reg[eax] = 4; /* cpuid request 4 */
308 reg[ecx] = index; /* index starting at 0 */
309 cpuid(reg);
310//kprintf("cpuid(4) index=%d eax=%p\n", index, reg[eax]);
311 cache_type = bitfield32(reg[eax], 4, 0);
312 if (cache_type == 0)
313 break; /* no more caches */
314 cache_level = bitfield32(reg[eax], 7, 5);
315 cache_sharing = bitfield32(reg[eax], 25, 14) + 1;
316 info_p->cpuid_cores_per_package
317 = bitfield32(reg[eax], 31, 26) + 1;
318 cache_linesize = bitfield32(reg[ebx], 11, 0) + 1;
319 cache_partitions = bitfield32(reg[ebx], 21, 12) + 1;
320 cache_associativity = bitfield32(reg[ebx], 31, 22) + 1;
321 cache_sets = bitfield32(reg[ecx], 31, 0) + 1;
322
323 /* Map type/levels returned by CPUID into cache_type_t */
324 switch (cache_level) {
325 case 1:
326 type = cache_type == 1 ? L1D :
327 cache_type == 2 ? L1I :
328 Lnone;
329 break;
330 case 2:
331 type = cache_type == 3 ? L2U :
332 Lnone;
333 break;
334 case 3:
335 type = cache_type == 3 ? L3U :
336 Lnone;
337 break;
338 default:
339 type = Lnone;
340 }
341
342 /* The total size of a cache is:
343 * ( linesize * sets * associativity * partitions )
344 */
345 if (type != Lnone) {
346 cache_size = cache_linesize * cache_sets *
347 cache_associativity * cache_partitions;
348 info_p->cache_size[type] = cache_size;
349 info_p->cache_sharing[type] = cache_sharing;
350 info_p->cache_partitions[type] = cache_partitions;
351 linesizes[type] = cache_linesize;
352
353 /* Compute the number of page colors for this cache,
354 * which is:
355 * ( linesize * sets ) / page_size
356 *
357 * To help visualize this, consider two views of a
358 * physical address. To the cache, it is composed
359 * of a line offset, a set selector, and a tag.
360 * To VM, it is composed of a page offset, a page
361 * color, and other bits in the pageframe number:
362 *
363 * +-----------------+---------+--------+
364 * cache: | tag | set | offset |
365 * +-----------------+---------+--------+
366 *
367 * +-----------------+-------+----------+
368 * VM: | don't care | color | pg offset|
369 * +-----------------+-------+----------+
370 *
371 * The color is those bits in (set+offset) not covered
372 * by the page offset.
373 */
374 colors = ( cache_linesize * cache_sets ) >> 12;
375
376 if ( colors > vm_cache_geometry_colors )
377 vm_cache_geometry_colors = colors;
378 }
379 }
380
381 /*
382 * If deterministic cache parameters are not available, use
383 * something else
384 */
385 if (info_p->cpuid_cores_per_package == 0) {
386 info_p->cpuid_cores_per_package = 1;
387
388 /* cpuid define in 1024 quantities */
389 info_p->cache_size[L2U] = info_p->cpuid_cache_size * 1024;
390 info_p->cache_sharing[L2U] = 1;
391 info_p->cache_partitions[L2U] = 1;
392
393 linesizes[L2U] = info_p->cpuid_cache_linesize;
394 }
395
396 /*
397 * What linesize to publish? We use the L2 linesize if any,
398 * else the L1D.
399 */
400 if ( linesizes[L2U] )
401 info_p->cache_linesize = linesizes[L2U];
402 else if (linesizes[L1D])
403 info_p->cache_linesize = linesizes[L1D];
404 else panic("no linesize");
405
406 /*
407 * Extract and publish TLB information from Leaf 2 descriptors.
408 */
409 for (i = 1; i < sizeof(info_p->cache_info); i++) {
410 cpuid_cache_descriptor_t *descp;
411 int id;
412 int level;
413 int page;
414
415 descp = cpuid_leaf2_find(info_p->cache_info[i]);
416 if (descp == NULL)
417 continue;
418
419 switch (descp->type) {
420 case TLB:
421 page = (descp->size == SMALL) ? TLB_SMALL : TLB_LARGE;
422 /* determine I or D: */
423 switch (descp->level) {
424 case INST:
425 id = TLB_INST;
426 break;
427 case DATA:
428 case DATA0:
429 case DATA1:
430 id = TLB_DATA;
431 break;
432 default:
433 continue;
434 }
435 /* determine level: */
436 switch (descp->level) {
437 case DATA1:
438 level = 1;
439 break;
440 default:
441 level = 0;
442 }
443 info_p->cpuid_tlb[id][page][level] = descp->entries;
444 break;
445 case STLB:
446 info_p->cpuid_stlb = descp->entries;
447 }
448 }
449}
450
451static void
452cpuid_set_generic_info(i386_cpu_info_t *info_p)
453{
454 uint32_t reg[4];
455 char str[128], *p;
456
457 /* do cpuid 0 to get vendor */
458 cpuid_fn(0, reg);
459 info_p->cpuid_max_basic = reg[eax];
460 bcopy((char *)&reg[ebx], &info_p->cpuid_vendor[0], 4); /* ug */
461 bcopy((char *)&reg[ecx], &info_p->cpuid_vendor[8], 4);
462 bcopy((char *)&reg[edx], &info_p->cpuid_vendor[4], 4);
463 info_p->cpuid_vendor[12] = 0;
464
465 /* get extended cpuid results */
466 cpuid_fn(0x80000000, reg);
467 info_p->cpuid_max_ext = reg[eax];
468
469 /* check to see if we can get brand string */
470 if (info_p->cpuid_max_ext >= 0x80000004) {
471 /*
472 * The brand string 48 bytes (max), guaranteed to
473 * be NUL terminated.
474 */
475 cpuid_fn(0x80000002, reg);
476 bcopy((char *)reg, &str[0], 16);
477 cpuid_fn(0x80000003, reg);
478 bcopy((char *)reg, &str[16], 16);
479 cpuid_fn(0x80000004, reg);
480 bcopy((char *)reg, &str[32], 16);
481 for (p = str; *p != '\0'; p++) {
482 if (*p != ' ') break;
483 }
484 strlcpy(info_p->cpuid_brand_string,
485 p, sizeof(info_p->cpuid_brand_string));
486
487 if (!strncmp(info_p->cpuid_brand_string, CPUID_STRING_UNKNOWN,
488 min(sizeof(info_p->cpuid_brand_string),
489 strlen(CPUID_STRING_UNKNOWN) + 1))) {
490 /*
491 * This string means we have a firmware-programmable brand string,
492 * and the firmware couldn't figure out what sort of CPU we have.
493 */
494 info_p->cpuid_brand_string[0] = '\0';
495 }
496 }
497
498 /* Get cache and addressing info. */
499 if (info_p->cpuid_max_ext >= 0x80000006) {
500 cpuid_fn(0x80000006, reg);
501 info_p->cpuid_cache_linesize = bitfield32(reg[ecx], 7, 0);
502 info_p->cpuid_cache_L2_associativity =
503 bitfield32(reg[ecx],15,12);
504 info_p->cpuid_cache_size = bitfield32(reg[ecx],31,16);
505 cpuid_fn(0x80000008, reg);
506 info_p->cpuid_address_bits_physical =
507 bitfield32(reg[eax], 7, 0);
508 info_p->cpuid_address_bits_virtual =
509 bitfield32(reg[eax],15, 8);
510 }
511
512 /* get processor signature and decode */
513 cpuid_fn(1, reg);
514 info_p->cpuid_signature = reg[eax];
515 info_p->cpuid_stepping = bitfield32(reg[eax], 3, 0);
516 info_p->cpuid_model = bitfield32(reg[eax], 7, 4);
517 info_p->cpuid_family = bitfield32(reg[eax], 11, 8);
518 info_p->cpuid_type = bitfield32(reg[eax], 13, 12);
519 info_p->cpuid_extmodel = bitfield32(reg[eax], 19, 16);
520 info_p->cpuid_extfamily = bitfield32(reg[eax], 27, 20);
521 info_p->cpuid_brand = bitfield32(reg[ebx], 7, 0);
522 info_p->cpuid_features = quad(reg[ecx], reg[edx]);
523
524 /* Fold extensions into family/model */
525 if (info_p->cpuid_family == 0x0f)
526 info_p->cpuid_family += info_p->cpuid_extfamily;
527 if (info_p->cpuid_family == 0x0f || info_p->cpuid_family == 0x06)
528 info_p->cpuid_model += (info_p->cpuid_extmodel << 4);
529
530 if (info_p->cpuid_features & CPUID_FEATURE_HTT)
531 info_p->cpuid_logical_per_package =
532 bitfield32(reg[ebx], 23, 16);
533 else
534 info_p->cpuid_logical_per_package = 1;
535
536 if (info_p->cpuid_max_ext >= 0x80000001) {
537 cpuid_fn(0x80000001, reg);
538 info_p->cpuid_extfeatures =
539 quad(reg[ecx], reg[edx]);
540 }
541
542 /* Fold in the Invariant TSC feature bit, if present */
543 if (info_p->cpuid_max_ext >= 0x80000007) {
544 cpuid_fn(0x80000007, reg);
545 info_p->cpuid_extfeatures |=
546 reg[edx] & (uint32_t)CPUID_EXTFEATURE_TSCI;
547 }
548
549 /* Find the microcode version number a.k.a. signature a.k.a. BIOS ID */
550 info_p->cpuid_microcode_version =
551 (uint32_t) (rdmsr64(MSR_IA32_BIOS_SIGN_ID) >> 32);
552
553 if (info_p->cpuid_max_basic >= 0x5) {
554 cpuid_mwait_leaf_t *cmp = &info_p->cpuid_mwait_leaf;
555
556 /*
557 * Extract the Monitor/Mwait Leaf info:
558 */
559 cpuid_fn(5, reg);
560 cmp->linesize_min = reg[eax];
561 cmp->linesize_max = reg[ebx];
562 cmp->extensions = reg[ecx];
563 cmp->sub_Cstates = reg[edx];
564 info_p->cpuid_mwait_leafp = cmp;
565 }
566
567 if (info_p->cpuid_max_basic >= 0x6) {
568 cpuid_thermal_leaf_t *ctp = &info_p->cpuid_thermal_leaf;
569
570 /*
571 * The thermal and Power Leaf:
572 */
573 cpuid_fn(6, reg);
574 ctp->sensor = bitfield32(reg[eax], 0, 0);
575 ctp->dynamic_acceleration = bitfield32(reg[eax], 1, 1);
576 ctp->thresholds = bitfield32(reg[ebx], 3, 0);
577 ctp->ACNT_MCNT = bitfield32(reg[ecx], 0, 0);
578 info_p->cpuid_thermal_leafp = ctp;
579 }
580
581 if (info_p->cpuid_max_basic >= 0xa) {
582 cpuid_arch_perf_leaf_t *capp = &info_p->cpuid_arch_perf_leaf;
583
584 /*
585 * Architectural Performance Monitoring Leaf:
586 */
587 cpuid_fn(0xa, reg);
588 capp->version = bitfield32(reg[eax], 7, 0);
589 capp->number = bitfield32(reg[eax], 15, 8);
590 capp->width = bitfield32(reg[eax], 23, 16);
591 capp->events_number = bitfield32(reg[eax], 31, 24);
592 capp->events = reg[ebx];
593 capp->fixed_number = bitfield32(reg[edx], 4, 0);
594 capp->fixed_width = bitfield32(reg[edx], 12, 5);
595 info_p->cpuid_arch_perf_leafp = capp;
596 }
597
598 return;
599}
600
601static uint32_t
602cpuid_set_cpufamily(i386_cpu_info_t *info_p)
603{
604 uint32_t cpufamily = CPUFAMILY_UNKNOWN;
605
606 switch (info_p->cpuid_family) {
607 case 6:
608 switch (info_p->cpuid_model) {
609 case 13:
610 cpufamily = CPUFAMILY_INTEL_6_13;
611 break;
612 case 14:
613 cpufamily = CPUFAMILY_INTEL_YONAH;
614 break;
615 case 15:
616 cpufamily = CPUFAMILY_INTEL_MEROM;
617 break;
618 case 23:
619 cpufamily = CPUFAMILY_INTEL_PENRYN;
620 break;
621 case CPUID_MODEL_NEHALEM:
622 case CPUID_MODEL_FIELDS:
623 case CPUID_MODEL_DALES:
624 case CPUID_MODEL_NEHALEM_EX:
625 cpufamily = CPUFAMILY_INTEL_NEHALEM;
626 break;
627 }
628 break;
629 }
630
631 info_p->cpuid_cpufamily = cpufamily;
632 return cpufamily;
633}
634
635void
636cpuid_set_info(void)
637{
638 i386_cpu_info_t *info_p = &cpuid_cpu_info;
639
640 bzero((void *)info_p, sizeof(cpuid_cpu_info));
641
642 cpuid_set_generic_info(info_p);
643
644 /* verify we are running on a supported CPU */
645 if ((strncmp(CPUID_VID_INTEL, info_p->cpuid_vendor,
646 min(strlen(CPUID_STRING_UNKNOWN) + 1,
647 sizeof(info_p->cpuid_vendor)))) ||
648 (cpuid_set_cpufamily(info_p) == CPUFAMILY_UNKNOWN))
649 panic("Unsupported CPU");
650
651 info_p->cpuid_cpu_type = CPU_TYPE_X86;
652 info_p->cpuid_cpu_subtype = CPU_SUBTYPE_X86_ARCH1;
653
654 cpuid_set_cache_info(&cpuid_cpu_info);
655
656 /*
657 * Find the number of enabled cores and threads
658 * (which determines whether SMT/Hyperthreading is active).
659 */
660 switch (info_p->cpuid_cpufamily) {
661 case CPUFAMILY_INTEL_NEHALEM: {
662 uint64_t msr = rdmsr64(MSR_CORE_THREAD_COUNT);
663 info_p->core_count = bitfield32((uint32_t)msr, 31, 16);
664 info_p->thread_count = bitfield32((uint32_t)msr, 15, 0);
665 break;
666 }
667 }
668 if (info_p->core_count == 0) {
669 info_p->core_count = info_p->cpuid_cores_per_package;
670 info_p->thread_count = info_p->cpuid_logical_per_package;
671 }
672
673 cpuid_cpu_info.cpuid_model_string = ""; /* deprecated */
674}
675
676static struct {
677 uint64_t mask;
678 const char *name;
679} feature_map[] = {
680 {CPUID_FEATURE_FPU, "FPU",},
681 {CPUID_FEATURE_VME, "VME",},
682 {CPUID_FEATURE_DE, "DE",},
683 {CPUID_FEATURE_PSE, "PSE",},
684 {CPUID_FEATURE_TSC, "TSC",},
685 {CPUID_FEATURE_MSR, "MSR",},
686 {CPUID_FEATURE_PAE, "PAE",},
687 {CPUID_FEATURE_MCE, "MCE",},
688 {CPUID_FEATURE_CX8, "CX8",},
689 {CPUID_FEATURE_APIC, "APIC",},
690 {CPUID_FEATURE_SEP, "SEP",},
691 {CPUID_FEATURE_MTRR, "MTRR",},
692 {CPUID_FEATURE_PGE, "PGE",},
693 {CPUID_FEATURE_MCA, "MCA",},
694 {CPUID_FEATURE_CMOV, "CMOV",},
695 {CPUID_FEATURE_PAT, "PAT",},
696 {CPUID_FEATURE_PSE36, "PSE36",},
697 {CPUID_FEATURE_PSN, "PSN",},
698 {CPUID_FEATURE_CLFSH, "CLFSH",},
699 {CPUID_FEATURE_DS, "DS",},
700 {CPUID_FEATURE_ACPI, "ACPI",},
701 {CPUID_FEATURE_MMX, "MMX",},
702 {CPUID_FEATURE_FXSR, "FXSR",},
703 {CPUID_FEATURE_SSE, "SSE",},
704 {CPUID_FEATURE_SSE2, "SSE2",},
705 {CPUID_FEATURE_SS, "SS",},
706 {CPUID_FEATURE_HTT, "HTT",},
707 {CPUID_FEATURE_TM, "TM",},
708 {CPUID_FEATURE_SSE3, "SSE3"},
709 {CPUID_FEATURE_MONITOR, "MON"},
710 {CPUID_FEATURE_DSCPL, "DSCPL"},
711 {CPUID_FEATURE_VMX, "VMX"},
712 {CPUID_FEATURE_SMX, "SMX"},
713 {CPUID_FEATURE_EST, "EST"},
714 {CPUID_FEATURE_TM2, "TM2"},
715 {CPUID_FEATURE_SSSE3, "SSSE3"},
716 {CPUID_FEATURE_CID, "CID"},
717 {CPUID_FEATURE_CX16, "CX16"},
718 {CPUID_FEATURE_xTPR, "TPR"},
719 {CPUID_FEATURE_PDCM, "PDCM"},
720 {CPUID_FEATURE_SSE4_1, "SSE4.1"},
721 {CPUID_FEATURE_SSE4_2, "SSE4.2"},
722 {CPUID_FEATURE_xAPIC, "xAPIC"},
723 {CPUID_FEATURE_POPCNT, "POPCNT"},
724 {CPUID_FEATURE_VMM, "VMM"},
725 {0, 0}
726},
727extfeature_map[] = {
728 {CPUID_EXTFEATURE_SYSCALL, "SYSCALL"},
729 {CPUID_EXTFEATURE_XD, "XD"},
730 {CPUID_EXTFEATURE_EM64T, "EM64T"},
731 {CPUID_EXTFEATURE_LAHF, "LAHF"},
732 {CPUID_EXTFEATURE_RDTSCP, "RDTSCP"},
733 {CPUID_EXTFEATURE_TSCI, "TSCI"},
734 {0, 0}
735};
736
737i386_cpu_info_t *
738cpuid_info(void)
739{
740 /* Set-up the cpuid_info stucture lazily */
741 if (cpuid_cpu_infop == NULL) {
742 cpuid_set_info();
743 cpuid_cpu_infop = &cpuid_cpu_info;
744 }
745 return cpuid_cpu_infop;
746}
747
748char *
749cpuid_get_feature_names(uint64_t features, char *buf, unsigned buf_len)
750{
751 size_t len = -1;
752 char *p = buf;
753 int i;
754
755 for (i = 0; feature_map[i].mask != 0; i++) {
756 if ((features & feature_map[i].mask) == 0)
757 continue;
758 if (len > 0)
759 *p++ = ' ';
760 len = min(strlen(feature_map[i].name), (size_t) ((buf_len-1) - (p-buf)));
761 if (len == 0)
762 break;
763 bcopy(feature_map[i].name, p, len);
764 p += len;
765 }
766 *p = '\0';
767 return buf;
768}
769
770char *
771cpuid_get_extfeature_names(uint64_t extfeatures, char *buf, unsigned buf_len)
772{
773 size_t len = -1;
774 char *p = buf;
775 int i;
776
777 for (i = 0; extfeature_map[i].mask != 0; i++) {
778 if ((extfeatures & extfeature_map[i].mask) == 0)
779 continue;
780 if (len > 0)
781 *p++ = ' ';
782 len = min(strlen(extfeature_map[i].name), (size_t) ((buf_len-1)-(p-buf)));
783 if (len == 0)
784 break;
785 bcopy(extfeature_map[i].name, p, len);
786 p += len;
787 }
788 *p = '\0';
789 return buf;
790}
791
792
793void
794cpuid_feature_display(
795 const char *header)
796{
797 char buf[256];
798
799 kprintf("%s: %s\n", header,
800 cpuid_get_feature_names(cpuid_features(),
801 buf, sizeof(buf)));
802 if (cpuid_features() & CPUID_FEATURE_HTT) {
803#define s_if_plural(n) ((n > 1) ? "s" : "")
804 kprintf(" HTT: %d core%s per package;"
805 " %d logical cpu%s per package\n",
806 cpuid_cpu_info.cpuid_cores_per_package,
807 s_if_plural(cpuid_cpu_info.cpuid_cores_per_package),
808 cpuid_cpu_info.cpuid_logical_per_package,
809 s_if_plural(cpuid_cpu_info.cpuid_logical_per_package));
810 }
811}
812
813void
814cpuid_extfeature_display(
815 const char *header)
816{
817 char buf[256];
818
819 kprintf("%s: %s\n", header,
820 cpuid_get_extfeature_names(cpuid_extfeatures(),
821 buf, sizeof(buf)));
822}
823
824void
825cpuid_cpu_display(
826 const char *header)
827{
828 if (cpuid_cpu_info.cpuid_brand_string[0] != '\0') {
829 kprintf("%s: %s\n", header, cpuid_cpu_info.cpuid_brand_string);
830 }
831}
832
833unsigned int
834cpuid_family(void)
835{
836 return cpuid_info()->cpuid_family;
837}
838
839uint32_t
840cpuid_cpufamily(void)
841{
842 return cpuid_info()->cpuid_cpufamily;
843}
844
845cpu_type_t
846cpuid_cputype(void)
847{
848 return cpuid_info()->cpuid_cpu_type;
849}
850
851cpu_subtype_t
852cpuid_cpusubtype(void)
853{
854 return cpuid_info()->cpuid_cpu_subtype;
855}
856
857uint64_t
858cpuid_features(void)
859{
860 static int checked = 0;
861 char fpu_arg[20] = { 0 };
862
863 (void) cpuid_info();
864 if (!checked) {
865 /* check for boot-time fpu limitations */
866 if (PE_parse_boot_argn("_fpu", &fpu_arg[0], sizeof (fpu_arg))) {
867 printf("limiting fpu features to: %s\n", fpu_arg);
868 if (!strncmp("387", fpu_arg, sizeof("387")) || !strncmp("mmx", fpu_arg, sizeof("mmx"))) {
869 printf("no sse or sse2\n");
870 cpuid_cpu_info.cpuid_features &= ~(CPUID_FEATURE_SSE | CPUID_FEATURE_SSE2 | CPUID_FEATURE_FXSR);
871 } else if (!strncmp("sse", fpu_arg, sizeof("sse"))) {
872 printf("no sse2\n");
873 cpuid_cpu_info.cpuid_features &= ~(CPUID_FEATURE_SSE2);
874 }
875 }
876 checked = 1;
877 }
878 return cpuid_cpu_info.cpuid_features;
879}
880
881uint64_t
882cpuid_extfeatures(void)
883{
884 return cpuid_info()->cpuid_extfeatures;
885}
886
887
888#if MACH_KDB
889
890/*
891 * Display the cpuid
892 * *
893 * cp
894 */
895void
896db_cpuid(__unused db_expr_t addr,
897 __unused int have_addr,
898 __unused db_expr_t count,
899 __unused char *modif)
900{
901
902 uint32_t i, mid;
903 uint32_t cpid[4];
904
905 do_cpuid(0, cpid); /* Get the first cpuid which is the number of
906 * basic ids */
907 db_printf("%08X - %08X %08X %08X %08X\n",
908 0, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
909
910 mid = cpid[eax]; /* Set the number */
911 for (i = 1; i <= mid; i++) { /* Dump 'em out */
912 do_cpuid(i, cpid); /* Get the next */
913 db_printf("%08X - %08X %08X %08X %08X\n",
914 i, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
915 }
916 db_printf("\n");
917
918 do_cpuid(0x80000000, cpid); /* Get the first extended cpuid which
919 * is the number of extended ids */
920 db_printf("%08X - %08X %08X %08X %08X\n",
921 0x80000000, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
922
923 mid = cpid[eax]; /* Set the number */
924 for (i = 0x80000001; i <= mid; i++) { /* Dump 'em out */
925 do_cpuid(i, cpid); /* Get the next */
926 db_printf("%08X - %08X %08X %08X %08X\n",
927 i, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
928 }
929}
930
931#endif