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