2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
23 #include <ipxe/cpuid.h>
27 * x86 CPU feature detection
32 * Check whether or not CPUID instruction is supported
34 * @ret is_supported CPUID instruction is supported
36 int cpuid_is_supported ( void ) {
37 unsigned long original;
38 unsigned long inverted;
50 : "=&r" ( original ), "=&r" ( inverted )
51 : "ir" ( CPUID_FLAG ) );
52 return ( ( original ^ inverted ) & CPUID_FLAG );
56 * Get Intel-defined x86 CPU features
58 * @v features x86 CPU features to fill in
60 static void x86_intel_features ( struct x86_features *features ) {
67 /* Check that features are available via CPUID */
68 cpuid ( CPUID_VENDOR_ID, &max_level, &discard_b, &discard_c,
70 if ( max_level < CPUID_FEATURES ) {
71 DBGC ( features, "CPUID has no Intel-defined features (max "
72 "level %08x)\n", max_level );
77 cpuid ( CPUID_FEATURES, &discard_a, &discard_b,
78 &features->intel.ecx, &features->intel.edx );
79 DBGC ( features, "CPUID Intel features: %%ecx=%08x, %%edx=%08x\n",
80 features->intel.ecx, features->intel.edx );
85 * Get AMD-defined x86 CPU features
87 * @v features x86 CPU features to fill in
89 static void x86_amd_features ( struct x86_features *features ) {
96 /* Check that features are available via CPUID */
97 cpuid ( CPUID_AMD_MAX_FN, &max_level, &discard_b, &discard_c,
99 if ( ( max_level & CPUID_AMD_CHECK_MASK ) != CPUID_AMD_CHECK ) {
100 DBGC ( features, "CPUID has no extended functions\n" );
103 if ( max_level < CPUID_AMD_FEATURES ) {
104 DBGC ( features, "CPUID has no AMD-defined features (max "
105 "level %08x)\n", max_level );
110 cpuid ( CPUID_AMD_FEATURES, &discard_a, &discard_b,
111 &features->amd.ecx, &features->amd.edx );
112 DBGC ( features, "CPUID AMD features: %%ecx=%08x, %%edx=%08x\n",
113 features->amd.ecx, features->amd.edx );
117 * Get x86 CPU features
119 * @v features x86 CPU features to fill in
121 void x86_features ( struct x86_features *features ) {
123 /* Clear all features */
124 memset ( features, 0, sizeof ( *features ) );
126 /* Check that CPUID instruction is available */
127 if ( ! cpuid_is_supported() ) {
128 DBGC ( features, "CPUID instruction is not supported\n" );
132 /* Get Intel-defined features */
133 x86_intel_features ( features );
135 /* Get AMD-defined features */
136 x86_amd_features ( features );