5955 pat_sync is clever enough to check for X86FSET_PAT
[illumos-gate.git] / usr / src / uts / i86pc / os / cpuid.c
blob586ec0656b28a65d26a695d367e290422f8a7699
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011 by Delphix. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2014 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
28 * Copyright (c) 2010, Intel Corporation.
29 * All rights reserved.
32 * Portions Copyright 2009 Advanced Micro Devices, Inc.
35 * Copyright (c) 2015, Joyent, Inc. All rights reserved.
38 * Various routines to handle identification
39 * and classification of x86 processors.
42 #include <sys/types.h>
43 #include <sys/archsystm.h>
44 #include <sys/x86_archext.h>
45 #include <sys/kmem.h>
46 #include <sys/systm.h>
47 #include <sys/cmn_err.h>
48 #include <sys/sunddi.h>
49 #include <sys/sunndi.h>
50 #include <sys/cpuvar.h>
51 #include <sys/processor.h>
52 #include <sys/sysmacros.h>
53 #include <sys/pg.h>
54 #include <sys/fp.h>
55 #include <sys/controlregs.h>
56 #include <sys/bitmap.h>
57 #include <sys/auxv_386.h>
58 #include <sys/memnode.h>
59 #include <sys/pci_cfgspace.h>
61 #ifdef __xpv
62 #include <sys/hypervisor.h>
63 #else
64 #include <sys/ontrap.h>
65 #endif
68 * Pass 0 of cpuid feature analysis happens in locore. It contains special code
69 * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
70 * them accordingly. For most modern processors, feature detection occurs here
71 * in pass 1.
73 * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
74 * for the boot CPU and does the basic analysis that the early kernel needs.
75 * x86_featureset is set based on the return value of cpuid_pass1() of the boot
76 * CPU.
78 * Pass 1 includes:
80 * o Determining vendor/model/family/stepping and setting x86_type and
81 * x86_vendor accordingly.
82 * o Processing the feature flags returned by the cpuid instruction while
83 * applying any workarounds or tricks for the specific processor.
84 * o Mapping the feature flags into Solaris feature bits (X86_*).
85 * o Processing extended feature flags if supported by the processor,
86 * again while applying specific processor knowledge.
87 * o Determining the CMT characteristics of the system.
89 * Pass 1 is done on non-boot CPUs during their initialization and the results
90 * are used only as a meager attempt at ensuring that all processors within the
91 * system support the same features.
93 * Pass 2 of cpuid feature analysis happens just at the beginning
94 * of startup(). It just copies in and corrects the remainder
95 * of the cpuid data we depend on: standard cpuid functions that we didn't
96 * need for pass1 feature analysis, and extended cpuid functions beyond the
97 * simple feature processing done in pass1.
99 * Pass 3 of cpuid analysis is invoked after basic kernel services; in
100 * particular kernel memory allocation has been made available. It creates a
101 * readable brand string based on the data collected in the first two passes.
103 * Pass 4 of cpuid analysis is invoked after post_startup() when all
104 * the support infrastructure for various hardware features has been
105 * initialized. It determines which processor features will be reported
106 * to userland via the aux vector.
108 * All passes are executed on all CPUs, but only the boot CPU determines what
109 * features the kernel will use.
111 * Much of the worst junk in this file is for the support of processors
112 * that didn't really implement the cpuid instruction properly.
114 * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
115 * the pass numbers. Accordingly, changes to the pass code may require changes
116 * to the accessor code.
119 uint_t x86_vendor = X86_VENDOR_IntelClone;
120 uint_t x86_type = X86_TYPE_OTHER;
121 uint_t x86_clflush_size = 0;
123 uint_t pentiumpro_bug4046376;
125 uchar_t x86_featureset[BT_SIZEOFMAP(NUM_X86_FEATURES)];
127 static char *x86_feature_names[NUM_X86_FEATURES] = {
128 "lgpg",
129 "tsc",
130 "msr",
131 "mtrr",
132 "pge",
133 "de",
134 "cmov",
135 "mmx",
136 "mca",
137 "pae",
138 "cv8",
139 "pat",
140 "sep",
141 "sse",
142 "sse2",
143 "htt",
144 "asysc",
145 "nx",
146 "sse3",
147 "cx16",
148 "cmp",
149 "tscp",
150 "mwait",
151 "sse4a",
152 "cpuid",
153 "ssse3",
154 "sse4_1",
155 "sse4_2",
156 "1gpg",
157 "clfsh",
158 "64",
159 "aes",
160 "pclmulqdq",
161 "xsave",
162 "avx",
163 "vmx",
164 "svm",
165 "topoext",
166 "f16c",
167 "rdrand",
168 "x2apic",
169 "avx2",
170 "bmi1",
171 "bmi2",
172 "fma",
173 "smep"
176 boolean_t
177 is_x86_feature(void *featureset, uint_t feature)
179 ASSERT(feature < NUM_X86_FEATURES);
180 return (BT_TEST((ulong_t *)featureset, feature));
183 void
184 add_x86_feature(void *featureset, uint_t feature)
186 ASSERT(feature < NUM_X86_FEATURES);
187 BT_SET((ulong_t *)featureset, feature);
190 void
191 remove_x86_feature(void *featureset, uint_t feature)
193 ASSERT(feature < NUM_X86_FEATURES);
194 BT_CLEAR((ulong_t *)featureset, feature);
197 boolean_t
198 compare_x86_featureset(void *setA, void *setB)
201 * We assume that the unused bits of the bitmap are always zero.
203 if (memcmp(setA, setB, BT_SIZEOFMAP(NUM_X86_FEATURES)) == 0) {
204 return (B_TRUE);
205 } else {
206 return (B_FALSE);
210 void
211 print_x86_featureset(void *featureset)
213 uint_t i;
215 for (i = 0; i < NUM_X86_FEATURES; i++) {
216 if (is_x86_feature(featureset, i)) {
217 cmn_err(CE_CONT, "?x86_feature: %s\n",
218 x86_feature_names[i]);
223 static size_t xsave_state_size = 0;
224 uint64_t xsave_bv_all = (XFEATURE_LEGACY_FP | XFEATURE_SSE);
225 boolean_t xsave_force_disable = B_FALSE;
228 * This is set to platform type we are running on.
230 static int platform_type = -1;
232 #if !defined(__xpv)
234 * Variable to patch if hypervisor platform detection needs to be
235 * disabled (e.g. platform_type will always be HW_NATIVE if this is 0).
237 int enable_platform_detection = 1;
238 #endif
241 * monitor/mwait info.
243 * size_actual and buf_actual are the real address and size allocated to get
244 * proper mwait_buf alignement. buf_actual and size_actual should be passed
245 * to kmem_free(). Currently kmem_alloc() and mwait happen to both use
246 * processor cache-line alignment, but this is not guarantied in the furture.
248 struct mwait_info {
249 size_t mon_min; /* min size to avoid missed wakeups */
250 size_t mon_max; /* size to avoid false wakeups */
251 size_t size_actual; /* size actually allocated */
252 void *buf_actual; /* memory actually allocated */
253 uint32_t support; /* processor support of monitor/mwait */
257 * xsave/xrestor info.
259 * This structure contains HW feature bits and size of the xsave save area.
260 * Note: the kernel will use the maximum size required for all hardware
261 * features. It is not optimize for potential memory savings if features at
262 * the end of the save area are not enabled.
264 struct xsave_info {
265 uint32_t xsav_hw_features_low; /* Supported HW features */
266 uint32_t xsav_hw_features_high; /* Supported HW features */
267 size_t xsav_max_size; /* max size save area for HW features */
268 size_t ymm_size; /* AVX: size of ymm save area */
269 size_t ymm_offset; /* AVX: offset for ymm save area */
274 * These constants determine how many of the elements of the
275 * cpuid we cache in the cpuid_info data structure; the
276 * remaining elements are accessible via the cpuid instruction.
279 #define NMAX_CPI_STD 8 /* eax = 0 .. 7 */
280 #define NMAX_CPI_EXTD 0x1f /* eax = 0x80000000 .. 0x8000001e */
283 * Some terminology needs to be explained:
284 * - Socket: Something that can be plugged into a motherboard.
285 * - Package: Same as socket
286 * - Chip: Same as socket. Note that AMD's documentation uses term "chip"
287 * differently: there, chip is the same as processor node (below)
288 * - Processor node: Some AMD processors have more than one
289 * "subprocessor" embedded in a package. These subprocessors (nodes)
290 * are fully-functional processors themselves with cores, caches,
291 * memory controllers, PCI configuration spaces. They are connected
292 * inside the package with Hypertransport links. On single-node
293 * processors, processor node is equivalent to chip/socket/package.
294 * - Compute Unit: Some AMD processors pair cores in "compute units" that
295 * share the FPU and the I$ and L2 caches.
298 struct cpuid_info {
299 uint_t cpi_pass; /* last pass completed */
301 * standard function information
303 uint_t cpi_maxeax; /* fn 0: %eax */
304 char cpi_vendorstr[13]; /* fn 0: %ebx:%ecx:%edx */
305 uint_t cpi_vendor; /* enum of cpi_vendorstr */
307 uint_t cpi_family; /* fn 1: extended family */
308 uint_t cpi_model; /* fn 1: extended model */
309 uint_t cpi_step; /* fn 1: stepping */
310 chipid_t cpi_chipid; /* fn 1: %ebx: Intel: chip # */
311 /* AMD: package/socket # */
312 uint_t cpi_brandid; /* fn 1: %ebx: brand ID */
313 int cpi_clogid; /* fn 1: %ebx: thread # */
314 uint_t cpi_ncpu_per_chip; /* fn 1: %ebx: logical cpu count */
315 uint8_t cpi_cacheinfo[16]; /* fn 2: intel-style cache desc */
316 uint_t cpi_ncache; /* fn 2: number of elements */
317 uint_t cpi_ncpu_shr_last_cache; /* fn 4: %eax: ncpus sharing cache */
318 id_t cpi_last_lvl_cacheid; /* fn 4: %eax: derived cache id */
319 uint_t cpi_std_4_size; /* fn 4: number of fn 4 elements */
320 struct cpuid_regs **cpi_std_4; /* fn 4: %ecx == 0 .. fn4_size */
321 struct cpuid_regs cpi_std[NMAX_CPI_STD]; /* 0 .. 7 */
323 * extended function information
325 uint_t cpi_xmaxeax; /* fn 0x80000000: %eax */
326 char cpi_brandstr[49]; /* fn 0x8000000[234] */
327 uint8_t cpi_pabits; /* fn 0x80000006: %eax */
328 uint8_t cpi_vabits; /* fn 0x80000006: %eax */
329 struct cpuid_regs cpi_extd[NMAX_CPI_EXTD]; /* 0x800000XX */
331 id_t cpi_coreid; /* same coreid => strands share core */
332 int cpi_pkgcoreid; /* core number within single package */
333 uint_t cpi_ncore_per_chip; /* AMD: fn 0x80000008: %ecx[7-0] */
334 /* Intel: fn 4: %eax[31-26] */
336 * supported feature information
338 uint32_t cpi_support[6];
339 #define STD_EDX_FEATURES 0
340 #define AMD_EDX_FEATURES 1
341 #define TM_EDX_FEATURES 2
342 #define STD_ECX_FEATURES 3
343 #define AMD_ECX_FEATURES 4
344 #define STD_EBX_FEATURES 5
346 * Synthesized information, where known.
348 uint32_t cpi_chiprev; /* See X86_CHIPREV_* in x86_archext.h */
349 const char *cpi_chiprevstr; /* May be NULL if chiprev unknown */
350 uint32_t cpi_socket; /* Chip package/socket type */
352 struct mwait_info cpi_mwait; /* fn 5: monitor/mwait info */
353 uint32_t cpi_apicid;
354 uint_t cpi_procnodeid; /* AMD: nodeID on HT, Intel: chipid */
355 uint_t cpi_procnodes_per_pkg; /* AMD: # of nodes in the package */
356 /* Intel: 1 */
357 uint_t cpi_compunitid; /* AMD: ComputeUnit ID, Intel: coreid */
358 uint_t cpi_cores_per_compunit; /* AMD: # of cores in the ComputeUnit */
360 struct xsave_info cpi_xsave; /* fn D: xsave/xrestor info */
364 static struct cpuid_info cpuid_info0;
367 * These bit fields are defined by the Intel Application Note AP-485
368 * "Intel Processor Identification and the CPUID Instruction"
370 #define CPI_FAMILY_XTD(cpi) BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
371 #define CPI_MODEL_XTD(cpi) BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
372 #define CPI_TYPE(cpi) BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
373 #define CPI_FAMILY(cpi) BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
374 #define CPI_STEP(cpi) BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
375 #define CPI_MODEL(cpi) BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
377 #define CPI_FEATURES_EDX(cpi) ((cpi)->cpi_std[1].cp_edx)
378 #define CPI_FEATURES_ECX(cpi) ((cpi)->cpi_std[1].cp_ecx)
379 #define CPI_FEATURES_XTD_EDX(cpi) ((cpi)->cpi_extd[1].cp_edx)
380 #define CPI_FEATURES_XTD_ECX(cpi) ((cpi)->cpi_extd[1].cp_ecx)
381 #define CPI_FEATURES_7_0_EBX(cpi) ((cpi)->cpi_std[7].cp_ebx)
383 #define CPI_BRANDID(cpi) BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
384 #define CPI_CHUNKS(cpi) BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
385 #define CPI_CPU_COUNT(cpi) BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
386 #define CPI_APIC_ID(cpi) BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
388 #define CPI_MAXEAX_MAX 0x100 /* sanity control */
389 #define CPI_XMAXEAX_MAX 0x80000100
390 #define CPI_FN4_ECX_MAX 0x20 /* sanity: max fn 4 levels */
391 #define CPI_FNB_ECX_MAX 0x20 /* sanity: max fn B levels */
394 * Function 4 (Deterministic Cache Parameters) macros
395 * Defined by Intel Application Note AP-485
397 #define CPI_NUM_CORES(regs) BITX((regs)->cp_eax, 31, 26)
398 #define CPI_NTHR_SHR_CACHE(regs) BITX((regs)->cp_eax, 25, 14)
399 #define CPI_FULL_ASSOC_CACHE(regs) BITX((regs)->cp_eax, 9, 9)
400 #define CPI_SELF_INIT_CACHE(regs) BITX((regs)->cp_eax, 8, 8)
401 #define CPI_CACHE_LVL(regs) BITX((regs)->cp_eax, 7, 5)
402 #define CPI_CACHE_TYPE(regs) BITX((regs)->cp_eax, 4, 0)
403 #define CPI_CPU_LEVEL_TYPE(regs) BITX((regs)->cp_ecx, 15, 8)
405 #define CPI_CACHE_WAYS(regs) BITX((regs)->cp_ebx, 31, 22)
406 #define CPI_CACHE_PARTS(regs) BITX((regs)->cp_ebx, 21, 12)
407 #define CPI_CACHE_COH_LN_SZ(regs) BITX((regs)->cp_ebx, 11, 0)
409 #define CPI_CACHE_SETS(regs) BITX((regs)->cp_ecx, 31, 0)
411 #define CPI_PREFCH_STRIDE(regs) BITX((regs)->cp_edx, 9, 0)
415 * A couple of shorthand macros to identify "later" P6-family chips
416 * like the Pentium M and Core. First, the "older" P6-based stuff
417 * (loosely defined as "pre-Pentium-4"):
418 * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
421 #define IS_LEGACY_P6(cpi) ( \
422 cpi->cpi_family == 6 && \
423 (cpi->cpi_model == 1 || \
424 cpi->cpi_model == 3 || \
425 cpi->cpi_model == 5 || \
426 cpi->cpi_model == 6 || \
427 cpi->cpi_model == 7 || \
428 cpi->cpi_model == 8 || \
429 cpi->cpi_model == 0xA || \
430 cpi->cpi_model == 0xB) \
433 /* A "new F6" is everything with family 6 that's not the above */
434 #define IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
436 /* Extended family/model support */
437 #define IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
438 cpi->cpi_family >= 0xf)
441 * Info for monitor/mwait idle loop.
443 * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
444 * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
445 * 2006.
446 * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
447 * Documentation Updates" #33633, Rev 2.05, December 2006.
449 #define MWAIT_SUPPORT (0x00000001) /* mwait supported */
450 #define MWAIT_EXTENSIONS (0x00000002) /* extenstion supported */
451 #define MWAIT_ECX_INT_ENABLE (0x00000004) /* ecx 1 extension supported */
452 #define MWAIT_SUPPORTED(cpi) ((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
453 #define MWAIT_INT_ENABLE(cpi) ((cpi)->cpi_std[5].cp_ecx & 0x2)
454 #define MWAIT_EXTENSION(cpi) ((cpi)->cpi_std[5].cp_ecx & 0x1)
455 #define MWAIT_SIZE_MIN(cpi) BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
456 #define MWAIT_SIZE_MAX(cpi) BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
458 * Number of sub-cstates for a given c-state.
460 #define MWAIT_NUM_SUBC_STATES(cpi, c_state) \
461 BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
464 * XSAVE leaf 0xD enumeration
466 #define CPUID_LEAFD_2_YMM_OFFSET 576
467 #define CPUID_LEAFD_2_YMM_SIZE 256
470 * Functions we consune from cpuid_subr.c; don't publish these in a header
471 * file to try and keep people using the expected cpuid_* interfaces.
473 extern uint32_t _cpuid_skt(uint_t, uint_t, uint_t, uint_t);
474 extern const char *_cpuid_sktstr(uint_t, uint_t, uint_t, uint_t);
475 extern uint32_t _cpuid_chiprev(uint_t, uint_t, uint_t, uint_t);
476 extern const char *_cpuid_chiprevstr(uint_t, uint_t, uint_t, uint_t);
477 extern uint_t _cpuid_vendorstr_to_vendorcode(char *);
480 * Apply up various platform-dependent restrictions where the
481 * underlying platform restrictions mean the CPU can be marked
482 * as less capable than its cpuid instruction would imply.
484 #if defined(__xpv)
485 static void
486 platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
488 switch (eax) {
489 case 1: {
490 uint32_t mcamask = DOMAIN_IS_INITDOMAIN(xen_info) ?
491 0 : CPUID_INTC_EDX_MCA;
492 cp->cp_edx &=
493 ~(mcamask |
494 CPUID_INTC_EDX_PSE |
495 CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
496 CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
497 CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
498 CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
499 CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
500 break;
503 case 0x80000001:
504 cp->cp_edx &=
505 ~(CPUID_AMD_EDX_PSE |
506 CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
507 CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
508 CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
509 CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
510 CPUID_AMD_EDX_TSCP);
511 cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
512 break;
513 default:
514 break;
517 switch (vendor) {
518 case X86_VENDOR_Intel:
519 switch (eax) {
520 case 4:
522 * Zero out the (ncores-per-chip - 1) field
524 cp->cp_eax &= 0x03fffffff;
525 break;
526 default:
527 break;
529 break;
530 case X86_VENDOR_AMD:
531 switch (eax) {
533 case 0x80000001:
534 cp->cp_ecx &= ~CPUID_AMD_ECX_CR8D;
535 break;
537 case 0x80000008:
539 * Zero out the (ncores-per-chip - 1) field
541 cp->cp_ecx &= 0xffffff00;
542 break;
543 default:
544 break;
546 break;
547 default:
548 break;
551 #else
552 #define platform_cpuid_mangle(vendor, eax, cp) /* nothing */
553 #endif
556 * Some undocumented ways of patching the results of the cpuid
557 * instruction to permit running Solaris 10 on future cpus that
558 * we don't currently support. Could be set to non-zero values
559 * via settings in eeprom.
562 uint32_t cpuid_feature_ecx_include;
563 uint32_t cpuid_feature_ecx_exclude;
564 uint32_t cpuid_feature_edx_include;
565 uint32_t cpuid_feature_edx_exclude;
568 * Allocate space for mcpu_cpi in the machcpu structure for all non-boot CPUs.
570 void
571 cpuid_alloc_space(cpu_t *cpu)
574 * By convention, cpu0 is the boot cpu, which is set up
575 * before memory allocation is available. All other cpus get
576 * their cpuid_info struct allocated here.
578 ASSERT(cpu->cpu_id != 0);
579 ASSERT(cpu->cpu_m.mcpu_cpi == NULL);
580 cpu->cpu_m.mcpu_cpi =
581 kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
584 void
585 cpuid_free_space(cpu_t *cpu)
587 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
588 int i;
590 ASSERT(cpi != NULL);
591 ASSERT(cpi != &cpuid_info0);
594 * Free up any function 4 related dynamic storage
596 for (i = 1; i < cpi->cpi_std_4_size; i++)
597 kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
598 if (cpi->cpi_std_4_size > 0)
599 kmem_free(cpi->cpi_std_4,
600 cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
602 kmem_free(cpi, sizeof (*cpi));
603 cpu->cpu_m.mcpu_cpi = NULL;
606 #if !defined(__xpv)
608 * Determine the type of the underlying platform. This is used to customize
609 * initialization of various subsystems (e.g. TSC). determine_platform() must
610 * only ever be called once to prevent two processors from seeing different
611 * values of platform_type. Must be called before cpuid_pass1(), the earliest
612 * consumer to execute (uses _cpuid_chiprev --> synth_amd_info --> get_hwenv).
614 void
615 determine_platform(void)
617 struct cpuid_regs cp;
618 uint32_t base;
619 uint32_t regs[4];
620 char *hvstr = (char *)regs;
622 ASSERT(platform_type == -1);
624 platform_type = HW_NATIVE;
626 if (!enable_platform_detection)
627 return;
630 * If Hypervisor CPUID bit is set, try to determine hypervisor
631 * vendor signature, and set platform type accordingly.
633 * References:
634 * http://lkml.org/lkml/2008/10/1/246
635 * http://kb.vmware.com/kb/1009458
637 cp.cp_eax = 0x1;
638 (void) __cpuid_insn(&cp);
639 if ((cp.cp_ecx & CPUID_INTC_ECX_HV) != 0) {
640 cp.cp_eax = 0x40000000;
641 (void) __cpuid_insn(&cp);
642 regs[0] = cp.cp_ebx;
643 regs[1] = cp.cp_ecx;
644 regs[2] = cp.cp_edx;
645 regs[3] = 0;
646 if (strcmp(hvstr, HVSIG_XEN_HVM) == 0) {
647 platform_type = HW_XEN_HVM;
648 return;
650 if (strcmp(hvstr, HVSIG_VMWARE) == 0) {
651 platform_type = HW_VMWARE;
652 return;
654 if (strcmp(hvstr, HVSIG_KVM) == 0) {
655 platform_type = HW_KVM;
656 return;
658 if (strcmp(hvstr, HVSIG_MICROSOFT) == 0)
659 platform_type = HW_MICROSOFT;
660 } else {
662 * Check older VMware hardware versions. VMware hypervisor is
663 * detected by performing an IN operation to VMware hypervisor
664 * port and checking that value returned in %ebx is VMware
665 * hypervisor magic value.
667 * References: http://kb.vmware.com/kb/1009458
669 vmware_port(VMWARE_HVCMD_GETVERSION, regs);
670 if (regs[1] == VMWARE_HVMAGIC) {
671 platform_type = HW_VMWARE;
672 return;
677 * Check Xen hypervisor. In a fully virtualized domain,
678 * Xen's pseudo-cpuid function returns a string representing the
679 * Xen signature in %ebx, %ecx, and %edx. %eax contains the maximum
680 * supported cpuid function. We need at least a (base + 2) leaf value
681 * to do what we want to do. Try different base values, since the
682 * hypervisor might use a different one depending on whether Hyper-V
683 * emulation is switched on by default or not.
685 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
686 cp.cp_eax = base;
687 (void) __cpuid_insn(&cp);
688 regs[0] = cp.cp_ebx;
689 regs[1] = cp.cp_ecx;
690 regs[2] = cp.cp_edx;
691 regs[3] = 0;
692 if (strcmp(hvstr, HVSIG_XEN_HVM) == 0 &&
693 cp.cp_eax >= (base + 2)) {
694 platform_type &= ~HW_NATIVE;
695 platform_type |= HW_XEN_HVM;
696 return;
702 get_hwenv(void)
704 ASSERT(platform_type != -1);
705 return (platform_type);
709 is_controldom(void)
711 return (0);
714 #else
717 get_hwenv(void)
719 return (HW_XEN_PV);
723 is_controldom(void)
725 return (DOMAIN_IS_INITDOMAIN(xen_info));
728 #endif /* __xpv */
730 static void
731 cpuid_intel_getids(cpu_t *cpu, void *feature)
733 uint_t i;
734 uint_t chipid_shift = 0;
735 uint_t coreid_shift = 0;
736 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
738 for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
739 chipid_shift++;
741 cpi->cpi_chipid = cpi->cpi_apicid >> chipid_shift;
742 cpi->cpi_clogid = cpi->cpi_apicid & ((1 << chipid_shift) - 1);
744 if (is_x86_feature(feature, X86FSET_CMP)) {
746 * Multi-core (and possibly multi-threaded)
747 * processors.
749 uint_t ncpu_per_core;
750 if (cpi->cpi_ncore_per_chip == 1)
751 ncpu_per_core = cpi->cpi_ncpu_per_chip;
752 else if (cpi->cpi_ncore_per_chip > 1)
753 ncpu_per_core = cpi->cpi_ncpu_per_chip /
754 cpi->cpi_ncore_per_chip;
756 * 8bit APIC IDs on dual core Pentiums
757 * look like this:
759 * +-----------------------+------+------+
760 * | Physical Package ID | MC | HT |
761 * +-----------------------+------+------+
762 * <------- chipid -------->
763 * <------- coreid --------------->
764 * <--- clogid -->
765 * <------>
766 * pkgcoreid
768 * Where the number of bits necessary to
769 * represent MC and HT fields together equals
770 * to the minimum number of bits necessary to
771 * store the value of cpi->cpi_ncpu_per_chip.
772 * Of those bits, the MC part uses the number
773 * of bits necessary to store the value of
774 * cpi->cpi_ncore_per_chip.
776 for (i = 1; i < ncpu_per_core; i <<= 1)
777 coreid_shift++;
778 cpi->cpi_coreid = cpi->cpi_apicid >> coreid_shift;
779 cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
780 } else if (is_x86_feature(feature, X86FSET_HTT)) {
782 * Single-core multi-threaded processors.
784 cpi->cpi_coreid = cpi->cpi_chipid;
785 cpi->cpi_pkgcoreid = 0;
787 cpi->cpi_procnodeid = cpi->cpi_chipid;
788 cpi->cpi_compunitid = cpi->cpi_coreid;
791 static void
792 cpuid_amd_getids(cpu_t *cpu)
794 int i, first_half, coreidsz;
795 uint32_t nb_caps_reg;
796 uint_t node2_1;
797 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
798 struct cpuid_regs *cp;
801 * AMD CMP chips currently have a single thread per core.
803 * Since no two cpus share a core we must assign a distinct coreid
804 * per cpu, and we do this by using the cpu_id. This scheme does not,
805 * however, guarantee that sibling cores of a chip will have sequential
806 * coreids starting at a multiple of the number of cores per chip -
807 * that is usually the case, but if the ACPI MADT table is presented
808 * in a different order then we need to perform a few more gymnastics
809 * for the pkgcoreid.
811 * All processors in the system have the same number of enabled
812 * cores. Cores within a processor are always numbered sequentially
813 * from 0 regardless of how many or which are disabled, and there
814 * is no way for operating system to discover the real core id when some
815 * are disabled.
817 * In family 0x15, the cores come in pairs called compute units. They
818 * share I$ and L2 caches and the FPU. Enumeration of this feature is
819 * simplified by the new topology extensions CPUID leaf, indicated by
820 * the X86 feature X86FSET_TOPOEXT.
823 cpi->cpi_coreid = cpu->cpu_id;
824 cpi->cpi_compunitid = cpu->cpu_id;
826 if (cpi->cpi_xmaxeax >= 0x80000008) {
828 coreidsz = BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
831 * In AMD parlance chip is really a node while Solaris
832 * sees chip as equivalent to socket/package.
834 cpi->cpi_ncore_per_chip =
835 BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
836 if (coreidsz == 0) {
837 /* Use legacy method */
838 for (i = 1; i < cpi->cpi_ncore_per_chip; i <<= 1)
839 coreidsz++;
840 if (coreidsz == 0)
841 coreidsz = 1;
843 } else {
844 /* Assume single-core part */
845 cpi->cpi_ncore_per_chip = 1;
846 coreidsz = 1;
849 cpi->cpi_clogid = cpi->cpi_pkgcoreid =
850 cpi->cpi_apicid & ((1<<coreidsz) - 1);
851 cpi->cpi_ncpu_per_chip = cpi->cpi_ncore_per_chip;
853 /* Get node ID, compute unit ID */
854 if (is_x86_feature(x86_featureset, X86FSET_TOPOEXT) &&
855 cpi->cpi_xmaxeax >= 0x8000001e) {
856 cp = &cpi->cpi_extd[0x1e];
857 cp->cp_eax = 0x8000001e;
858 (void) __cpuid_insn(cp);
860 cpi->cpi_procnodes_per_pkg = BITX(cp->cp_ecx, 10, 8) + 1;
861 cpi->cpi_procnodeid = BITX(cp->cp_ecx, 7, 0);
862 cpi->cpi_cores_per_compunit = BITX(cp->cp_ebx, 15, 8) + 1;
863 cpi->cpi_compunitid = BITX(cp->cp_ebx, 7, 0)
864 + (cpi->cpi_ncore_per_chip / cpi->cpi_cores_per_compunit)
865 * (cpi->cpi_procnodeid / cpi->cpi_procnodes_per_pkg);
866 } else if (cpi->cpi_family == 0xf || cpi->cpi_family >= 0x11) {
867 cpi->cpi_procnodeid = (cpi->cpi_apicid >> coreidsz) & 7;
868 } else if (cpi->cpi_family == 0x10) {
870 * See if we are a multi-node processor.
871 * All processors in the system have the same number of nodes
873 nb_caps_reg = pci_getl_func(0, 24, 3, 0xe8);
874 if ((cpi->cpi_model < 8) || BITX(nb_caps_reg, 29, 29) == 0) {
875 /* Single-node */
876 cpi->cpi_procnodeid = BITX(cpi->cpi_apicid, 5,
877 coreidsz);
878 } else {
881 * Multi-node revision D (2 nodes per package
882 * are supported)
884 cpi->cpi_procnodes_per_pkg = 2;
886 first_half = (cpi->cpi_pkgcoreid <=
887 (cpi->cpi_ncore_per_chip/2 - 1));
889 if (cpi->cpi_apicid == cpi->cpi_pkgcoreid) {
890 /* We are BSP */
891 cpi->cpi_procnodeid = (first_half ? 0 : 1);
892 } else {
894 /* We are AP */
895 /* NodeId[2:1] bits to use for reading F3xe8 */
896 node2_1 = BITX(cpi->cpi_apicid, 5, 4) << 1;
898 nb_caps_reg =
899 pci_getl_func(0, 24 + node2_1, 3, 0xe8);
902 * Check IntNodeNum bit (31:30, but bit 31 is
903 * always 0 on dual-node processors)
905 if (BITX(nb_caps_reg, 30, 30) == 0)
906 cpi->cpi_procnodeid = node2_1 +
907 !first_half;
908 else
909 cpi->cpi_procnodeid = node2_1 +
910 first_half;
913 } else {
914 cpi->cpi_procnodeid = 0;
917 cpi->cpi_chipid =
918 cpi->cpi_procnodeid / cpi->cpi_procnodes_per_pkg;
922 * Setup XFeature_Enabled_Mask register. Required by xsave feature.
924 void
925 setup_xfem(void)
927 uint64_t flags = XFEATURE_LEGACY_FP;
929 ASSERT(is_x86_feature(x86_featureset, X86FSET_XSAVE));
931 if (is_x86_feature(x86_featureset, X86FSET_SSE))
932 flags |= XFEATURE_SSE;
934 if (is_x86_feature(x86_featureset, X86FSET_AVX))
935 flags |= XFEATURE_AVX;
937 set_xcr(XFEATURE_ENABLED_MASK, flags);
939 xsave_bv_all = flags;
942 void
943 cpuid_pass1(cpu_t *cpu, uchar_t *featureset)
945 uint32_t mask_ecx, mask_edx;
946 struct cpuid_info *cpi;
947 struct cpuid_regs *cp;
948 int xcpuid;
949 #if !defined(__xpv)
950 extern int idle_cpu_prefer_mwait;
951 #endif
954 * Space statically allocated for BSP, ensure pointer is set
956 if (cpu->cpu_id == 0) {
957 if (cpu->cpu_m.mcpu_cpi == NULL)
958 cpu->cpu_m.mcpu_cpi = &cpuid_info0;
961 add_x86_feature(featureset, X86FSET_CPUID);
963 cpi = cpu->cpu_m.mcpu_cpi;
964 ASSERT(cpi != NULL);
965 cp = &cpi->cpi_std[0];
966 cp->cp_eax = 0;
967 cpi->cpi_maxeax = __cpuid_insn(cp);
969 uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
970 *iptr++ = cp->cp_ebx;
971 *iptr++ = cp->cp_edx;
972 *iptr++ = cp->cp_ecx;
973 *(char *)&cpi->cpi_vendorstr[12] = '\0';
976 cpi->cpi_vendor = _cpuid_vendorstr_to_vendorcode(cpi->cpi_vendorstr);
977 x86_vendor = cpi->cpi_vendor; /* for compatibility */
980 * Limit the range in case of weird hardware
982 if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
983 cpi->cpi_maxeax = CPI_MAXEAX_MAX;
984 if (cpi->cpi_maxeax < 1)
985 goto pass1_done;
987 cp = &cpi->cpi_std[1];
988 cp->cp_eax = 1;
989 (void) __cpuid_insn(cp);
992 * Extract identifying constants for easy access.
994 cpi->cpi_model = CPI_MODEL(cpi);
995 cpi->cpi_family = CPI_FAMILY(cpi);
997 if (cpi->cpi_family == 0xf)
998 cpi->cpi_family += CPI_FAMILY_XTD(cpi);
1001 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
1002 * Intel, and presumably everyone else, uses model == 0xf, as
1003 * one would expect (max value means possible overflow). Sigh.
1006 switch (cpi->cpi_vendor) {
1007 case X86_VENDOR_Intel:
1008 if (IS_EXTENDED_MODEL_INTEL(cpi))
1009 cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
1010 break;
1011 case X86_VENDOR_AMD:
1012 if (CPI_FAMILY(cpi) == 0xf)
1013 cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
1014 break;
1015 default:
1016 if (cpi->cpi_model == 0xf)
1017 cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
1018 break;
1021 cpi->cpi_step = CPI_STEP(cpi);
1022 cpi->cpi_brandid = CPI_BRANDID(cpi);
1025 * *default* assumptions:
1026 * - believe %edx feature word
1027 * - ignore %ecx feature word
1028 * - 32-bit virtual and physical addressing
1030 mask_edx = 0xffffffff;
1031 mask_ecx = 0;
1033 cpi->cpi_pabits = cpi->cpi_vabits = 32;
1035 switch (cpi->cpi_vendor) {
1036 case X86_VENDOR_Intel:
1037 if (cpi->cpi_family == 5)
1038 x86_type = X86_TYPE_P5;
1039 else if (IS_LEGACY_P6(cpi)) {
1040 x86_type = X86_TYPE_P6;
1041 pentiumpro_bug4046376 = 1;
1043 * Clear the SEP bit when it was set erroneously
1045 if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
1046 cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
1047 } else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
1048 x86_type = X86_TYPE_P4;
1050 * We don't currently depend on any of the %ecx
1051 * features until Prescott, so we'll only check
1052 * this from P4 onwards. We might want to revisit
1053 * that idea later.
1055 mask_ecx = 0xffffffff;
1056 } else if (cpi->cpi_family > 0xf)
1057 mask_ecx = 0xffffffff;
1059 * We don't support MONITOR/MWAIT if leaf 5 is not available
1060 * to obtain the monitor linesize.
1062 if (cpi->cpi_maxeax < 5)
1063 mask_ecx &= ~CPUID_INTC_ECX_MON;
1064 break;
1065 case X86_VENDOR_IntelClone:
1066 default:
1067 break;
1068 case X86_VENDOR_AMD:
1069 #if defined(OPTERON_ERRATUM_108)
1070 if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
1071 cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
1072 cpi->cpi_model = 0xc;
1073 } else
1074 #endif
1075 if (cpi->cpi_family == 5) {
1077 * AMD K5 and K6
1079 * These CPUs have an incomplete implementation
1080 * of MCA/MCE which we mask away.
1082 mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
1085 * Model 0 uses the wrong (APIC) bit
1086 * to indicate PGE. Fix it here.
1088 if (cpi->cpi_model == 0) {
1089 if (cp->cp_edx & 0x200) {
1090 cp->cp_edx &= ~0x200;
1091 cp->cp_edx |= CPUID_INTC_EDX_PGE;
1096 * Early models had problems w/ MMX; disable.
1098 if (cpi->cpi_model < 6)
1099 mask_edx &= ~CPUID_INTC_EDX_MMX;
1103 * For newer families, SSE3 and CX16, at least, are valid;
1104 * enable all
1106 if (cpi->cpi_family >= 0xf)
1107 mask_ecx = 0xffffffff;
1109 * We don't support MONITOR/MWAIT if leaf 5 is not available
1110 * to obtain the monitor linesize.
1112 if (cpi->cpi_maxeax < 5)
1113 mask_ecx &= ~CPUID_INTC_ECX_MON;
1115 #if !defined(__xpv)
1117 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
1118 * processors. AMD does not intend MWAIT to be used in the cpu
1119 * idle loop on current and future processors. 10h and future
1120 * AMD processors use more power in MWAIT than HLT.
1121 * Pre-family-10h Opterons do not have the MWAIT instruction.
1123 idle_cpu_prefer_mwait = 0;
1124 #endif
1126 break;
1127 case X86_VENDOR_TM:
1129 * workaround the NT workaround in CMS 4.1
1131 if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
1132 (cpi->cpi_step == 2 || cpi->cpi_step == 3))
1133 cp->cp_edx |= CPUID_INTC_EDX_CX8;
1134 break;
1135 case X86_VENDOR_Centaur:
1137 * workaround the NT workarounds again
1139 if (cpi->cpi_family == 6)
1140 cp->cp_edx |= CPUID_INTC_EDX_CX8;
1141 break;
1142 case X86_VENDOR_Cyrix:
1144 * We rely heavily on the probing in locore
1145 * to actually figure out what parts, if any,
1146 * of the Cyrix cpuid instruction to believe.
1148 switch (x86_type) {
1149 case X86_TYPE_CYRIX_486:
1150 mask_edx = 0;
1151 break;
1152 case X86_TYPE_CYRIX_6x86:
1153 mask_edx = 0;
1154 break;
1155 case X86_TYPE_CYRIX_6x86L:
1156 mask_edx =
1157 CPUID_INTC_EDX_DE |
1158 CPUID_INTC_EDX_CX8;
1159 break;
1160 case X86_TYPE_CYRIX_6x86MX:
1161 mask_edx =
1162 CPUID_INTC_EDX_DE |
1163 CPUID_INTC_EDX_MSR |
1164 CPUID_INTC_EDX_CX8 |
1165 CPUID_INTC_EDX_PGE |
1166 CPUID_INTC_EDX_CMOV |
1167 CPUID_INTC_EDX_MMX;
1168 break;
1169 case X86_TYPE_CYRIX_GXm:
1170 mask_edx =
1171 CPUID_INTC_EDX_MSR |
1172 CPUID_INTC_EDX_CX8 |
1173 CPUID_INTC_EDX_CMOV |
1174 CPUID_INTC_EDX_MMX;
1175 break;
1176 case X86_TYPE_CYRIX_MediaGX:
1177 break;
1178 case X86_TYPE_CYRIX_MII:
1179 case X86_TYPE_VIA_CYRIX_III:
1180 mask_edx =
1181 CPUID_INTC_EDX_DE |
1182 CPUID_INTC_EDX_TSC |
1183 CPUID_INTC_EDX_MSR |
1184 CPUID_INTC_EDX_CX8 |
1185 CPUID_INTC_EDX_PGE |
1186 CPUID_INTC_EDX_CMOV |
1187 CPUID_INTC_EDX_MMX;
1188 break;
1189 default:
1190 break;
1192 break;
1195 #if defined(__xpv)
1197 * Do not support MONITOR/MWAIT under a hypervisor
1199 mask_ecx &= ~CPUID_INTC_ECX_MON;
1201 * Do not support XSAVE under a hypervisor for now
1203 xsave_force_disable = B_TRUE;
1205 #endif /* __xpv */
1207 if (xsave_force_disable) {
1208 mask_ecx &= ~CPUID_INTC_ECX_XSAVE;
1209 mask_ecx &= ~CPUID_INTC_ECX_AVX;
1210 mask_ecx &= ~CPUID_INTC_ECX_F16C;
1211 mask_ecx &= ~CPUID_INTC_ECX_FMA;
1215 * Now we've figured out the masks that determine
1216 * which bits we choose to believe, apply the masks
1217 * to the feature words, then map the kernel's view
1218 * of these feature words into its feature word.
1220 cp->cp_edx &= mask_edx;
1221 cp->cp_ecx &= mask_ecx;
1224 * apply any platform restrictions (we don't call this
1225 * immediately after __cpuid_insn here, because we need the
1226 * workarounds applied above first)
1228 platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
1231 * In addition to ecx and edx, Intel is storing a bunch of instruction
1232 * set extensions in leaf 7's ebx.
1234 if (cpi->cpi_vendor == X86_VENDOR_Intel && cpi->cpi_maxeax >= 7) {
1235 struct cpuid_regs *ecp;
1236 ecp = &cpi->cpi_std[7];
1237 ecp->cp_eax = 7;
1238 ecp->cp_ecx = 0;
1239 (void) __cpuid_insn(ecp);
1241 * If XSAVE has been disabled, just ignore all of the AVX
1242 * dependent flags here.
1244 if (xsave_force_disable) {
1245 ecp->cp_ebx &= ~CPUID_INTC_EBX_7_0_BMI1;
1246 ecp->cp_ebx &= ~CPUID_INTC_EBX_7_0_BMI2;
1247 ecp->cp_ebx &= ~CPUID_INTC_EBX_7_0_AVX2;
1250 if (ecp->cp_ebx & CPUID_INTC_EBX_7_0_SMEP)
1251 add_x86_feature(featureset, X86FSET_SMEP);
1255 * fold in overrides from the "eeprom" mechanism
1257 cp->cp_edx |= cpuid_feature_edx_include;
1258 cp->cp_edx &= ~cpuid_feature_edx_exclude;
1260 cp->cp_ecx |= cpuid_feature_ecx_include;
1261 cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
1263 if (cp->cp_edx & CPUID_INTC_EDX_PSE) {
1264 add_x86_feature(featureset, X86FSET_LARGEPAGE);
1266 if (cp->cp_edx & CPUID_INTC_EDX_TSC) {
1267 add_x86_feature(featureset, X86FSET_TSC);
1269 if (cp->cp_edx & CPUID_INTC_EDX_MSR) {
1270 add_x86_feature(featureset, X86FSET_MSR);
1272 if (cp->cp_edx & CPUID_INTC_EDX_MTRR) {
1273 add_x86_feature(featureset, X86FSET_MTRR);
1275 if (cp->cp_edx & CPUID_INTC_EDX_PGE) {
1276 add_x86_feature(featureset, X86FSET_PGE);
1278 if (cp->cp_edx & CPUID_INTC_EDX_CMOV) {
1279 add_x86_feature(featureset, X86FSET_CMOV);
1281 if (cp->cp_edx & CPUID_INTC_EDX_MMX) {
1282 add_x86_feature(featureset, X86FSET_MMX);
1284 if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
1285 (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0) {
1286 add_x86_feature(featureset, X86FSET_MCA);
1288 if (cp->cp_edx & CPUID_INTC_EDX_PAE) {
1289 add_x86_feature(featureset, X86FSET_PAE);
1291 if (cp->cp_edx & CPUID_INTC_EDX_CX8) {
1292 add_x86_feature(featureset, X86FSET_CX8);
1294 if (cp->cp_ecx & CPUID_INTC_ECX_CX16) {
1295 add_x86_feature(featureset, X86FSET_CX16);
1297 if (cp->cp_edx & CPUID_INTC_EDX_PAT) {
1298 add_x86_feature(featureset, X86FSET_PAT);
1300 if (cp->cp_edx & CPUID_INTC_EDX_SEP) {
1301 add_x86_feature(featureset, X86FSET_SEP);
1303 if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
1305 * In our implementation, fxsave/fxrstor
1306 * are prerequisites before we'll even
1307 * try and do SSE things.
1309 if (cp->cp_edx & CPUID_INTC_EDX_SSE) {
1310 add_x86_feature(featureset, X86FSET_SSE);
1312 if (cp->cp_edx & CPUID_INTC_EDX_SSE2) {
1313 add_x86_feature(featureset, X86FSET_SSE2);
1315 if (cp->cp_ecx & CPUID_INTC_ECX_SSE3) {
1316 add_x86_feature(featureset, X86FSET_SSE3);
1318 if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3) {
1319 add_x86_feature(featureset, X86FSET_SSSE3);
1321 if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1) {
1322 add_x86_feature(featureset, X86FSET_SSE4_1);
1324 if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2) {
1325 add_x86_feature(featureset, X86FSET_SSE4_2);
1327 if (cp->cp_ecx & CPUID_INTC_ECX_AES) {
1328 add_x86_feature(featureset, X86FSET_AES);
1330 if (cp->cp_ecx & CPUID_INTC_ECX_PCLMULQDQ) {
1331 add_x86_feature(featureset, X86FSET_PCLMULQDQ);
1334 if (cp->cp_ecx & CPUID_INTC_ECX_XSAVE) {
1335 add_x86_feature(featureset, X86FSET_XSAVE);
1337 /* We only test AVX when there is XSAVE */
1338 if (cp->cp_ecx & CPUID_INTC_ECX_AVX) {
1339 add_x86_feature(featureset,
1340 X86FSET_AVX);
1343 * Intel says we can't check these without also
1344 * checking AVX.
1346 if (cp->cp_ecx & CPUID_INTC_ECX_F16C)
1347 add_x86_feature(featureset,
1348 X86FSET_F16C);
1350 if (cp->cp_ecx & CPUID_INTC_ECX_FMA)
1351 add_x86_feature(featureset,
1352 X86FSET_FMA);
1354 if (cpi->cpi_std[7].cp_ebx &
1355 CPUID_INTC_EBX_7_0_BMI1)
1356 add_x86_feature(featureset,
1357 X86FSET_BMI1);
1359 if (cpi->cpi_std[7].cp_ebx &
1360 CPUID_INTC_EBX_7_0_BMI2)
1361 add_x86_feature(featureset,
1362 X86FSET_BMI2);
1364 if (cpi->cpi_std[7].cp_ebx &
1365 CPUID_INTC_EBX_7_0_AVX2)
1366 add_x86_feature(featureset,
1367 X86FSET_AVX2);
1371 if (cp->cp_ecx & CPUID_INTC_ECX_X2APIC) {
1372 add_x86_feature(featureset, X86FSET_X2APIC);
1374 if (cp->cp_edx & CPUID_INTC_EDX_DE) {
1375 add_x86_feature(featureset, X86FSET_DE);
1377 #if !defined(__xpv)
1378 if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
1381 * We require the CLFLUSH instruction for erratum workaround
1382 * to use MONITOR/MWAIT.
1384 if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
1385 cpi->cpi_mwait.support |= MWAIT_SUPPORT;
1386 add_x86_feature(featureset, X86FSET_MWAIT);
1387 } else {
1388 extern int idle_cpu_assert_cflush_monitor;
1391 * All processors we are aware of which have
1392 * MONITOR/MWAIT also have CLFLUSH.
1394 if (idle_cpu_assert_cflush_monitor) {
1395 ASSERT((cp->cp_ecx & CPUID_INTC_ECX_MON) &&
1396 (cp->cp_edx & CPUID_INTC_EDX_CLFSH));
1400 #endif /* __xpv */
1402 if (cp->cp_ecx & CPUID_INTC_ECX_VMX) {
1403 add_x86_feature(featureset, X86FSET_VMX);
1406 if (cp->cp_ecx & CPUID_INTC_ECX_RDRAND)
1407 add_x86_feature(featureset, X86FSET_RDRAND);
1410 * Only need it first time, rest of the cpus would follow suit.
1411 * we only capture this for the bootcpu.
1413 if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
1414 add_x86_feature(featureset, X86FSET_CLFSH);
1415 x86_clflush_size = (BITX(cp->cp_ebx, 15, 8) * 8);
1417 if (is_x86_feature(featureset, X86FSET_PAE))
1418 cpi->cpi_pabits = 36;
1421 * Hyperthreading configuration is slightly tricky on Intel
1422 * and pure clones, and even trickier on AMD.
1424 * (AMD chose to set the HTT bit on their CMP processors,
1425 * even though they're not actually hyperthreaded. Thus it
1426 * takes a bit more work to figure out what's really going
1427 * on ... see the handling of the CMP_LGCY bit below)
1429 if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
1430 cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
1431 if (cpi->cpi_ncpu_per_chip > 1)
1432 add_x86_feature(featureset, X86FSET_HTT);
1433 } else {
1434 cpi->cpi_ncpu_per_chip = 1;
1438 * Work on the "extended" feature information, doing
1439 * some basic initialization for cpuid_pass2()
1441 xcpuid = 0;
1442 switch (cpi->cpi_vendor) {
1443 case X86_VENDOR_Intel:
1444 if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
1445 xcpuid++;
1446 break;
1447 case X86_VENDOR_AMD:
1448 if (cpi->cpi_family > 5 ||
1449 (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
1450 xcpuid++;
1451 break;
1452 case X86_VENDOR_Cyrix:
1454 * Only these Cyrix CPUs are -known- to support
1455 * extended cpuid operations.
1457 if (x86_type == X86_TYPE_VIA_CYRIX_III ||
1458 x86_type == X86_TYPE_CYRIX_GXm)
1459 xcpuid++;
1460 break;
1461 case X86_VENDOR_Centaur:
1462 case X86_VENDOR_TM:
1463 default:
1464 xcpuid++;
1465 break;
1468 if (xcpuid) {
1469 cp = &cpi->cpi_extd[0];
1470 cp->cp_eax = 0x80000000;
1471 cpi->cpi_xmaxeax = __cpuid_insn(cp);
1474 if (cpi->cpi_xmaxeax & 0x80000000) {
1476 if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
1477 cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
1479 switch (cpi->cpi_vendor) {
1480 case X86_VENDOR_Intel:
1481 case X86_VENDOR_AMD:
1482 if (cpi->cpi_xmaxeax < 0x80000001)
1483 break;
1484 cp = &cpi->cpi_extd[1];
1485 cp->cp_eax = 0x80000001;
1486 (void) __cpuid_insn(cp);
1488 if (cpi->cpi_vendor == X86_VENDOR_AMD &&
1489 cpi->cpi_family == 5 &&
1490 cpi->cpi_model == 6 &&
1491 cpi->cpi_step == 6) {
1493 * K6 model 6 uses bit 10 to indicate SYSC
1494 * Later models use bit 11. Fix it here.
1496 if (cp->cp_edx & 0x400) {
1497 cp->cp_edx &= ~0x400;
1498 cp->cp_edx |= CPUID_AMD_EDX_SYSC;
1502 platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
1505 * Compute the additions to the kernel's feature word.
1507 if (cp->cp_edx & CPUID_AMD_EDX_NX) {
1508 add_x86_feature(featureset, X86FSET_NX);
1512 * Regardless whether or not we boot 64-bit,
1513 * we should have a way to identify whether
1514 * the CPU is capable of running 64-bit.
1516 if (cp->cp_edx & CPUID_AMD_EDX_LM) {
1517 add_x86_feature(featureset, X86FSET_64);
1520 #if defined(__amd64)
1521 /* 1 GB large page - enable only for 64 bit kernel */
1522 if (cp->cp_edx & CPUID_AMD_EDX_1GPG) {
1523 add_x86_feature(featureset, X86FSET_1GPG);
1525 #endif
1527 if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
1528 (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
1529 (cp->cp_ecx & CPUID_AMD_ECX_SSE4A)) {
1530 add_x86_feature(featureset, X86FSET_SSE4A);
1534 * If both the HTT and CMP_LGCY bits are set,
1535 * then we're not actually HyperThreaded. Read
1536 * "AMD CPUID Specification" for more details.
1538 if (cpi->cpi_vendor == X86_VENDOR_AMD &&
1539 is_x86_feature(featureset, X86FSET_HTT) &&
1540 (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
1541 remove_x86_feature(featureset, X86FSET_HTT);
1542 add_x86_feature(featureset, X86FSET_CMP);
1544 #if defined(__amd64)
1546 * It's really tricky to support syscall/sysret in
1547 * the i386 kernel; we rely on sysenter/sysexit
1548 * instead. In the amd64 kernel, things are -way-
1549 * better.
1551 if (cp->cp_edx & CPUID_AMD_EDX_SYSC) {
1552 add_x86_feature(featureset, X86FSET_ASYSC);
1556 * While we're thinking about system calls, note
1557 * that AMD processors don't support sysenter
1558 * in long mode at all, so don't try to program them.
1560 if (x86_vendor == X86_VENDOR_AMD) {
1561 remove_x86_feature(featureset, X86FSET_SEP);
1563 #endif
1564 if (cp->cp_edx & CPUID_AMD_EDX_TSCP) {
1565 add_x86_feature(featureset, X86FSET_TSCP);
1568 if (cp->cp_ecx & CPUID_AMD_ECX_SVM) {
1569 add_x86_feature(featureset, X86FSET_SVM);
1572 if (cp->cp_ecx & CPUID_AMD_ECX_TOPOEXT) {
1573 add_x86_feature(featureset, X86FSET_TOPOEXT);
1575 break;
1576 default:
1577 break;
1581 * Get CPUID data about processor cores and hyperthreads.
1583 switch (cpi->cpi_vendor) {
1584 case X86_VENDOR_Intel:
1585 if (cpi->cpi_maxeax >= 4) {
1586 cp = &cpi->cpi_std[4];
1587 cp->cp_eax = 4;
1588 cp->cp_ecx = 0;
1589 (void) __cpuid_insn(cp);
1590 platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
1592 /*FALLTHROUGH*/
1593 case X86_VENDOR_AMD:
1594 if (cpi->cpi_xmaxeax < 0x80000008)
1595 break;
1596 cp = &cpi->cpi_extd[8];
1597 cp->cp_eax = 0x80000008;
1598 (void) __cpuid_insn(cp);
1599 platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
1602 * Virtual and physical address limits from
1603 * cpuid override previously guessed values.
1605 cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
1606 cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
1607 break;
1608 default:
1609 break;
1613 * Derive the number of cores per chip
1615 switch (cpi->cpi_vendor) {
1616 case X86_VENDOR_Intel:
1617 if (cpi->cpi_maxeax < 4) {
1618 cpi->cpi_ncore_per_chip = 1;
1619 break;
1620 } else {
1621 cpi->cpi_ncore_per_chip =
1622 BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
1624 break;
1625 case X86_VENDOR_AMD:
1626 if (cpi->cpi_xmaxeax < 0x80000008) {
1627 cpi->cpi_ncore_per_chip = 1;
1628 break;
1629 } else {
1631 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
1632 * 1 less than the number of physical cores on
1633 * the chip. In family 0x10 this value can
1634 * be affected by "downcoring" - it reflects
1635 * 1 less than the number of cores actually
1636 * enabled on this node.
1638 cpi->cpi_ncore_per_chip =
1639 BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
1641 break;
1642 default:
1643 cpi->cpi_ncore_per_chip = 1;
1644 break;
1648 * Get CPUID data about TSC Invariance in Deep C-State.
1650 switch (cpi->cpi_vendor) {
1651 case X86_VENDOR_Intel:
1652 if (cpi->cpi_maxeax >= 7) {
1653 cp = &cpi->cpi_extd[7];
1654 cp->cp_eax = 0x80000007;
1655 cp->cp_ecx = 0;
1656 (void) __cpuid_insn(cp);
1658 break;
1659 default:
1660 break;
1662 } else {
1663 cpi->cpi_ncore_per_chip = 1;
1667 * If more than one core, then this processor is CMP.
1669 if (cpi->cpi_ncore_per_chip > 1) {
1670 add_x86_feature(featureset, X86FSET_CMP);
1674 * If the number of cores is the same as the number
1675 * of CPUs, then we cannot have HyperThreading.
1677 if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip) {
1678 remove_x86_feature(featureset, X86FSET_HTT);
1681 cpi->cpi_apicid = CPI_APIC_ID(cpi);
1682 cpi->cpi_procnodes_per_pkg = 1;
1683 cpi->cpi_cores_per_compunit = 1;
1684 if (is_x86_feature(featureset, X86FSET_HTT) == B_FALSE &&
1685 is_x86_feature(featureset, X86FSET_CMP) == B_FALSE) {
1687 * Single-core single-threaded processors.
1689 cpi->cpi_chipid = -1;
1690 cpi->cpi_clogid = 0;
1691 cpi->cpi_coreid = cpu->cpu_id;
1692 cpi->cpi_pkgcoreid = 0;
1693 if (cpi->cpi_vendor == X86_VENDOR_AMD)
1694 cpi->cpi_procnodeid = BITX(cpi->cpi_apicid, 3, 0);
1695 else
1696 cpi->cpi_procnodeid = cpi->cpi_chipid;
1697 } else if (cpi->cpi_ncpu_per_chip > 1) {
1698 if (cpi->cpi_vendor == X86_VENDOR_Intel)
1699 cpuid_intel_getids(cpu, featureset);
1700 else if (cpi->cpi_vendor == X86_VENDOR_AMD)
1701 cpuid_amd_getids(cpu);
1702 else {
1704 * All other processors are currently
1705 * assumed to have single cores.
1707 cpi->cpi_coreid = cpi->cpi_chipid;
1708 cpi->cpi_pkgcoreid = 0;
1709 cpi->cpi_procnodeid = cpi->cpi_chipid;
1710 cpi->cpi_compunitid = cpi->cpi_chipid;
1715 * Synthesize chip "revision" and socket type
1717 cpi->cpi_chiprev = _cpuid_chiprev(cpi->cpi_vendor, cpi->cpi_family,
1718 cpi->cpi_model, cpi->cpi_step);
1719 cpi->cpi_chiprevstr = _cpuid_chiprevstr(cpi->cpi_vendor,
1720 cpi->cpi_family, cpi->cpi_model, cpi->cpi_step);
1721 cpi->cpi_socket = _cpuid_skt(cpi->cpi_vendor, cpi->cpi_family,
1722 cpi->cpi_model, cpi->cpi_step);
1724 pass1_done:
1725 cpi->cpi_pass = 1;
1729 * Make copies of the cpuid table entries we depend on, in
1730 * part for ease of parsing now, in part so that we have only
1731 * one place to correct any of it, in part for ease of
1732 * later export to userland, and in part so we can look at
1733 * this stuff in a crash dump.
1736 /*ARGSUSED*/
1737 void
1738 cpuid_pass2(cpu_t *cpu)
1740 uint_t n, nmax;
1741 int i;
1742 struct cpuid_regs *cp;
1743 uint8_t *dp;
1744 uint32_t *iptr;
1745 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
1747 ASSERT(cpi->cpi_pass == 1);
1749 if (cpi->cpi_maxeax < 1)
1750 goto pass2_done;
1752 if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
1753 nmax = NMAX_CPI_STD;
1755 * (We already handled n == 0 and n == 1 in pass 1)
1757 for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
1758 cp->cp_eax = n;
1761 * CPUID function 4 expects %ecx to be initialized
1762 * with an index which indicates which cache to return
1763 * information about. The OS is expected to call function 4
1764 * with %ecx set to 0, 1, 2, ... until it returns with
1765 * EAX[4:0] set to 0, which indicates there are no more
1766 * caches.
1768 * Here, populate cpi_std[4] with the information returned by
1769 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
1770 * when dynamic memory allocation becomes available.
1772 * Note: we need to explicitly initialize %ecx here, since
1773 * function 4 may have been previously invoked.
1775 if (n == 4)
1776 cp->cp_ecx = 0;
1778 (void) __cpuid_insn(cp);
1779 platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
1780 switch (n) {
1781 case 2:
1783 * "the lower 8 bits of the %eax register
1784 * contain a value that identifies the number
1785 * of times the cpuid [instruction] has to be
1786 * executed to obtain a complete image of the
1787 * processor's caching systems."
1789 * How *do* they make this stuff up?
1791 cpi->cpi_ncache = sizeof (*cp) *
1792 BITX(cp->cp_eax, 7, 0);
1793 if (cpi->cpi_ncache == 0)
1794 break;
1795 cpi->cpi_ncache--; /* skip count byte */
1798 * Well, for now, rather than attempt to implement
1799 * this slightly dubious algorithm, we just look
1800 * at the first 15 ..
1802 if (cpi->cpi_ncache > (sizeof (*cp) - 1))
1803 cpi->cpi_ncache = sizeof (*cp) - 1;
1805 dp = cpi->cpi_cacheinfo;
1806 if (BITX(cp->cp_eax, 31, 31) == 0) {
1807 uint8_t *p = (void *)&cp->cp_eax;
1808 for (i = 1; i < 4; i++)
1809 if (p[i] != 0)
1810 *dp++ = p[i];
1812 if (BITX(cp->cp_ebx, 31, 31) == 0) {
1813 uint8_t *p = (void *)&cp->cp_ebx;
1814 for (i = 0; i < 4; i++)
1815 if (p[i] != 0)
1816 *dp++ = p[i];
1818 if (BITX(cp->cp_ecx, 31, 31) == 0) {
1819 uint8_t *p = (void *)&cp->cp_ecx;
1820 for (i = 0; i < 4; i++)
1821 if (p[i] != 0)
1822 *dp++ = p[i];
1824 if (BITX(cp->cp_edx, 31, 31) == 0) {
1825 uint8_t *p = (void *)&cp->cp_edx;
1826 for (i = 0; i < 4; i++)
1827 if (p[i] != 0)
1828 *dp++ = p[i];
1830 break;
1832 case 3: /* Processor serial number, if PSN supported */
1833 break;
1835 case 4: /* Deterministic cache parameters */
1836 break;
1838 case 5: /* Monitor/Mwait parameters */
1840 size_t mwait_size;
1843 * check cpi_mwait.support which was set in cpuid_pass1
1845 if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
1846 break;
1849 * Protect ourself from insane mwait line size.
1850 * Workaround for incomplete hardware emulator(s).
1852 mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
1853 if (mwait_size < sizeof (uint32_t) ||
1854 !ISP2(mwait_size)) {
1855 #if DEBUG
1856 cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
1857 "size %ld", cpu->cpu_id, (long)mwait_size);
1858 #endif
1859 break;
1862 cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
1863 cpi->cpi_mwait.mon_max = mwait_size;
1864 if (MWAIT_EXTENSION(cpi)) {
1865 cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
1866 if (MWAIT_INT_ENABLE(cpi))
1867 cpi->cpi_mwait.support |=
1868 MWAIT_ECX_INT_ENABLE;
1870 break;
1872 default:
1873 break;
1877 if (cpi->cpi_maxeax >= 0xB && cpi->cpi_vendor == X86_VENDOR_Intel) {
1878 struct cpuid_regs regs;
1880 cp = &regs;
1881 cp->cp_eax = 0xB;
1882 cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
1884 (void) __cpuid_insn(cp);
1887 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
1888 * indicates that the extended topology enumeration leaf is
1889 * available.
1891 if (cp->cp_ebx) {
1892 uint32_t x2apic_id;
1893 uint_t coreid_shift = 0;
1894 uint_t ncpu_per_core = 1;
1895 uint_t chipid_shift = 0;
1896 uint_t ncpu_per_chip = 1;
1897 uint_t i;
1898 uint_t level;
1900 for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
1901 cp->cp_eax = 0xB;
1902 cp->cp_ecx = i;
1904 (void) __cpuid_insn(cp);
1905 level = CPI_CPU_LEVEL_TYPE(cp);
1907 if (level == 1) {
1908 x2apic_id = cp->cp_edx;
1909 coreid_shift = BITX(cp->cp_eax, 4, 0);
1910 ncpu_per_core = BITX(cp->cp_ebx, 15, 0);
1911 } else if (level == 2) {
1912 x2apic_id = cp->cp_edx;
1913 chipid_shift = BITX(cp->cp_eax, 4, 0);
1914 ncpu_per_chip = BITX(cp->cp_ebx, 15, 0);
1918 cpi->cpi_apicid = x2apic_id;
1919 cpi->cpi_ncpu_per_chip = ncpu_per_chip;
1920 cpi->cpi_ncore_per_chip = ncpu_per_chip /
1921 ncpu_per_core;
1922 cpi->cpi_chipid = x2apic_id >> chipid_shift;
1923 cpi->cpi_clogid = x2apic_id & ((1 << chipid_shift) - 1);
1924 cpi->cpi_coreid = x2apic_id >> coreid_shift;
1925 cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
1928 /* Make cp NULL so that we don't stumble on others */
1929 cp = NULL;
1933 * XSAVE enumeration
1935 if (cpi->cpi_maxeax >= 0xD) {
1936 struct cpuid_regs regs;
1937 boolean_t cpuid_d_valid = B_TRUE;
1939 cp = &regs;
1940 cp->cp_eax = 0xD;
1941 cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
1943 (void) __cpuid_insn(cp);
1946 * Sanity checks for debug
1948 if ((cp->cp_eax & XFEATURE_LEGACY_FP) == 0 ||
1949 (cp->cp_eax & XFEATURE_SSE) == 0) {
1950 cpuid_d_valid = B_FALSE;
1953 cpi->cpi_xsave.xsav_hw_features_low = cp->cp_eax;
1954 cpi->cpi_xsave.xsav_hw_features_high = cp->cp_edx;
1955 cpi->cpi_xsave.xsav_max_size = cp->cp_ecx;
1958 * If the hw supports AVX, get the size and offset in the save
1959 * area for the ymm state.
1961 if (cpi->cpi_xsave.xsav_hw_features_low & XFEATURE_AVX) {
1962 cp->cp_eax = 0xD;
1963 cp->cp_ecx = 2;
1964 cp->cp_edx = cp->cp_ebx = 0;
1966 (void) __cpuid_insn(cp);
1968 if (cp->cp_ebx != CPUID_LEAFD_2_YMM_OFFSET ||
1969 cp->cp_eax != CPUID_LEAFD_2_YMM_SIZE) {
1970 cpuid_d_valid = B_FALSE;
1973 cpi->cpi_xsave.ymm_size = cp->cp_eax;
1974 cpi->cpi_xsave.ymm_offset = cp->cp_ebx;
1977 if (is_x86_feature(x86_featureset, X86FSET_XSAVE)) {
1978 xsave_state_size = 0;
1979 } else if (cpuid_d_valid) {
1980 xsave_state_size = cpi->cpi_xsave.xsav_max_size;
1981 } else {
1982 /* Broken CPUID 0xD, probably in HVM */
1983 cmn_err(CE_WARN, "cpu%d: CPUID.0xD returns invalid "
1984 "value: hw_low = %d, hw_high = %d, xsave_size = %d"
1985 ", ymm_size = %d, ymm_offset = %d\n",
1986 cpu->cpu_id, cpi->cpi_xsave.xsav_hw_features_low,
1987 cpi->cpi_xsave.xsav_hw_features_high,
1988 (int)cpi->cpi_xsave.xsav_max_size,
1989 (int)cpi->cpi_xsave.ymm_size,
1990 (int)cpi->cpi_xsave.ymm_offset);
1992 if (xsave_state_size != 0) {
1994 * This must be a non-boot CPU. We cannot
1995 * continue, because boot cpu has already
1996 * enabled XSAVE.
1998 ASSERT(cpu->cpu_id != 0);
1999 cmn_err(CE_PANIC, "cpu%d: we have already "
2000 "enabled XSAVE on boot cpu, cannot "
2001 "continue.", cpu->cpu_id);
2002 } else {
2004 * If we reached here on the boot CPU, it's also
2005 * almost certain that we'll reach here on the
2006 * non-boot CPUs. When we're here on a boot CPU
2007 * we should disable the feature, on a non-boot
2008 * CPU we need to confirm that we have.
2010 if (cpu->cpu_id == 0) {
2011 remove_x86_feature(x86_featureset,
2012 X86FSET_XSAVE);
2013 remove_x86_feature(x86_featureset,
2014 X86FSET_AVX);
2015 remove_x86_feature(x86_featureset,
2016 X86FSET_F16C);
2017 remove_x86_feature(x86_featureset,
2018 X86FSET_BMI1);
2019 remove_x86_feature(x86_featureset,
2020 X86FSET_BMI2);
2021 remove_x86_feature(x86_featureset,
2022 X86FSET_FMA);
2023 remove_x86_feature(x86_featureset,
2024 X86FSET_AVX2);
2025 CPI_FEATURES_ECX(cpi) &=
2026 ~CPUID_INTC_ECX_XSAVE;
2027 CPI_FEATURES_ECX(cpi) &=
2028 ~CPUID_INTC_ECX_AVX;
2029 CPI_FEATURES_ECX(cpi) &=
2030 ~CPUID_INTC_ECX_F16C;
2031 CPI_FEATURES_ECX(cpi) &=
2032 ~CPUID_INTC_ECX_FMA;
2033 CPI_FEATURES_7_0_EBX(cpi) &=
2034 ~CPUID_INTC_EBX_7_0_BMI1;
2035 CPI_FEATURES_7_0_EBX(cpi) &=
2036 ~CPUID_INTC_EBX_7_0_BMI2;
2037 CPI_FEATURES_7_0_EBX(cpi) &=
2038 ~CPUID_INTC_EBX_7_0_AVX2;
2039 xsave_force_disable = B_TRUE;
2040 } else {
2041 VERIFY(is_x86_feature(x86_featureset,
2042 X86FSET_XSAVE) == B_FALSE);
2049 if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
2050 goto pass2_done;
2052 if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
2053 nmax = NMAX_CPI_EXTD;
2055 * Copy the extended properties, fixing them as we go.
2056 * (We already handled n == 0 and n == 1 in pass 1)
2058 iptr = (void *)cpi->cpi_brandstr;
2059 for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
2060 cp->cp_eax = 0x80000000 + n;
2061 (void) __cpuid_insn(cp);
2062 platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
2063 switch (n) {
2064 case 2:
2065 case 3:
2066 case 4:
2068 * Extract the brand string
2070 *iptr++ = cp->cp_eax;
2071 *iptr++ = cp->cp_ebx;
2072 *iptr++ = cp->cp_ecx;
2073 *iptr++ = cp->cp_edx;
2074 break;
2075 case 5:
2076 switch (cpi->cpi_vendor) {
2077 case X86_VENDOR_AMD:
2079 * The Athlon and Duron were the first
2080 * parts to report the sizes of the
2081 * TLB for large pages. Before then,
2082 * we don't trust the data.
2084 if (cpi->cpi_family < 6 ||
2085 (cpi->cpi_family == 6 &&
2086 cpi->cpi_model < 1))
2087 cp->cp_eax = 0;
2088 break;
2089 default:
2090 break;
2092 break;
2093 case 6:
2094 switch (cpi->cpi_vendor) {
2095 case X86_VENDOR_AMD:
2097 * The Athlon and Duron were the first
2098 * AMD parts with L2 TLB's.
2099 * Before then, don't trust the data.
2101 if (cpi->cpi_family < 6 ||
2102 cpi->cpi_family == 6 &&
2103 cpi->cpi_model < 1)
2104 cp->cp_eax = cp->cp_ebx = 0;
2106 * AMD Duron rev A0 reports L2
2107 * cache size incorrectly as 1K
2108 * when it is really 64K
2110 if (cpi->cpi_family == 6 &&
2111 cpi->cpi_model == 3 &&
2112 cpi->cpi_step == 0) {
2113 cp->cp_ecx &= 0xffff;
2114 cp->cp_ecx |= 0x400000;
2116 break;
2117 case X86_VENDOR_Cyrix: /* VIA C3 */
2119 * VIA C3 processors are a bit messed
2120 * up w.r.t. encoding cache sizes in %ecx
2122 if (cpi->cpi_family != 6)
2123 break;
2125 * model 7 and 8 were incorrectly encoded
2127 * xxx is model 8 really broken?
2129 if (cpi->cpi_model == 7 ||
2130 cpi->cpi_model == 8)
2131 cp->cp_ecx =
2132 BITX(cp->cp_ecx, 31, 24) << 16 |
2133 BITX(cp->cp_ecx, 23, 16) << 12 |
2134 BITX(cp->cp_ecx, 15, 8) << 8 |
2135 BITX(cp->cp_ecx, 7, 0);
2137 * model 9 stepping 1 has wrong associativity
2139 if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
2140 cp->cp_ecx |= 8 << 12;
2141 break;
2142 case X86_VENDOR_Intel:
2144 * Extended L2 Cache features function.
2145 * First appeared on Prescott.
2147 default:
2148 break;
2150 break;
2151 default:
2152 break;
2156 pass2_done:
2157 cpi->cpi_pass = 2;
2160 static const char *
2161 intel_cpubrand(const struct cpuid_info *cpi)
2163 int i;
2165 if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2166 cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
2167 return ("i486");
2169 switch (cpi->cpi_family) {
2170 case 5:
2171 return ("Intel Pentium(r)");
2172 case 6:
2173 switch (cpi->cpi_model) {
2174 uint_t celeron, xeon;
2175 const struct cpuid_regs *cp;
2176 case 0:
2177 case 1:
2178 case 2:
2179 return ("Intel Pentium(r) Pro");
2180 case 3:
2181 case 4:
2182 return ("Intel Pentium(r) II");
2183 case 6:
2184 return ("Intel Celeron(r)");
2185 case 5:
2186 case 7:
2187 celeron = xeon = 0;
2188 cp = &cpi->cpi_std[2]; /* cache info */
2190 for (i = 1; i < 4; i++) {
2191 uint_t tmp;
2193 tmp = (cp->cp_eax >> (8 * i)) & 0xff;
2194 if (tmp == 0x40)
2195 celeron++;
2196 if (tmp >= 0x44 && tmp <= 0x45)
2197 xeon++;
2200 for (i = 0; i < 2; i++) {
2201 uint_t tmp;
2203 tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
2204 if (tmp == 0x40)
2205 celeron++;
2206 else if (tmp >= 0x44 && tmp <= 0x45)
2207 xeon++;
2210 for (i = 0; i < 4; i++) {
2211 uint_t tmp;
2213 tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
2214 if (tmp == 0x40)
2215 celeron++;
2216 else if (tmp >= 0x44 && tmp <= 0x45)
2217 xeon++;
2220 for (i = 0; i < 4; i++) {
2221 uint_t tmp;
2223 tmp = (cp->cp_edx >> (8 * i)) & 0xff;
2224 if (tmp == 0x40)
2225 celeron++;
2226 else if (tmp >= 0x44 && tmp <= 0x45)
2227 xeon++;
2230 if (celeron)
2231 return ("Intel Celeron(r)");
2232 if (xeon)
2233 return (cpi->cpi_model == 5 ?
2234 "Intel Pentium(r) II Xeon(tm)" :
2235 "Intel Pentium(r) III Xeon(tm)");
2236 return (cpi->cpi_model == 5 ?
2237 "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
2238 "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
2239 default:
2240 break;
2242 default:
2243 break;
2246 /* BrandID is present if the field is nonzero */
2247 if (cpi->cpi_brandid != 0) {
2248 static const struct {
2249 uint_t bt_bid;
2250 const char *bt_str;
2251 } brand_tbl[] = {
2252 { 0x1, "Intel(r) Celeron(r)" },
2253 { 0x2, "Intel(r) Pentium(r) III" },
2254 { 0x3, "Intel(r) Pentium(r) III Xeon(tm)" },
2255 { 0x4, "Intel(r) Pentium(r) III" },
2256 { 0x6, "Mobile Intel(r) Pentium(r) III" },
2257 { 0x7, "Mobile Intel(r) Celeron(r)" },
2258 { 0x8, "Intel(r) Pentium(r) 4" },
2259 { 0x9, "Intel(r) Pentium(r) 4" },
2260 { 0xa, "Intel(r) Celeron(r)" },
2261 { 0xb, "Intel(r) Xeon(tm)" },
2262 { 0xc, "Intel(r) Xeon(tm) MP" },
2263 { 0xe, "Mobile Intel(r) Pentium(r) 4" },
2264 { 0xf, "Mobile Intel(r) Celeron(r)" },
2265 { 0x11, "Mobile Genuine Intel(r)" },
2266 { 0x12, "Intel(r) Celeron(r) M" },
2267 { 0x13, "Mobile Intel(r) Celeron(r)" },
2268 { 0x14, "Intel(r) Celeron(r)" },
2269 { 0x15, "Mobile Genuine Intel(r)" },
2270 { 0x16, "Intel(r) Pentium(r) M" },
2271 { 0x17, "Mobile Intel(r) Celeron(r)" }
2273 uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
2274 uint_t sgn;
2276 sgn = (cpi->cpi_family << 8) |
2277 (cpi->cpi_model << 4) | cpi->cpi_step;
2279 for (i = 0; i < btblmax; i++)
2280 if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
2281 break;
2282 if (i < btblmax) {
2283 if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
2284 return ("Intel(r) Celeron(r)");
2285 if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
2286 return ("Intel(r) Xeon(tm) MP");
2287 if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
2288 return ("Intel(r) Xeon(tm)");
2289 return (brand_tbl[i].bt_str);
2293 return (NULL);
2296 static const char *
2297 amd_cpubrand(const struct cpuid_info *cpi)
2299 if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2300 cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
2301 return ("i486 compatible");
2303 switch (cpi->cpi_family) {
2304 case 5:
2305 switch (cpi->cpi_model) {
2306 case 0:
2307 case 1:
2308 case 2:
2309 case 3:
2310 case 4:
2311 case 5:
2312 return ("AMD-K5(r)");
2313 case 6:
2314 case 7:
2315 return ("AMD-K6(r)");
2316 case 8:
2317 return ("AMD-K6(r)-2");
2318 case 9:
2319 return ("AMD-K6(r)-III");
2320 default:
2321 return ("AMD (family 5)");
2323 case 6:
2324 switch (cpi->cpi_model) {
2325 case 1:
2326 return ("AMD-K7(tm)");
2327 case 0:
2328 case 2:
2329 case 4:
2330 return ("AMD Athlon(tm)");
2331 case 3:
2332 case 7:
2333 return ("AMD Duron(tm)");
2334 case 6:
2335 case 8:
2336 case 10:
2338 * Use the L2 cache size to distinguish
2340 return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
2341 "AMD Athlon(tm)" : "AMD Duron(tm)");
2342 default:
2343 return ("AMD (family 6)");
2345 default:
2346 break;
2349 if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
2350 cpi->cpi_brandid != 0) {
2351 switch (BITX(cpi->cpi_brandid, 7, 5)) {
2352 case 3:
2353 return ("AMD Opteron(tm) UP 1xx");
2354 case 4:
2355 return ("AMD Opteron(tm) DP 2xx");
2356 case 5:
2357 return ("AMD Opteron(tm) MP 8xx");
2358 default:
2359 return ("AMD Opteron(tm)");
2363 return (NULL);
2366 static const char *
2367 cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
2369 if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2370 cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
2371 type == X86_TYPE_CYRIX_486)
2372 return ("i486 compatible");
2374 switch (type) {
2375 case X86_TYPE_CYRIX_6x86:
2376 return ("Cyrix 6x86");
2377 case X86_TYPE_CYRIX_6x86L:
2378 return ("Cyrix 6x86L");
2379 case X86_TYPE_CYRIX_6x86MX:
2380 return ("Cyrix 6x86MX");
2381 case X86_TYPE_CYRIX_GXm:
2382 return ("Cyrix GXm");
2383 case X86_TYPE_CYRIX_MediaGX:
2384 return ("Cyrix MediaGX");
2385 case X86_TYPE_CYRIX_MII:
2386 return ("Cyrix M2");
2387 case X86_TYPE_VIA_CYRIX_III:
2388 return ("VIA Cyrix M3");
2389 default:
2391 * Have another wild guess ..
2393 if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
2394 return ("Cyrix 5x86");
2395 else if (cpi->cpi_family == 5) {
2396 switch (cpi->cpi_model) {
2397 case 2:
2398 return ("Cyrix 6x86"); /* Cyrix M1 */
2399 case 4:
2400 return ("Cyrix MediaGX");
2401 default:
2402 break;
2404 } else if (cpi->cpi_family == 6) {
2405 switch (cpi->cpi_model) {
2406 case 0:
2407 return ("Cyrix 6x86MX"); /* Cyrix M2? */
2408 case 5:
2409 case 6:
2410 case 7:
2411 case 8:
2412 case 9:
2413 return ("VIA C3");
2414 default:
2415 break;
2418 break;
2420 return (NULL);
2424 * This only gets called in the case that the CPU extended
2425 * feature brand string (0x80000002, 0x80000003, 0x80000004)
2426 * aren't available, or contain null bytes for some reason.
2428 static void
2429 fabricate_brandstr(struct cpuid_info *cpi)
2431 const char *brand = NULL;
2433 switch (cpi->cpi_vendor) {
2434 case X86_VENDOR_Intel:
2435 brand = intel_cpubrand(cpi);
2436 break;
2437 case X86_VENDOR_AMD:
2438 brand = amd_cpubrand(cpi);
2439 break;
2440 case X86_VENDOR_Cyrix:
2441 brand = cyrix_cpubrand(cpi, x86_type);
2442 break;
2443 case X86_VENDOR_NexGen:
2444 if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
2445 brand = "NexGen Nx586";
2446 break;
2447 case X86_VENDOR_Centaur:
2448 if (cpi->cpi_family == 5)
2449 switch (cpi->cpi_model) {
2450 case 4:
2451 brand = "Centaur C6";
2452 break;
2453 case 8:
2454 brand = "Centaur C2";
2455 break;
2456 case 9:
2457 brand = "Centaur C3";
2458 break;
2459 default:
2460 break;
2462 break;
2463 case X86_VENDOR_Rise:
2464 if (cpi->cpi_family == 5 &&
2465 (cpi->cpi_model == 0 || cpi->cpi_model == 2))
2466 brand = "Rise mP6";
2467 break;
2468 case X86_VENDOR_SiS:
2469 if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
2470 brand = "SiS 55x";
2471 break;
2472 case X86_VENDOR_TM:
2473 if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
2474 brand = "Transmeta Crusoe TM3x00 or TM5x00";
2475 break;
2476 case X86_VENDOR_NSC:
2477 case X86_VENDOR_UMC:
2478 default:
2479 break;
2481 if (brand) {
2482 (void) strcpy((char *)cpi->cpi_brandstr, brand);
2483 return;
2487 * If all else fails ...
2489 (void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
2490 "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
2491 cpi->cpi_model, cpi->cpi_step);
2495 * This routine is called just after kernel memory allocation
2496 * becomes available on cpu0, and as part of mp_startup() on
2497 * the other cpus.
2499 * Fixup the brand string, and collect any information from cpuid
2500 * that requires dynamically allocated storage to represent.
2502 /*ARGSUSED*/
2503 void
2504 cpuid_pass3(cpu_t *cpu)
2506 int i, max, shft, level, size;
2507 struct cpuid_regs regs;
2508 struct cpuid_regs *cp;
2509 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2511 ASSERT(cpi->cpi_pass == 2);
2514 * Function 4: Deterministic cache parameters
2516 * Take this opportunity to detect the number of threads
2517 * sharing the last level cache, and construct a corresponding
2518 * cache id. The respective cpuid_info members are initialized
2519 * to the default case of "no last level cache sharing".
2521 cpi->cpi_ncpu_shr_last_cache = 1;
2522 cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
2524 if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
2527 * Find the # of elements (size) returned by fn 4, and along
2528 * the way detect last level cache sharing details.
2530 bzero(&regs, sizeof (regs));
2531 cp = &regs;
2532 for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
2533 cp->cp_eax = 4;
2534 cp->cp_ecx = i;
2536 (void) __cpuid_insn(cp);
2538 if (CPI_CACHE_TYPE(cp) == 0)
2539 break;
2540 level = CPI_CACHE_LVL(cp);
2541 if (level > max) {
2542 max = level;
2543 cpi->cpi_ncpu_shr_last_cache =
2544 CPI_NTHR_SHR_CACHE(cp) + 1;
2547 cpi->cpi_std_4_size = size = i;
2550 * Allocate the cpi_std_4 array. The first element
2551 * references the regs for fn 4, %ecx == 0, which
2552 * cpuid_pass2() stashed in cpi->cpi_std[4].
2554 if (size > 0) {
2555 cpi->cpi_std_4 =
2556 kmem_alloc(size * sizeof (cp), KM_SLEEP);
2557 cpi->cpi_std_4[0] = &cpi->cpi_std[4];
2560 * Allocate storage to hold the additional regs
2561 * for function 4, %ecx == 1 .. cpi_std_4_size.
2563 * The regs for fn 4, %ecx == 0 has already
2564 * been allocated as indicated above.
2566 for (i = 1; i < size; i++) {
2567 cp = cpi->cpi_std_4[i] =
2568 kmem_zalloc(sizeof (regs), KM_SLEEP);
2569 cp->cp_eax = 4;
2570 cp->cp_ecx = i;
2572 (void) __cpuid_insn(cp);
2576 * Determine the number of bits needed to represent
2577 * the number of CPUs sharing the last level cache.
2579 * Shift off that number of bits from the APIC id to
2580 * derive the cache id.
2582 shft = 0;
2583 for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
2584 shft++;
2585 cpi->cpi_last_lvl_cacheid = cpi->cpi_apicid >> shft;
2589 * Now fixup the brand string
2591 if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
2592 fabricate_brandstr(cpi);
2593 } else {
2596 * If we successfully extracted a brand string from the cpuid
2597 * instruction, clean it up by removing leading spaces and
2598 * similar junk.
2600 if (cpi->cpi_brandstr[0]) {
2601 size_t maxlen = sizeof (cpi->cpi_brandstr);
2602 char *src, *dst;
2604 dst = src = (char *)cpi->cpi_brandstr;
2605 src[maxlen - 1] = '\0';
2607 * strip leading spaces
2609 while (*src == ' ')
2610 src++;
2612 * Remove any 'Genuine' or "Authentic" prefixes
2614 if (strncmp(src, "Genuine ", 8) == 0)
2615 src += 8;
2616 if (strncmp(src, "Authentic ", 10) == 0)
2617 src += 10;
2620 * Now do an in-place copy.
2621 * Map (R) to (r) and (TM) to (tm).
2622 * The era of teletypes is long gone, and there's
2623 * -really- no need to shout.
2625 while (*src != '\0') {
2626 if (src[0] == '(') {
2627 if (strncmp(src + 1, "R)", 2) == 0) {
2628 (void) strncpy(dst, "(r)", 3);
2629 src += 3;
2630 dst += 3;
2631 continue;
2633 if (strncmp(src + 1, "TM)", 3) == 0) {
2634 (void) strncpy(dst, "(tm)", 4);
2635 src += 4;
2636 dst += 4;
2637 continue;
2640 *dst++ = *src++;
2642 *dst = '\0';
2645 * Finally, remove any trailing spaces
2647 while (--dst > cpi->cpi_brandstr)
2648 if (*dst == ' ')
2649 *dst = '\0';
2650 else
2651 break;
2652 } else
2653 fabricate_brandstr(cpi);
2655 cpi->cpi_pass = 3;
2659 * This routine is called out of bind_hwcap() much later in the life
2660 * of the kernel (post_startup()). The job of this routine is to resolve
2661 * the hardware feature support and kernel support for those features into
2662 * what we're actually going to tell applications via the aux vector.
2664 void
2665 cpuid_pass4(cpu_t *cpu, uint_t *hwcap_out)
2667 struct cpuid_info *cpi;
2668 uint_t hwcap_flags = 0, hwcap_flags_2 = 0;
2670 if (cpu == NULL)
2671 cpu = CPU;
2672 cpi = cpu->cpu_m.mcpu_cpi;
2674 ASSERT(cpi->cpi_pass == 3);
2676 if (cpi->cpi_maxeax >= 1) {
2677 uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
2678 uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
2679 uint32_t *ebx = &cpi->cpi_support[STD_EBX_FEATURES];
2681 *edx = CPI_FEATURES_EDX(cpi);
2682 *ecx = CPI_FEATURES_ECX(cpi);
2683 *ebx = CPI_FEATURES_7_0_EBX(cpi);
2686 * [these require explicit kernel support]
2688 if (!is_x86_feature(x86_featureset, X86FSET_SEP))
2689 *edx &= ~CPUID_INTC_EDX_SEP;
2691 if (!is_x86_feature(x86_featureset, X86FSET_SSE))
2692 *edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
2693 if (!is_x86_feature(x86_featureset, X86FSET_SSE2))
2694 *edx &= ~CPUID_INTC_EDX_SSE2;
2696 if (!is_x86_feature(x86_featureset, X86FSET_HTT))
2697 *edx &= ~CPUID_INTC_EDX_HTT;
2699 if (!is_x86_feature(x86_featureset, X86FSET_SSE3))
2700 *ecx &= ~CPUID_INTC_ECX_SSE3;
2702 if (!is_x86_feature(x86_featureset, X86FSET_SSSE3))
2703 *ecx &= ~CPUID_INTC_ECX_SSSE3;
2704 if (!is_x86_feature(x86_featureset, X86FSET_SSE4_1))
2705 *ecx &= ~CPUID_INTC_ECX_SSE4_1;
2706 if (!is_x86_feature(x86_featureset, X86FSET_SSE4_2))
2707 *ecx &= ~CPUID_INTC_ECX_SSE4_2;
2708 if (!is_x86_feature(x86_featureset, X86FSET_AES))
2709 *ecx &= ~CPUID_INTC_ECX_AES;
2710 if (!is_x86_feature(x86_featureset, X86FSET_PCLMULQDQ))
2711 *ecx &= ~CPUID_INTC_ECX_PCLMULQDQ;
2712 if (!is_x86_feature(x86_featureset, X86FSET_XSAVE))
2713 *ecx &= ~(CPUID_INTC_ECX_XSAVE |
2714 CPUID_INTC_ECX_OSXSAVE);
2715 if (!is_x86_feature(x86_featureset, X86FSET_AVX))
2716 *ecx &= ~CPUID_INTC_ECX_AVX;
2717 if (!is_x86_feature(x86_featureset, X86FSET_F16C))
2718 *ecx &= ~CPUID_INTC_ECX_F16C;
2719 if (!is_x86_feature(x86_featureset, X86FSET_FMA))
2720 *ecx &= ~CPUID_INTC_ECX_FMA;
2721 if (!is_x86_feature(x86_featureset, X86FSET_BMI1))
2722 *ebx &= ~CPUID_INTC_EBX_7_0_BMI1;
2723 if (!is_x86_feature(x86_featureset, X86FSET_BMI2))
2724 *ebx &= ~CPUID_INTC_EBX_7_0_BMI2;
2725 if (!is_x86_feature(x86_featureset, X86FSET_AVX2))
2726 *ebx &= ~CPUID_INTC_EBX_7_0_AVX2;
2729 * [no explicit support required beyond x87 fp context]
2731 if (!fpu_exists)
2732 *edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
2735 * Now map the supported feature vector to things that we
2736 * think userland will care about.
2738 if (*edx & CPUID_INTC_EDX_SEP)
2739 hwcap_flags |= AV_386_SEP;
2740 if (*edx & CPUID_INTC_EDX_SSE)
2741 hwcap_flags |= AV_386_FXSR | AV_386_SSE;
2742 if (*edx & CPUID_INTC_EDX_SSE2)
2743 hwcap_flags |= AV_386_SSE2;
2744 if (*ecx & CPUID_INTC_ECX_SSE3)
2745 hwcap_flags |= AV_386_SSE3;
2746 if (*ecx & CPUID_INTC_ECX_SSSE3)
2747 hwcap_flags |= AV_386_SSSE3;
2748 if (*ecx & CPUID_INTC_ECX_SSE4_1)
2749 hwcap_flags |= AV_386_SSE4_1;
2750 if (*ecx & CPUID_INTC_ECX_SSE4_2)
2751 hwcap_flags |= AV_386_SSE4_2;
2752 if (*ecx & CPUID_INTC_ECX_MOVBE)
2753 hwcap_flags |= AV_386_MOVBE;
2754 if (*ecx & CPUID_INTC_ECX_AES)
2755 hwcap_flags |= AV_386_AES;
2756 if (*ecx & CPUID_INTC_ECX_PCLMULQDQ)
2757 hwcap_flags |= AV_386_PCLMULQDQ;
2758 if ((*ecx & CPUID_INTC_ECX_XSAVE) &&
2759 (*ecx & CPUID_INTC_ECX_OSXSAVE)) {
2760 hwcap_flags |= AV_386_XSAVE;
2762 if (*ecx & CPUID_INTC_ECX_AVX) {
2763 hwcap_flags |= AV_386_AVX;
2764 if (*ecx & CPUID_INTC_ECX_F16C)
2765 hwcap_flags_2 |= AV_386_2_F16C;
2766 if (*ecx & CPUID_INTC_ECX_FMA)
2767 hwcap_flags_2 |= AV_386_2_FMA;
2768 if (*ebx & CPUID_INTC_EBX_7_0_BMI1)
2769 hwcap_flags_2 |= AV_386_2_BMI1;
2770 if (*ebx & CPUID_INTC_EBX_7_0_BMI2)
2771 hwcap_flags_2 |= AV_386_2_BMI2;
2772 if (*ebx & CPUID_INTC_EBX_7_0_AVX2)
2773 hwcap_flags_2 |= AV_386_2_AVX2;
2776 if (*ecx & CPUID_INTC_ECX_VMX)
2777 hwcap_flags |= AV_386_VMX;
2778 if (*ecx & CPUID_INTC_ECX_POPCNT)
2779 hwcap_flags |= AV_386_POPCNT;
2780 if (*edx & CPUID_INTC_EDX_FPU)
2781 hwcap_flags |= AV_386_FPU;
2782 if (*edx & CPUID_INTC_EDX_MMX)
2783 hwcap_flags |= AV_386_MMX;
2785 if (*edx & CPUID_INTC_EDX_TSC)
2786 hwcap_flags |= AV_386_TSC;
2787 if (*edx & CPUID_INTC_EDX_CX8)
2788 hwcap_flags |= AV_386_CX8;
2789 if (*edx & CPUID_INTC_EDX_CMOV)
2790 hwcap_flags |= AV_386_CMOV;
2791 if (*ecx & CPUID_INTC_ECX_CX16)
2792 hwcap_flags |= AV_386_CX16;
2794 if (*ecx & CPUID_INTC_ECX_RDRAND)
2795 hwcap_flags_2 |= AV_386_2_RDRAND;
2798 if (cpi->cpi_xmaxeax < 0x80000001)
2799 goto pass4_done;
2801 switch (cpi->cpi_vendor) {
2802 struct cpuid_regs cp;
2803 uint32_t *edx, *ecx;
2805 case X86_VENDOR_Intel:
2807 * Seems like Intel duplicated what we necessary
2808 * here to make the initial crop of 64-bit OS's work.
2809 * Hopefully, those are the only "extended" bits
2810 * they'll add.
2812 /*FALLTHROUGH*/
2814 case X86_VENDOR_AMD:
2815 edx = &cpi->cpi_support[AMD_EDX_FEATURES];
2816 ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
2818 *edx = CPI_FEATURES_XTD_EDX(cpi);
2819 *ecx = CPI_FEATURES_XTD_ECX(cpi);
2822 * [these features require explicit kernel support]
2824 switch (cpi->cpi_vendor) {
2825 case X86_VENDOR_Intel:
2826 if (!is_x86_feature(x86_featureset, X86FSET_TSCP))
2827 *edx &= ~CPUID_AMD_EDX_TSCP;
2828 break;
2830 case X86_VENDOR_AMD:
2831 if (!is_x86_feature(x86_featureset, X86FSET_TSCP))
2832 *edx &= ~CPUID_AMD_EDX_TSCP;
2833 if (!is_x86_feature(x86_featureset, X86FSET_SSE4A))
2834 *ecx &= ~CPUID_AMD_ECX_SSE4A;
2835 break;
2837 default:
2838 break;
2842 * [no explicit support required beyond
2843 * x87 fp context and exception handlers]
2845 if (!fpu_exists)
2846 *edx &= ~(CPUID_AMD_EDX_MMXamd |
2847 CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
2849 if (!is_x86_feature(x86_featureset, X86FSET_NX))
2850 *edx &= ~CPUID_AMD_EDX_NX;
2851 #if !defined(__amd64)
2852 *edx &= ~CPUID_AMD_EDX_LM;
2853 #endif
2855 * Now map the supported feature vector to
2856 * things that we think userland will care about.
2858 #if defined(__amd64)
2859 if (*edx & CPUID_AMD_EDX_SYSC)
2860 hwcap_flags |= AV_386_AMD_SYSC;
2861 #endif
2862 if (*edx & CPUID_AMD_EDX_MMXamd)
2863 hwcap_flags |= AV_386_AMD_MMX;
2864 if (*edx & CPUID_AMD_EDX_3DNow)
2865 hwcap_flags |= AV_386_AMD_3DNow;
2866 if (*edx & CPUID_AMD_EDX_3DNowx)
2867 hwcap_flags |= AV_386_AMD_3DNowx;
2868 if (*ecx & CPUID_AMD_ECX_SVM)
2869 hwcap_flags |= AV_386_AMD_SVM;
2871 switch (cpi->cpi_vendor) {
2872 case X86_VENDOR_AMD:
2873 if (*edx & CPUID_AMD_EDX_TSCP)
2874 hwcap_flags |= AV_386_TSCP;
2875 if (*ecx & CPUID_AMD_ECX_AHF64)
2876 hwcap_flags |= AV_386_AHF;
2877 if (*ecx & CPUID_AMD_ECX_SSE4A)
2878 hwcap_flags |= AV_386_AMD_SSE4A;
2879 if (*ecx & CPUID_AMD_ECX_LZCNT)
2880 hwcap_flags |= AV_386_AMD_LZCNT;
2881 break;
2883 case X86_VENDOR_Intel:
2884 if (*edx & CPUID_AMD_EDX_TSCP)
2885 hwcap_flags |= AV_386_TSCP;
2887 * Aarrgh.
2888 * Intel uses a different bit in the same word.
2890 if (*ecx & CPUID_INTC_ECX_AHF64)
2891 hwcap_flags |= AV_386_AHF;
2892 break;
2894 default:
2895 break;
2897 break;
2899 case X86_VENDOR_TM:
2900 cp.cp_eax = 0x80860001;
2901 (void) __cpuid_insn(&cp);
2902 cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
2903 break;
2905 default:
2906 break;
2909 pass4_done:
2910 cpi->cpi_pass = 4;
2911 if (hwcap_out != NULL) {
2912 hwcap_out[0] = hwcap_flags;
2913 hwcap_out[1] = hwcap_flags_2;
2919 * Simulate the cpuid instruction using the data we previously
2920 * captured about this CPU. We try our best to return the truth
2921 * about the hardware, independently of kernel support.
2923 uint32_t
2924 cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
2926 struct cpuid_info *cpi;
2927 struct cpuid_regs *xcp;
2929 if (cpu == NULL)
2930 cpu = CPU;
2931 cpi = cpu->cpu_m.mcpu_cpi;
2933 ASSERT(cpuid_checkpass(cpu, 3));
2936 * CPUID data is cached in two separate places: cpi_std for standard
2937 * CPUID functions, and cpi_extd for extended CPUID functions.
2939 if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
2940 xcp = &cpi->cpi_std[cp->cp_eax];
2941 else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
2942 cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
2943 xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
2944 else
2946 * The caller is asking for data from an input parameter which
2947 * the kernel has not cached. In this case we go fetch from
2948 * the hardware and return the data directly to the user.
2950 return (__cpuid_insn(cp));
2952 cp->cp_eax = xcp->cp_eax;
2953 cp->cp_ebx = xcp->cp_ebx;
2954 cp->cp_ecx = xcp->cp_ecx;
2955 cp->cp_edx = xcp->cp_edx;
2956 return (cp->cp_eax);
2960 cpuid_checkpass(cpu_t *cpu, int pass)
2962 return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
2963 cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
2967 cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
2969 ASSERT(cpuid_checkpass(cpu, 3));
2971 return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
2975 cpuid_is_cmt(cpu_t *cpu)
2977 if (cpu == NULL)
2978 cpu = CPU;
2980 ASSERT(cpuid_checkpass(cpu, 1));
2982 return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
2986 * AMD and Intel both implement the 64-bit variant of the syscall
2987 * instruction (syscallq), so if there's -any- support for syscall,
2988 * cpuid currently says "yes, we support this".
2990 * However, Intel decided to -not- implement the 32-bit variant of the
2991 * syscall instruction, so we provide a predicate to allow our caller
2992 * to test that subtlety here.
2994 * XXPV Currently, 32-bit syscall instructions don't work via the hypervisor,
2995 * even in the case where the hardware would in fact support it.
2997 /*ARGSUSED*/
2999 cpuid_syscall32_insn(cpu_t *cpu)
3001 ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
3003 #if !defined(__xpv)
3004 if (cpu == NULL)
3005 cpu = CPU;
3007 /*CSTYLED*/
3009 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3011 if (cpi->cpi_vendor == X86_VENDOR_AMD &&
3012 cpi->cpi_xmaxeax >= 0x80000001 &&
3013 (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
3014 return (1);
3016 #endif
3017 return (0);
3021 cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
3023 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3025 static const char fmt[] =
3026 "x86 (%s %X family %d model %d step %d clock %d MHz)";
3027 static const char fmt_ht[] =
3028 "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
3030 ASSERT(cpuid_checkpass(cpu, 1));
3032 if (cpuid_is_cmt(cpu))
3033 return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
3034 cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
3035 cpi->cpi_family, cpi->cpi_model,
3036 cpi->cpi_step, cpu->cpu_type_info.pi_clock));
3037 return (snprintf(s, n, fmt,
3038 cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
3039 cpi->cpi_family, cpi->cpi_model,
3040 cpi->cpi_step, cpu->cpu_type_info.pi_clock));
3043 const char *
3044 cpuid_getvendorstr(cpu_t *cpu)
3046 ASSERT(cpuid_checkpass(cpu, 1));
3047 return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
3050 uint_t
3051 cpuid_getvendor(cpu_t *cpu)
3053 ASSERT(cpuid_checkpass(cpu, 1));
3054 return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
3057 uint_t
3058 cpuid_getfamily(cpu_t *cpu)
3060 ASSERT(cpuid_checkpass(cpu, 1));
3061 return (cpu->cpu_m.mcpu_cpi->cpi_family);
3064 uint_t
3065 cpuid_getmodel(cpu_t *cpu)
3067 ASSERT(cpuid_checkpass(cpu, 1));
3068 return (cpu->cpu_m.mcpu_cpi->cpi_model);
3071 uint_t
3072 cpuid_get_ncpu_per_chip(cpu_t *cpu)
3074 ASSERT(cpuid_checkpass(cpu, 1));
3075 return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
3078 uint_t
3079 cpuid_get_ncore_per_chip(cpu_t *cpu)
3081 ASSERT(cpuid_checkpass(cpu, 1));
3082 return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
3085 uint_t
3086 cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
3088 ASSERT(cpuid_checkpass(cpu, 2));
3089 return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
3092 id_t
3093 cpuid_get_last_lvl_cacheid(cpu_t *cpu)
3095 ASSERT(cpuid_checkpass(cpu, 2));
3096 return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
3099 uint_t
3100 cpuid_getstep(cpu_t *cpu)
3102 ASSERT(cpuid_checkpass(cpu, 1));
3103 return (cpu->cpu_m.mcpu_cpi->cpi_step);
3106 uint_t
3107 cpuid_getsig(struct cpu *cpu)
3109 ASSERT(cpuid_checkpass(cpu, 1));
3110 return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
3113 uint32_t
3114 cpuid_getchiprev(struct cpu *cpu)
3116 ASSERT(cpuid_checkpass(cpu, 1));
3117 return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
3120 const char *
3121 cpuid_getchiprevstr(struct cpu *cpu)
3123 ASSERT(cpuid_checkpass(cpu, 1));
3124 return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
3127 uint32_t
3128 cpuid_getsockettype(struct cpu *cpu)
3130 ASSERT(cpuid_checkpass(cpu, 1));
3131 return (cpu->cpu_m.mcpu_cpi->cpi_socket);
3134 const char *
3135 cpuid_getsocketstr(cpu_t *cpu)
3137 static const char *socketstr = NULL;
3138 struct cpuid_info *cpi;
3140 ASSERT(cpuid_checkpass(cpu, 1));
3141 cpi = cpu->cpu_m.mcpu_cpi;
3143 /* Assume that socket types are the same across the system */
3144 if (socketstr == NULL)
3145 socketstr = _cpuid_sktstr(cpi->cpi_vendor, cpi->cpi_family,
3146 cpi->cpi_model, cpi->cpi_step);
3149 return (socketstr);
3153 cpuid_get_chipid(cpu_t *cpu)
3155 ASSERT(cpuid_checkpass(cpu, 1));
3157 if (cpuid_is_cmt(cpu))
3158 return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
3159 return (cpu->cpu_id);
3162 id_t
3163 cpuid_get_coreid(cpu_t *cpu)
3165 ASSERT(cpuid_checkpass(cpu, 1));
3166 return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
3170 cpuid_get_pkgcoreid(cpu_t *cpu)
3172 ASSERT(cpuid_checkpass(cpu, 1));
3173 return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
3177 cpuid_get_clogid(cpu_t *cpu)
3179 ASSERT(cpuid_checkpass(cpu, 1));
3180 return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
3184 cpuid_get_cacheid(cpu_t *cpu)
3186 ASSERT(cpuid_checkpass(cpu, 1));
3187 return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
3190 uint_t
3191 cpuid_get_procnodeid(cpu_t *cpu)
3193 ASSERT(cpuid_checkpass(cpu, 1));
3194 return (cpu->cpu_m.mcpu_cpi->cpi_procnodeid);
3197 uint_t
3198 cpuid_get_procnodes_per_pkg(cpu_t *cpu)
3200 ASSERT(cpuid_checkpass(cpu, 1));
3201 return (cpu->cpu_m.mcpu_cpi->cpi_procnodes_per_pkg);
3204 uint_t
3205 cpuid_get_compunitid(cpu_t *cpu)
3207 ASSERT(cpuid_checkpass(cpu, 1));
3208 return (cpu->cpu_m.mcpu_cpi->cpi_compunitid);
3211 uint_t
3212 cpuid_get_cores_per_compunit(cpu_t *cpu)
3214 ASSERT(cpuid_checkpass(cpu, 1));
3215 return (cpu->cpu_m.mcpu_cpi->cpi_cores_per_compunit);
3218 /*ARGSUSED*/
3220 cpuid_have_cr8access(cpu_t *cpu)
3222 #if defined(__amd64)
3223 return (1);
3224 #else
3225 struct cpuid_info *cpi;
3227 ASSERT(cpu != NULL);
3228 cpi = cpu->cpu_m.mcpu_cpi;
3229 if (cpi->cpi_vendor == X86_VENDOR_AMD && cpi->cpi_maxeax >= 1 &&
3230 (CPI_FEATURES_XTD_ECX(cpi) & CPUID_AMD_ECX_CR8D) != 0)
3231 return (1);
3232 return (0);
3233 #endif
3236 uint32_t
3237 cpuid_get_apicid(cpu_t *cpu)
3239 ASSERT(cpuid_checkpass(cpu, 1));
3240 if (cpu->cpu_m.mcpu_cpi->cpi_maxeax < 1) {
3241 return (UINT32_MAX);
3242 } else {
3243 return (cpu->cpu_m.mcpu_cpi->cpi_apicid);
3247 void
3248 cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
3250 struct cpuid_info *cpi;
3252 if (cpu == NULL)
3253 cpu = CPU;
3254 cpi = cpu->cpu_m.mcpu_cpi;
3256 ASSERT(cpuid_checkpass(cpu, 1));
3258 if (pabits)
3259 *pabits = cpi->cpi_pabits;
3260 if (vabits)
3261 *vabits = cpi->cpi_vabits;
3265 * Returns the number of data TLB entries for a corresponding
3266 * pagesize. If it can't be computed, or isn't known, the
3267 * routine returns zero. If you ask about an architecturally
3268 * impossible pagesize, the routine will panic (so that the
3269 * hat implementor knows that things are inconsistent.)
3271 uint_t
3272 cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
3274 struct cpuid_info *cpi;
3275 uint_t dtlb_nent = 0;
3277 if (cpu == NULL)
3278 cpu = CPU;
3279 cpi = cpu->cpu_m.mcpu_cpi;
3281 ASSERT(cpuid_checkpass(cpu, 1));
3284 * Check the L2 TLB info
3286 if (cpi->cpi_xmaxeax >= 0x80000006) {
3287 struct cpuid_regs *cp = &cpi->cpi_extd[6];
3289 switch (pagesize) {
3291 case 4 * 1024:
3293 * All zero in the top 16 bits of the register
3294 * indicates a unified TLB. Size is in low 16 bits.
3296 if ((cp->cp_ebx & 0xffff0000) == 0)
3297 dtlb_nent = cp->cp_ebx & 0x0000ffff;
3298 else
3299 dtlb_nent = BITX(cp->cp_ebx, 27, 16);
3300 break;
3302 case 2 * 1024 * 1024:
3303 if ((cp->cp_eax & 0xffff0000) == 0)
3304 dtlb_nent = cp->cp_eax & 0x0000ffff;
3305 else
3306 dtlb_nent = BITX(cp->cp_eax, 27, 16);
3307 break;
3309 default:
3310 panic("unknown L2 pagesize");
3311 /*NOTREACHED*/
3315 if (dtlb_nent != 0)
3316 return (dtlb_nent);
3319 * No L2 TLB support for this size, try L1.
3321 if (cpi->cpi_xmaxeax >= 0x80000005) {
3322 struct cpuid_regs *cp = &cpi->cpi_extd[5];
3324 switch (pagesize) {
3325 case 4 * 1024:
3326 dtlb_nent = BITX(cp->cp_ebx, 23, 16);
3327 break;
3328 case 2 * 1024 * 1024:
3329 dtlb_nent = BITX(cp->cp_eax, 23, 16);
3330 break;
3331 default:
3332 panic("unknown L1 d-TLB pagesize");
3333 /*NOTREACHED*/
3337 return (dtlb_nent);
3341 * Return 0 if the erratum is not present or not applicable, positive
3342 * if it is, and negative if the status of the erratum is unknown.
3344 * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
3345 * Processors" #25759, Rev 3.57, August 2005
3348 cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
3350 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3351 uint_t eax;
3354 * Bail out if this CPU isn't an AMD CPU, or if it's
3355 * a legacy (32-bit) AMD CPU.
3357 if (cpi->cpi_vendor != X86_VENDOR_AMD ||
3358 cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
3359 cpi->cpi_family == 6)
3361 return (0);
3363 eax = cpi->cpi_std[1].cp_eax;
3365 #define SH_B0(eax) (eax == 0xf40 || eax == 0xf50)
3366 #define SH_B3(eax) (eax == 0xf51)
3367 #define B(eax) (SH_B0(eax) || SH_B3(eax))
3369 #define SH_C0(eax) (eax == 0xf48 || eax == 0xf58)
3371 #define SH_CG(eax) (eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
3372 #define DH_CG(eax) (eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
3373 #define CH_CG(eax) (eax == 0xf82 || eax == 0xfb2)
3374 #define CG(eax) (SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
3376 #define SH_D0(eax) (eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
3377 #define DH_D0(eax) (eax == 0x10fc0 || eax == 0x10ff0)
3378 #define CH_D0(eax) (eax == 0x10f80 || eax == 0x10fb0)
3379 #define D0(eax) (SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
3381 #define SH_E0(eax) (eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
3382 #define JH_E1(eax) (eax == 0x20f10) /* JH8_E0 had 0x20f30 */
3383 #define DH_E3(eax) (eax == 0x20fc0 || eax == 0x20ff0)
3384 #define SH_E4(eax) (eax == 0x20f51 || eax == 0x20f71)
3385 #define BH_E4(eax) (eax == 0x20fb1)
3386 #define SH_E5(eax) (eax == 0x20f42)
3387 #define DH_E6(eax) (eax == 0x20ff2 || eax == 0x20fc2)
3388 #define JH_E6(eax) (eax == 0x20f12 || eax == 0x20f32)
3389 #define EX(eax) (SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
3390 SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
3391 DH_E6(eax) || JH_E6(eax))
3393 #define DR_AX(eax) (eax == 0x100f00 || eax == 0x100f01 || eax == 0x100f02)
3394 #define DR_B0(eax) (eax == 0x100f20)
3395 #define DR_B1(eax) (eax == 0x100f21)
3396 #define DR_BA(eax) (eax == 0x100f2a)
3397 #define DR_B2(eax) (eax == 0x100f22)
3398 #define DR_B3(eax) (eax == 0x100f23)
3399 #define RB_C0(eax) (eax == 0x100f40)
3401 switch (erratum) {
3402 case 1:
3403 return (cpi->cpi_family < 0x10);
3404 case 51: /* what does the asterisk mean? */
3405 return (B(eax) || SH_C0(eax) || CG(eax));
3406 case 52:
3407 return (B(eax));
3408 case 57:
3409 return (cpi->cpi_family <= 0x11);
3410 case 58:
3411 return (B(eax));
3412 case 60:
3413 return (cpi->cpi_family <= 0x11);
3414 case 61:
3415 case 62:
3416 case 63:
3417 case 64:
3418 case 65:
3419 case 66:
3420 case 68:
3421 case 69:
3422 case 70:
3423 case 71:
3424 return (B(eax));
3425 case 72:
3426 return (SH_B0(eax));
3427 case 74:
3428 return (B(eax));
3429 case 75:
3430 return (cpi->cpi_family < 0x10);
3431 case 76:
3432 return (B(eax));
3433 case 77:
3434 return (cpi->cpi_family <= 0x11);
3435 case 78:
3436 return (B(eax) || SH_C0(eax));
3437 case 79:
3438 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
3439 case 80:
3440 case 81:
3441 case 82:
3442 return (B(eax));
3443 case 83:
3444 return (B(eax) || SH_C0(eax) || CG(eax));
3445 case 85:
3446 return (cpi->cpi_family < 0x10);
3447 case 86:
3448 return (SH_C0(eax) || CG(eax));
3449 case 88:
3450 #if !defined(__amd64)
3451 return (0);
3452 #else
3453 return (B(eax) || SH_C0(eax));
3454 #endif
3455 case 89:
3456 return (cpi->cpi_family < 0x10);
3457 case 90:
3458 return (B(eax) || SH_C0(eax) || CG(eax));
3459 case 91:
3460 case 92:
3461 return (B(eax) || SH_C0(eax));
3462 case 93:
3463 return (SH_C0(eax));
3464 case 94:
3465 return (B(eax) || SH_C0(eax) || CG(eax));
3466 case 95:
3467 #if !defined(__amd64)
3468 return (0);
3469 #else
3470 return (B(eax) || SH_C0(eax));
3471 #endif
3472 case 96:
3473 return (B(eax) || SH_C0(eax) || CG(eax));
3474 case 97:
3475 case 98:
3476 return (SH_C0(eax) || CG(eax));
3477 case 99:
3478 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3479 case 100:
3480 return (B(eax) || SH_C0(eax));
3481 case 101:
3482 case 103:
3483 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3484 case 104:
3485 return (SH_C0(eax) || CG(eax) || D0(eax));
3486 case 105:
3487 case 106:
3488 case 107:
3489 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3490 case 108:
3491 return (DH_CG(eax));
3492 case 109:
3493 return (SH_C0(eax) || CG(eax) || D0(eax));
3494 case 110:
3495 return (D0(eax) || EX(eax));
3496 case 111:
3497 return (CG(eax));
3498 case 112:
3499 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
3500 case 113:
3501 return (eax == 0x20fc0);
3502 case 114:
3503 return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
3504 case 115:
3505 return (SH_E0(eax) || JH_E1(eax));
3506 case 116:
3507 return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
3508 case 117:
3509 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3510 case 118:
3511 return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
3512 JH_E6(eax));
3513 case 121:
3514 return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
3515 case 122:
3516 return (cpi->cpi_family < 0x10 || cpi->cpi_family == 0x11);
3517 case 123:
3518 return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
3519 case 131:
3520 return (cpi->cpi_family < 0x10);
3521 case 6336786:
3523 * Test for AdvPowerMgmtInfo.TscPStateInvariant
3524 * if this is a K8 family or newer processor
3526 if (CPI_FAMILY(cpi) == 0xf) {
3527 struct cpuid_regs regs;
3528 regs.cp_eax = 0x80000007;
3529 (void) __cpuid_insn(&regs);
3530 return (!(regs.cp_edx & 0x100));
3532 return (0);
3533 case 6323525:
3534 return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
3535 (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
3537 case 6671130:
3539 * check for processors (pre-Shanghai) that do not provide
3540 * optimal management of 1gb ptes in its tlb.
3542 return (cpi->cpi_family == 0x10 && cpi->cpi_model < 4);
3544 case 298:
3545 return (DR_AX(eax) || DR_B0(eax) || DR_B1(eax) || DR_BA(eax) ||
3546 DR_B2(eax) || RB_C0(eax));
3548 case 721:
3549 #if defined(__amd64)
3550 return (cpi->cpi_family == 0x10 || cpi->cpi_family == 0x12);
3551 #else
3552 return (0);
3553 #endif
3555 default:
3556 return (-1);
3562 * Determine if specified erratum is present via OSVW (OS Visible Workaround).
3563 * Return 1 if erratum is present, 0 if not present and -1 if indeterminate.
3566 osvw_opteron_erratum(cpu_t *cpu, uint_t erratum)
3568 struct cpuid_info *cpi;
3569 uint_t osvwid;
3570 static int osvwfeature = -1;
3571 uint64_t osvwlength;
3574 cpi = cpu->cpu_m.mcpu_cpi;
3576 /* confirm OSVW supported */
3577 if (osvwfeature == -1) {
3578 osvwfeature = cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW;
3579 } else {
3580 /* assert that osvw feature setting is consistent on all cpus */
3581 ASSERT(osvwfeature ==
3582 (cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW));
3584 if (!osvwfeature)
3585 return (-1);
3587 osvwlength = rdmsr(MSR_AMD_OSVW_ID_LEN) & OSVW_ID_LEN_MASK;
3589 switch (erratum) {
3590 case 298: /* osvwid is 0 */
3591 osvwid = 0;
3592 if (osvwlength <= (uint64_t)osvwid) {
3593 /* osvwid 0 is unknown */
3594 return (-1);
3598 * Check the OSVW STATUS MSR to determine the state
3599 * of the erratum where:
3600 * 0 - fixed by HW
3601 * 1 - BIOS has applied the workaround when BIOS
3602 * workaround is available. (Or for other errata,
3603 * OS workaround is required.)
3604 * For a value of 1, caller will confirm that the
3605 * erratum 298 workaround has indeed been applied by BIOS.
3607 * A 1 may be set in cpus that have a HW fix
3608 * in a mixed cpu system. Regarding erratum 298:
3609 * In a multiprocessor platform, the workaround above
3610 * should be applied to all processors regardless of
3611 * silicon revision when an affected processor is
3612 * present.
3615 return (rdmsr(MSR_AMD_OSVW_STATUS +
3616 (osvwid / OSVW_ID_CNT_PER_MSR)) &
3617 (1ULL << (osvwid % OSVW_ID_CNT_PER_MSR)));
3619 default:
3620 return (-1);
3624 static const char assoc_str[] = "associativity";
3625 static const char line_str[] = "line-size";
3626 static const char size_str[] = "size";
3628 static void
3629 add_cache_prop(dev_info_t *devi, const char *label, const char *type,
3630 uint32_t val)
3632 char buf[128];
3635 * ndi_prop_update_int() is used because it is desirable for
3636 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
3638 if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
3639 (void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
3643 * Intel-style cache/tlb description
3645 * Standard cpuid level 2 gives a randomly ordered
3646 * selection of tags that index into a table that describes
3647 * cache and tlb properties.
3650 static const char l1_icache_str[] = "l1-icache";
3651 static const char l1_dcache_str[] = "l1-dcache";
3652 static const char l2_cache_str[] = "l2-cache";
3653 static const char l3_cache_str[] = "l3-cache";
3654 static const char itlb4k_str[] = "itlb-4K";
3655 static const char dtlb4k_str[] = "dtlb-4K";
3656 static const char itlb2M_str[] = "itlb-2M";
3657 static const char itlb4M_str[] = "itlb-4M";
3658 static const char dtlb4M_str[] = "dtlb-4M";
3659 static const char dtlb24_str[] = "dtlb0-2M-4M";
3660 static const char itlb424_str[] = "itlb-4K-2M-4M";
3661 static const char itlb24_str[] = "itlb-2M-4M";
3662 static const char dtlb44_str[] = "dtlb-4K-4M";
3663 static const char sl1_dcache_str[] = "sectored-l1-dcache";
3664 static const char sl2_cache_str[] = "sectored-l2-cache";
3665 static const char itrace_str[] = "itrace-cache";
3666 static const char sl3_cache_str[] = "sectored-l3-cache";
3667 static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
3669 static const struct cachetab {
3670 uint8_t ct_code;
3671 uint8_t ct_assoc;
3672 uint16_t ct_line_size;
3673 size_t ct_size;
3674 const char *ct_label;
3675 } intel_ctab[] = {
3677 * maintain descending order!
3679 * Codes ignored - Reason
3680 * ----------------------
3681 * 40H - intel_cpuid_4_cache_info() disambiguates l2/l3 cache
3682 * f0H/f1H - Currently we do not interpret prefetch size by design
3684 { 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
3685 { 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
3686 { 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
3687 { 0xde, 12, 64, 6*1024*1024, l3_cache_str},
3688 { 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
3689 { 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
3690 { 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
3691 { 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
3692 { 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
3693 { 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
3694 { 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
3695 { 0xd0, 4, 64, 512*1024, l3_cache_str},
3696 { 0xca, 4, 0, 512, sh_l2_tlb4k_str},
3697 { 0xc0, 4, 0, 8, dtlb44_str },
3698 { 0xba, 4, 0, 64, dtlb4k_str },
3699 { 0xb4, 4, 0, 256, dtlb4k_str },
3700 { 0xb3, 4, 0, 128, dtlb4k_str },
3701 { 0xb2, 4, 0, 64, itlb4k_str },
3702 { 0xb0, 4, 0, 128, itlb4k_str },
3703 { 0x87, 8, 64, 1024*1024, l2_cache_str},
3704 { 0x86, 4, 64, 512*1024, l2_cache_str},
3705 { 0x85, 8, 32, 2*1024*1024, l2_cache_str},
3706 { 0x84, 8, 32, 1024*1024, l2_cache_str},
3707 { 0x83, 8, 32, 512*1024, l2_cache_str},
3708 { 0x82, 8, 32, 256*1024, l2_cache_str},
3709 { 0x80, 8, 64, 512*1024, l2_cache_str},
3710 { 0x7f, 2, 64, 512*1024, l2_cache_str},
3711 { 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
3712 { 0x7c, 8, 64, 1024*1024, sl2_cache_str},
3713 { 0x7b, 8, 64, 512*1024, sl2_cache_str},
3714 { 0x7a, 8, 64, 256*1024, sl2_cache_str},
3715 { 0x79, 8, 64, 128*1024, sl2_cache_str},
3716 { 0x78, 8, 64, 1024*1024, l2_cache_str},
3717 { 0x73, 8, 0, 64*1024, itrace_str},
3718 { 0x72, 8, 0, 32*1024, itrace_str},
3719 { 0x71, 8, 0, 16*1024, itrace_str},
3720 { 0x70, 8, 0, 12*1024, itrace_str},
3721 { 0x68, 4, 64, 32*1024, sl1_dcache_str},
3722 { 0x67, 4, 64, 16*1024, sl1_dcache_str},
3723 { 0x66, 4, 64, 8*1024, sl1_dcache_str},
3724 { 0x60, 8, 64, 16*1024, sl1_dcache_str},
3725 { 0x5d, 0, 0, 256, dtlb44_str},
3726 { 0x5c, 0, 0, 128, dtlb44_str},
3727 { 0x5b, 0, 0, 64, dtlb44_str},
3728 { 0x5a, 4, 0, 32, dtlb24_str},
3729 { 0x59, 0, 0, 16, dtlb4k_str},
3730 { 0x57, 4, 0, 16, dtlb4k_str},
3731 { 0x56, 4, 0, 16, dtlb4M_str},
3732 { 0x55, 0, 0, 7, itlb24_str},
3733 { 0x52, 0, 0, 256, itlb424_str},
3734 { 0x51, 0, 0, 128, itlb424_str},
3735 { 0x50, 0, 0, 64, itlb424_str},
3736 { 0x4f, 0, 0, 32, itlb4k_str},
3737 { 0x4e, 24, 64, 6*1024*1024, l2_cache_str},
3738 { 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
3739 { 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
3740 { 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
3741 { 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
3742 { 0x49, 16, 64, 4*1024*1024, l3_cache_str},
3743 { 0x48, 12, 64, 3*1024*1024, l2_cache_str},
3744 { 0x47, 8, 64, 8*1024*1024, l3_cache_str},
3745 { 0x46, 4, 64, 4*1024*1024, l3_cache_str},
3746 { 0x45, 4, 32, 2*1024*1024, l2_cache_str},
3747 { 0x44, 4, 32, 1024*1024, l2_cache_str},
3748 { 0x43, 4, 32, 512*1024, l2_cache_str},
3749 { 0x42, 4, 32, 256*1024, l2_cache_str},
3750 { 0x41, 4, 32, 128*1024, l2_cache_str},
3751 { 0x3e, 4, 64, 512*1024, sl2_cache_str},
3752 { 0x3d, 6, 64, 384*1024, sl2_cache_str},
3753 { 0x3c, 4, 64, 256*1024, sl2_cache_str},
3754 { 0x3b, 2, 64, 128*1024, sl2_cache_str},
3755 { 0x3a, 6, 64, 192*1024, sl2_cache_str},
3756 { 0x39, 4, 64, 128*1024, sl2_cache_str},
3757 { 0x30, 8, 64, 32*1024, l1_icache_str},
3758 { 0x2c, 8, 64, 32*1024, l1_dcache_str},
3759 { 0x29, 8, 64, 4096*1024, sl3_cache_str},
3760 { 0x25, 8, 64, 2048*1024, sl3_cache_str},
3761 { 0x23, 8, 64, 1024*1024, sl3_cache_str},
3762 { 0x22, 4, 64, 512*1024, sl3_cache_str},
3763 { 0x0e, 6, 64, 24*1024, l1_dcache_str},
3764 { 0x0d, 4, 32, 16*1024, l1_dcache_str},
3765 { 0x0c, 4, 32, 16*1024, l1_dcache_str},
3766 { 0x0b, 4, 0, 4, itlb4M_str},
3767 { 0x0a, 2, 32, 8*1024, l1_dcache_str},
3768 { 0x08, 4, 32, 16*1024, l1_icache_str},
3769 { 0x06, 4, 32, 8*1024, l1_icache_str},
3770 { 0x05, 4, 0, 32, dtlb4M_str},
3771 { 0x04, 4, 0, 8, dtlb4M_str},
3772 { 0x03, 4, 0, 64, dtlb4k_str},
3773 { 0x02, 4, 0, 2, itlb4M_str},
3774 { 0x01, 4, 0, 32, itlb4k_str},
3775 { 0 }
3778 static const struct cachetab cyrix_ctab[] = {
3779 { 0x70, 4, 0, 32, "tlb-4K" },
3780 { 0x80, 4, 16, 16*1024, "l1-cache" },
3781 { 0 }
3785 * Search a cache table for a matching entry
3787 static const struct cachetab *
3788 find_cacheent(const struct cachetab *ct, uint_t code)
3790 if (code != 0) {
3791 for (; ct->ct_code != 0; ct++)
3792 if (ct->ct_code <= code)
3793 break;
3794 if (ct->ct_code == code)
3795 return (ct);
3797 return (NULL);
3801 * Populate cachetab entry with L2 or L3 cache-information using
3802 * cpuid function 4. This function is called from intel_walk_cacheinfo()
3803 * when descriptor 0x49 is encountered. It returns 0 if no such cache
3804 * information is found.
3806 static int
3807 intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
3809 uint32_t level, i;
3810 int ret = 0;
3812 for (i = 0; i < cpi->cpi_std_4_size; i++) {
3813 level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
3815 if (level == 2 || level == 3) {
3816 ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
3817 ct->ct_line_size =
3818 CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
3819 ct->ct_size = ct->ct_assoc *
3820 (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
3821 ct->ct_line_size *
3822 (cpi->cpi_std_4[i]->cp_ecx + 1);
3824 if (level == 2) {
3825 ct->ct_label = l2_cache_str;
3826 } else if (level == 3) {
3827 ct->ct_label = l3_cache_str;
3829 ret = 1;
3833 return (ret);
3837 * Walk the cacheinfo descriptor, applying 'func' to every valid element
3838 * The walk is terminated if the walker returns non-zero.
3840 static void
3841 intel_walk_cacheinfo(struct cpuid_info *cpi,
3842 void *arg, int (*func)(void *, const struct cachetab *))
3844 const struct cachetab *ct;
3845 struct cachetab des_49_ct, des_b1_ct;
3846 uint8_t *dp;
3847 int i;
3849 if ((dp = cpi->cpi_cacheinfo) == NULL)
3850 return;
3851 for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3853 * For overloaded descriptor 0x49 we use cpuid function 4
3854 * if supported by the current processor, to create
3855 * cache information.
3856 * For overloaded descriptor 0xb1 we use X86_PAE flag
3857 * to disambiguate the cache information.
3859 if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
3860 intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
3861 ct = &des_49_ct;
3862 } else if (*dp == 0xb1) {
3863 des_b1_ct.ct_code = 0xb1;
3864 des_b1_ct.ct_assoc = 4;
3865 des_b1_ct.ct_line_size = 0;
3866 if (is_x86_feature(x86_featureset, X86FSET_PAE)) {
3867 des_b1_ct.ct_size = 8;
3868 des_b1_ct.ct_label = itlb2M_str;
3869 } else {
3870 des_b1_ct.ct_size = 4;
3871 des_b1_ct.ct_label = itlb4M_str;
3873 ct = &des_b1_ct;
3874 } else {
3875 if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
3876 continue;
3880 if (func(arg, ct) != 0) {
3881 break;
3887 * (Like the Intel one, except for Cyrix CPUs)
3889 static void
3890 cyrix_walk_cacheinfo(struct cpuid_info *cpi,
3891 void *arg, int (*func)(void *, const struct cachetab *))
3893 const struct cachetab *ct;
3894 uint8_t *dp;
3895 int i;
3897 if ((dp = cpi->cpi_cacheinfo) == NULL)
3898 return;
3899 for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3901 * Search Cyrix-specific descriptor table first ..
3903 if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
3904 if (func(arg, ct) != 0)
3905 break;
3906 continue;
3909 * .. else fall back to the Intel one
3911 if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
3912 if (func(arg, ct) != 0)
3913 break;
3914 continue;
3920 * A cacheinfo walker that adds associativity, line-size, and size properties
3921 * to the devinfo node it is passed as an argument.
3923 static int
3924 add_cacheent_props(void *arg, const struct cachetab *ct)
3926 dev_info_t *devi = arg;
3928 add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
3929 if (ct->ct_line_size != 0)
3930 add_cache_prop(devi, ct->ct_label, line_str,
3931 ct->ct_line_size);
3932 add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
3933 return (0);
3937 static const char fully_assoc[] = "fully-associative?";
3940 * AMD style cache/tlb description
3942 * Extended functions 5 and 6 directly describe properties of
3943 * tlbs and various cache levels.
3945 static void
3946 add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3948 switch (assoc) {
3949 case 0: /* reserved; ignore */
3950 break;
3951 default:
3952 add_cache_prop(devi, label, assoc_str, assoc);
3953 break;
3954 case 0xff:
3955 add_cache_prop(devi, label, fully_assoc, 1);
3956 break;
3960 static void
3961 add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
3963 if (size == 0)
3964 return;
3965 add_cache_prop(devi, label, size_str, size);
3966 add_amd_assoc(devi, label, assoc);
3969 static void
3970 add_amd_cache(dev_info_t *devi, const char *label,
3971 uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
3973 if (size == 0 || line_size == 0)
3974 return;
3975 add_amd_assoc(devi, label, assoc);
3977 * Most AMD parts have a sectored cache. Multiple cache lines are
3978 * associated with each tag. A sector consists of all cache lines
3979 * associated with a tag. For example, the AMD K6-III has a sector
3980 * size of 2 cache lines per tag.
3982 if (lines_per_tag != 0)
3983 add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
3984 add_cache_prop(devi, label, line_str, line_size);
3985 add_cache_prop(devi, label, size_str, size * 1024);
3988 static void
3989 add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3991 switch (assoc) {
3992 case 0: /* off */
3993 break;
3994 case 1:
3995 case 2:
3996 case 4:
3997 add_cache_prop(devi, label, assoc_str, assoc);
3998 break;
3999 case 6:
4000 add_cache_prop(devi, label, assoc_str, 8);
4001 break;
4002 case 8:
4003 add_cache_prop(devi, label, assoc_str, 16);
4004 break;
4005 case 0xf:
4006 add_cache_prop(devi, label, fully_assoc, 1);
4007 break;
4008 default: /* reserved; ignore */
4009 break;
4013 static void
4014 add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
4016 if (size == 0 || assoc == 0)
4017 return;
4018 add_amd_l2_assoc(devi, label, assoc);
4019 add_cache_prop(devi, label, size_str, size);
4022 static void
4023 add_amd_l2_cache(dev_info_t *devi, const char *label,
4024 uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
4026 if (size == 0 || assoc == 0 || line_size == 0)
4027 return;
4028 add_amd_l2_assoc(devi, label, assoc);
4029 if (lines_per_tag != 0)
4030 add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
4031 add_cache_prop(devi, label, line_str, line_size);
4032 add_cache_prop(devi, label, size_str, size * 1024);
4035 static void
4036 amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
4038 struct cpuid_regs *cp;
4040 if (cpi->cpi_xmaxeax < 0x80000005)
4041 return;
4042 cp = &cpi->cpi_extd[5];
4045 * 4M/2M L1 TLB configuration
4047 * We report the size for 2M pages because AMD uses two
4048 * TLB entries for one 4M page.
4050 add_amd_tlb(devi, "dtlb-2M",
4051 BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
4052 add_amd_tlb(devi, "itlb-2M",
4053 BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
4056 * 4K L1 TLB configuration
4059 switch (cpi->cpi_vendor) {
4060 uint_t nentries;
4061 case X86_VENDOR_TM:
4062 if (cpi->cpi_family >= 5) {
4064 * Crusoe processors have 256 TLB entries, but
4065 * cpuid data format constrains them to only
4066 * reporting 255 of them.
4068 if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
4069 nentries = 256;
4071 * Crusoe processors also have a unified TLB
4073 add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
4074 nentries);
4075 break;
4077 /*FALLTHROUGH*/
4078 default:
4079 add_amd_tlb(devi, itlb4k_str,
4080 BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
4081 add_amd_tlb(devi, dtlb4k_str,
4082 BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
4083 break;
4087 * data L1 cache configuration
4090 add_amd_cache(devi, l1_dcache_str,
4091 BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
4092 BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
4095 * code L1 cache configuration
4098 add_amd_cache(devi, l1_icache_str,
4099 BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
4100 BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
4102 if (cpi->cpi_xmaxeax < 0x80000006)
4103 return;
4104 cp = &cpi->cpi_extd[6];
4106 /* Check for a unified L2 TLB for large pages */
4108 if (BITX(cp->cp_eax, 31, 16) == 0)
4109 add_amd_l2_tlb(devi, "l2-tlb-2M",
4110 BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4111 else {
4112 add_amd_l2_tlb(devi, "l2-dtlb-2M",
4113 BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
4114 add_amd_l2_tlb(devi, "l2-itlb-2M",
4115 BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4118 /* Check for a unified L2 TLB for 4K pages */
4120 if (BITX(cp->cp_ebx, 31, 16) == 0) {
4121 add_amd_l2_tlb(devi, "l2-tlb-4K",
4122 BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4123 } else {
4124 add_amd_l2_tlb(devi, "l2-dtlb-4K",
4125 BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
4126 add_amd_l2_tlb(devi, "l2-itlb-4K",
4127 BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4130 add_amd_l2_cache(devi, l2_cache_str,
4131 BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
4132 BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
4136 * There are two basic ways that the x86 world describes it cache
4137 * and tlb architecture - Intel's way and AMD's way.
4139 * Return which flavor of cache architecture we should use
4141 static int
4142 x86_which_cacheinfo(struct cpuid_info *cpi)
4144 switch (cpi->cpi_vendor) {
4145 case X86_VENDOR_Intel:
4146 if (cpi->cpi_maxeax >= 2)
4147 return (X86_VENDOR_Intel);
4148 break;
4149 case X86_VENDOR_AMD:
4151 * The K5 model 1 was the first part from AMD that reported
4152 * cache sizes via extended cpuid functions.
4154 if (cpi->cpi_family > 5 ||
4155 (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
4156 return (X86_VENDOR_AMD);
4157 break;
4158 case X86_VENDOR_TM:
4159 if (cpi->cpi_family >= 5)
4160 return (X86_VENDOR_AMD);
4161 /*FALLTHROUGH*/
4162 default:
4164 * If they have extended CPU data for 0x80000005
4165 * then we assume they have AMD-format cache
4166 * information.
4168 * If not, and the vendor happens to be Cyrix,
4169 * then try our-Cyrix specific handler.
4171 * If we're not Cyrix, then assume we're using Intel's
4172 * table-driven format instead.
4174 if (cpi->cpi_xmaxeax >= 0x80000005)
4175 return (X86_VENDOR_AMD);
4176 else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
4177 return (X86_VENDOR_Cyrix);
4178 else if (cpi->cpi_maxeax >= 2)
4179 return (X86_VENDOR_Intel);
4180 break;
4182 return (-1);
4185 void
4186 cpuid_set_cpu_properties(void *dip, processorid_t cpu_id,
4187 struct cpuid_info *cpi)
4189 dev_info_t *cpu_devi;
4190 int create;
4192 cpu_devi = (dev_info_t *)dip;
4194 /* device_type */
4195 (void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
4196 "device_type", "cpu");
4198 /* reg */
4199 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4200 "reg", cpu_id);
4202 /* cpu-mhz, and clock-frequency */
4203 if (cpu_freq > 0) {
4204 long long mul;
4206 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4207 "cpu-mhz", cpu_freq);
4208 if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
4209 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4210 "clock-frequency", (int)mul);
4213 if (!is_x86_feature(x86_featureset, X86FSET_CPUID)) {
4214 return;
4217 /* vendor-id */
4218 (void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
4219 "vendor-id", cpi->cpi_vendorstr);
4221 if (cpi->cpi_maxeax == 0) {
4222 return;
4226 * family, model, and step
4228 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4229 "family", CPI_FAMILY(cpi));
4230 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4231 "cpu-model", CPI_MODEL(cpi));
4232 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4233 "stepping-id", CPI_STEP(cpi));
4235 /* type */
4236 switch (cpi->cpi_vendor) {
4237 case X86_VENDOR_Intel:
4238 create = 1;
4239 break;
4240 default:
4241 create = 0;
4242 break;
4244 if (create)
4245 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4246 "type", CPI_TYPE(cpi));
4248 /* ext-family */
4249 switch (cpi->cpi_vendor) {
4250 case X86_VENDOR_Intel:
4251 case X86_VENDOR_AMD:
4252 create = cpi->cpi_family >= 0xf;
4253 break;
4254 default:
4255 create = 0;
4256 break;
4258 if (create)
4259 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4260 "ext-family", CPI_FAMILY_XTD(cpi));
4262 /* ext-model */
4263 switch (cpi->cpi_vendor) {
4264 case X86_VENDOR_Intel:
4265 create = IS_EXTENDED_MODEL_INTEL(cpi);
4266 break;
4267 case X86_VENDOR_AMD:
4268 create = CPI_FAMILY(cpi) == 0xf;
4269 break;
4270 default:
4271 create = 0;
4272 break;
4274 if (create)
4275 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4276 "ext-model", CPI_MODEL_XTD(cpi));
4278 /* generation */
4279 switch (cpi->cpi_vendor) {
4280 case X86_VENDOR_AMD:
4282 * AMD K5 model 1 was the first part to support this
4284 create = cpi->cpi_xmaxeax >= 0x80000001;
4285 break;
4286 default:
4287 create = 0;
4288 break;
4290 if (create)
4291 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4292 "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
4294 /* brand-id */
4295 switch (cpi->cpi_vendor) {
4296 case X86_VENDOR_Intel:
4298 * brand id first appeared on Pentium III Xeon model 8,
4299 * and Celeron model 8 processors and Opteron
4301 create = cpi->cpi_family > 6 ||
4302 (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
4303 break;
4304 case X86_VENDOR_AMD:
4305 create = cpi->cpi_family >= 0xf;
4306 break;
4307 default:
4308 create = 0;
4309 break;
4311 if (create && cpi->cpi_brandid != 0) {
4312 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4313 "brand-id", cpi->cpi_brandid);
4316 /* chunks, and apic-id */
4317 switch (cpi->cpi_vendor) {
4319 * first available on Pentium IV and Opteron (K8)
4321 case X86_VENDOR_Intel:
4322 create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
4323 break;
4324 case X86_VENDOR_AMD:
4325 create = cpi->cpi_family >= 0xf;
4326 break;
4327 default:
4328 create = 0;
4329 break;
4331 if (create) {
4332 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4333 "chunks", CPI_CHUNKS(cpi));
4334 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4335 "apic-id", cpi->cpi_apicid);
4336 if (cpi->cpi_chipid >= 0) {
4337 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4338 "chip#", cpi->cpi_chipid);
4339 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4340 "clog#", cpi->cpi_clogid);
4344 /* cpuid-features */
4345 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4346 "cpuid-features", CPI_FEATURES_EDX(cpi));
4349 /* cpuid-features-ecx */
4350 switch (cpi->cpi_vendor) {
4351 case X86_VENDOR_Intel:
4352 create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
4353 break;
4354 case X86_VENDOR_AMD:
4355 create = cpi->cpi_family >= 0xf;
4356 break;
4357 default:
4358 create = 0;
4359 break;
4361 if (create)
4362 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4363 "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
4365 /* ext-cpuid-features */
4366 switch (cpi->cpi_vendor) {
4367 case X86_VENDOR_Intel:
4368 case X86_VENDOR_AMD:
4369 case X86_VENDOR_Cyrix:
4370 case X86_VENDOR_TM:
4371 case X86_VENDOR_Centaur:
4372 create = cpi->cpi_xmaxeax >= 0x80000001;
4373 break;
4374 default:
4375 create = 0;
4376 break;
4378 if (create) {
4379 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4380 "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
4381 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4382 "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
4386 * Brand String first appeared in Intel Pentium IV, AMD K5
4387 * model 1, and Cyrix GXm. On earlier models we try and
4388 * simulate something similar .. so this string should always
4389 * same -something- about the processor, however lame.
4391 (void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
4392 "brand-string", cpi->cpi_brandstr);
4395 * Finally, cache and tlb information
4397 switch (x86_which_cacheinfo(cpi)) {
4398 case X86_VENDOR_Intel:
4399 intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
4400 break;
4401 case X86_VENDOR_Cyrix:
4402 cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
4403 break;
4404 case X86_VENDOR_AMD:
4405 amd_cache_info(cpi, cpu_devi);
4406 break;
4407 default:
4408 break;
4412 struct l2info {
4413 int *l2i_csz;
4414 int *l2i_lsz;
4415 int *l2i_assoc;
4416 int l2i_ret;
4420 * A cacheinfo walker that fetches the size, line-size and associativity
4421 * of the L2 cache
4423 static int
4424 intel_l2cinfo(void *arg, const struct cachetab *ct)
4426 struct l2info *l2i = arg;
4427 int *ip;
4429 if (ct->ct_label != l2_cache_str &&
4430 ct->ct_label != sl2_cache_str)
4431 return (0); /* not an L2 -- keep walking */
4433 if ((ip = l2i->l2i_csz) != NULL)
4434 *ip = ct->ct_size;
4435 if ((ip = l2i->l2i_lsz) != NULL)
4436 *ip = ct->ct_line_size;
4437 if ((ip = l2i->l2i_assoc) != NULL)
4438 *ip = ct->ct_assoc;
4439 l2i->l2i_ret = ct->ct_size;
4440 return (1); /* was an L2 -- terminate walk */
4444 * AMD L2/L3 Cache and TLB Associativity Field Definition:
4446 * Unlike the associativity for the L1 cache and tlb where the 8 bit
4447 * value is the associativity, the associativity for the L2 cache and
4448 * tlb is encoded in the following table. The 4 bit L2 value serves as
4449 * an index into the amd_afd[] array to determine the associativity.
4450 * -1 is undefined. 0 is fully associative.
4453 static int amd_afd[] =
4454 {-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
4456 static void
4457 amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
4459 struct cpuid_regs *cp;
4460 uint_t size, assoc;
4461 int i;
4462 int *ip;
4464 if (cpi->cpi_xmaxeax < 0x80000006)
4465 return;
4466 cp = &cpi->cpi_extd[6];
4468 if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
4469 (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
4470 uint_t cachesz = size * 1024;
4471 assoc = amd_afd[i];
4473 ASSERT(assoc != -1);
4475 if ((ip = l2i->l2i_csz) != NULL)
4476 *ip = cachesz;
4477 if ((ip = l2i->l2i_lsz) != NULL)
4478 *ip = BITX(cp->cp_ecx, 7, 0);
4479 if ((ip = l2i->l2i_assoc) != NULL)
4480 *ip = assoc;
4481 l2i->l2i_ret = cachesz;
4486 getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
4488 struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
4489 struct l2info __l2info, *l2i = &__l2info;
4491 l2i->l2i_csz = csz;
4492 l2i->l2i_lsz = lsz;
4493 l2i->l2i_assoc = assoc;
4494 l2i->l2i_ret = -1;
4496 switch (x86_which_cacheinfo(cpi)) {
4497 case X86_VENDOR_Intel:
4498 intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
4499 break;
4500 case X86_VENDOR_Cyrix:
4501 cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
4502 break;
4503 case X86_VENDOR_AMD:
4504 amd_l2cacheinfo(cpi, l2i);
4505 break;
4506 default:
4507 break;
4509 return (l2i->l2i_ret);
4512 #if !defined(__xpv)
4514 uint32_t *
4515 cpuid_mwait_alloc(cpu_t *cpu)
4517 uint32_t *ret;
4518 size_t mwait_size;
4520 ASSERT(cpuid_checkpass(CPU, 2));
4522 mwait_size = CPU->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
4523 if (mwait_size == 0)
4524 return (NULL);
4527 * kmem_alloc() returns cache line size aligned data for mwait_size
4528 * allocations. mwait_size is currently cache line sized. Neither
4529 * of these implementation details are guarantied to be true in the
4530 * future.
4532 * First try allocating mwait_size as kmem_alloc() currently returns
4533 * correctly aligned memory. If kmem_alloc() does not return
4534 * mwait_size aligned memory, then use mwait_size ROUNDUP.
4536 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
4537 * decide to free this memory.
4539 ret = kmem_zalloc(mwait_size, KM_SLEEP);
4540 if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
4541 cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
4542 cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
4543 *ret = MWAIT_RUNNING;
4544 return (ret);
4545 } else {
4546 kmem_free(ret, mwait_size);
4547 ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
4548 cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
4549 cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
4550 ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
4551 *ret = MWAIT_RUNNING;
4552 return (ret);
4556 void
4557 cpuid_mwait_free(cpu_t *cpu)
4559 if (cpu->cpu_m.mcpu_cpi == NULL) {
4560 return;
4563 if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
4564 cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
4565 kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
4566 cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
4569 cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
4570 cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
4573 void
4574 patch_tsc_read(int flag)
4576 size_t cnt;
4578 switch (flag) {
4579 case X86_NO_TSC:
4580 cnt = &_no_rdtsc_end - &_no_rdtsc_start;
4581 (void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
4582 break;
4583 case X86_HAVE_TSCP:
4584 cnt = &_tscp_end - &_tscp_start;
4585 (void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
4586 break;
4587 case X86_TSC_MFENCE:
4588 cnt = &_tsc_mfence_end - &_tsc_mfence_start;
4589 (void) memcpy((void *)tsc_read,
4590 (void *)&_tsc_mfence_start, cnt);
4591 break;
4592 case X86_TSC_LFENCE:
4593 cnt = &_tsc_lfence_end - &_tsc_lfence_start;
4594 (void) memcpy((void *)tsc_read,
4595 (void *)&_tsc_lfence_start, cnt);
4596 break;
4597 default:
4598 break;
4603 cpuid_deep_cstates_supported(void)
4605 struct cpuid_info *cpi;
4606 struct cpuid_regs regs;
4608 ASSERT(cpuid_checkpass(CPU, 1));
4610 cpi = CPU->cpu_m.mcpu_cpi;
4612 if (!is_x86_feature(x86_featureset, X86FSET_CPUID))
4613 return (0);
4615 switch (cpi->cpi_vendor) {
4616 case X86_VENDOR_Intel:
4617 if (cpi->cpi_xmaxeax < 0x80000007)
4618 return (0);
4621 * TSC run at a constant rate in all ACPI C-states?
4623 regs.cp_eax = 0x80000007;
4624 (void) __cpuid_insn(&regs);
4625 return (regs.cp_edx & CPUID_TSC_CSTATE_INVARIANCE);
4627 default:
4628 return (0);
4632 #endif /* !__xpv */
4634 void
4635 post_startup_cpu_fixups(void)
4637 #ifndef __xpv
4639 * Some AMD processors support C1E state. Entering this state will
4640 * cause the local APIC timer to stop, which we can't deal with at
4641 * this time.
4643 if (cpuid_getvendor(CPU) == X86_VENDOR_AMD) {
4644 on_trap_data_t otd;
4645 uint64_t reg;
4647 if (!on_trap(&otd, OT_DATA_ACCESS)) {
4648 reg = rdmsr(MSR_AMD_INT_PENDING_CMP_HALT);
4649 /* Disable C1E state if it is enabled by BIOS */
4650 if ((reg >> AMD_ACTONCMPHALT_SHIFT) &
4651 AMD_ACTONCMPHALT_MASK) {
4652 reg &= ~(AMD_ACTONCMPHALT_MASK <<
4653 AMD_ACTONCMPHALT_SHIFT);
4654 wrmsr(MSR_AMD_INT_PENDING_CMP_HALT, reg);
4657 no_trap();
4659 #endif /* !__xpv */
4663 * Setup necessary registers to enable XSAVE feature on this processor.
4664 * This function needs to be called early enough, so that no xsave/xrstor
4665 * ops will execute on the processor before the MSRs are properly set up.
4667 * Current implementation has the following assumption:
4668 * - cpuid_pass1() is done, so that X86 features are known.
4669 * - fpu_probe() is done, so that fp_save_mech is chosen.
4671 void
4672 xsave_setup_msr(cpu_t *cpu)
4674 ASSERT(fp_save_mech == FP_XSAVE);
4675 ASSERT(is_x86_feature(x86_featureset, X86FSET_XSAVE));
4677 /* Enable OSXSAVE in CR4. */
4678 setcr4(getcr4() | CR4_OSXSAVE);
4680 * Update SW copy of ECX, so that /dev/cpu/self/cpuid will report
4681 * correct value.
4683 cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_ecx |= CPUID_INTC_ECX_OSXSAVE;
4684 setup_xfem();
4688 * Starting with the Westmere processor the local
4689 * APIC timer will continue running in all C-states,
4690 * including the deepest C-states.
4693 cpuid_arat_supported(void)
4695 struct cpuid_info *cpi;
4696 struct cpuid_regs regs;
4698 ASSERT(cpuid_checkpass(CPU, 1));
4699 ASSERT(is_x86_feature(x86_featureset, X86FSET_CPUID));
4701 cpi = CPU->cpu_m.mcpu_cpi;
4703 switch (cpi->cpi_vendor) {
4704 case X86_VENDOR_Intel:
4706 * Always-running Local APIC Timer is
4707 * indicated by CPUID.6.EAX[2].
4709 if (cpi->cpi_maxeax >= 6) {
4710 regs.cp_eax = 6;
4711 (void) cpuid_insn(NULL, &regs);
4712 return (regs.cp_eax & CPUID_CSTATE_ARAT);
4713 } else {
4714 return (0);
4716 default:
4717 return (0);
4722 * Check support for Intel ENERGY_PERF_BIAS feature
4725 cpuid_iepb_supported(struct cpu *cp)
4727 struct cpuid_info *cpi = cp->cpu_m.mcpu_cpi;
4728 struct cpuid_regs regs;
4730 ASSERT(cpuid_checkpass(cp, 1));
4732 if (!(is_x86_feature(x86_featureset, X86FSET_CPUID)) ||
4733 !(is_x86_feature(x86_featureset, X86FSET_MSR))) {
4734 return (0);
4738 * Intel ENERGY_PERF_BIAS MSR is indicated by
4739 * capability bit CPUID.6.ECX.3
4741 if ((cpi->cpi_vendor != X86_VENDOR_Intel) || (cpi->cpi_maxeax < 6))
4742 return (0);
4744 regs.cp_eax = 0x6;
4745 (void) cpuid_insn(NULL, &regs);
4746 return (regs.cp_ecx & CPUID_EPB_SUPPORT);
4750 * Check support for TSC deadline timer
4752 * TSC deadline timer provides a superior software programming
4753 * model over local APIC timer that eliminates "time drifts".
4754 * Instead of specifying a relative time, software specifies an
4755 * absolute time as the target at which the processor should
4756 * generate a timer event.
4759 cpuid_deadline_tsc_supported(void)
4761 struct cpuid_info *cpi = CPU->cpu_m.mcpu_cpi;
4762 struct cpuid_regs regs;
4764 ASSERT(cpuid_checkpass(CPU, 1));
4765 ASSERT(is_x86_feature(x86_featureset, X86FSET_CPUID));
4767 switch (cpi->cpi_vendor) {
4768 case X86_VENDOR_Intel:
4769 if (cpi->cpi_maxeax >= 1) {
4770 regs.cp_eax = 1;
4771 (void) cpuid_insn(NULL, &regs);
4772 return (regs.cp_ecx & CPUID_DEADLINE_TSC);
4773 } else {
4774 return (0);
4776 default:
4777 return (0);
4781 #if defined(__amd64) && !defined(__xpv)
4783 * Patch in versions of bcopy for high performance Intel Nhm processors
4784 * and later...
4786 void
4787 patch_memops(uint_t vendor)
4789 size_t cnt, i;
4790 caddr_t to, from;
4792 if ((vendor == X86_VENDOR_Intel) &&
4793 is_x86_feature(x86_featureset, X86FSET_SSE4_2)) {
4794 cnt = &bcopy_patch_end - &bcopy_patch_start;
4795 to = &bcopy_ck_size;
4796 from = &bcopy_patch_start;
4797 for (i = 0; i < cnt; i++) {
4798 *to++ = *from++;
4802 #endif /* __amd64 && !__xpv */
4805 * This function finds the number of bits to represent the number of cores per
4806 * chip and the number of strands per core for the Intel platforms.
4807 * It re-uses the x2APIC cpuid code of the cpuid_pass2().
4809 void
4810 cpuid_get_ext_topo(uint_t vendor, uint_t *core_nbits, uint_t *strand_nbits)
4812 struct cpuid_regs regs;
4813 struct cpuid_regs *cp = &regs;
4815 if (vendor != X86_VENDOR_Intel) {
4816 return;
4819 /* if the cpuid level is 0xB, extended topo is available. */
4820 cp->cp_eax = 0;
4821 if (__cpuid_insn(cp) >= 0xB) {
4823 cp->cp_eax = 0xB;
4824 cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
4825 (void) __cpuid_insn(cp);
4828 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
4829 * indicates that the extended topology enumeration leaf is
4830 * available.
4832 if (cp->cp_ebx) {
4833 uint_t coreid_shift = 0;
4834 uint_t chipid_shift = 0;
4835 uint_t i;
4836 uint_t level;
4838 for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
4839 cp->cp_eax = 0xB;
4840 cp->cp_ecx = i;
4842 (void) __cpuid_insn(cp);
4843 level = CPI_CPU_LEVEL_TYPE(cp);
4845 if (level == 1) {
4847 * Thread level processor topology
4848 * Number of bits shift right APIC ID
4849 * to get the coreid.
4851 coreid_shift = BITX(cp->cp_eax, 4, 0);
4852 } else if (level == 2) {
4854 * Core level processor topology
4855 * Number of bits shift right APIC ID
4856 * to get the chipid.
4858 chipid_shift = BITX(cp->cp_eax, 4, 0);
4862 if (coreid_shift > 0 && chipid_shift > coreid_shift) {
4863 *strand_nbits = coreid_shift;
4864 *core_nbits = chipid_shift - coreid_shift;