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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 #include <ipxe/cpuid.h>
31 * x86 CPU feature detection
36 * Check whether or not CPUID instruction is supported
38 * @ret is_supported CPUID instruction is supported
40 int cpuid_is_supported ( void ) {
41 unsigned long original;
42 unsigned long inverted;
54 : "=&r" ( original ), "=&r" ( inverted )
55 : "ir" ( CPUID_FLAG ) );
56 return ( ( original ^ inverted ) & CPUID_FLAG );
60 * Get Intel-defined x86 CPU features
62 * @v features x86 CPU features to fill in
64 static void x86_intel_features ( struct x86_features *features ) {
71 /* Check that features are available via CPUID */
72 cpuid ( CPUID_VENDOR_ID, &max_level, &discard_b, &discard_c,
74 if ( max_level < CPUID_FEATURES ) {
75 DBGC ( features, "CPUID has no Intel-defined features (max "
76 "level %08x)\n", max_level );
81 cpuid ( CPUID_FEATURES, &discard_a, &discard_b,
82 &features->intel.ecx, &features->intel.edx );
83 DBGC ( features, "CPUID Intel features: %%ecx=%08x, %%edx=%08x\n",
84 features->intel.ecx, features->intel.edx );
89 * Get AMD-defined x86 CPU features
91 * @v features x86 CPU features to fill in
93 static void x86_amd_features ( struct x86_features *features ) {
100 /* Check that features are available via CPUID */
101 cpuid ( CPUID_AMD_MAX_FN, &max_level, &discard_b, &discard_c,
103 if ( ( max_level & CPUID_AMD_CHECK_MASK ) != CPUID_AMD_CHECK ) {
104 DBGC ( features, "CPUID has no extended functions\n" );
107 if ( max_level < CPUID_AMD_FEATURES ) {
108 DBGC ( features, "CPUID has no AMD-defined features (max "
109 "level %08x)\n", max_level );
114 cpuid ( CPUID_AMD_FEATURES, &discard_a, &discard_b,
115 &features->amd.ecx, &features->amd.edx );
116 DBGC ( features, "CPUID AMD features: %%ecx=%08x, %%edx=%08x\n",
117 features->amd.ecx, features->amd.edx );
121 * Get x86 CPU features
123 * @v features x86 CPU features to fill in
125 void x86_features ( struct x86_features *features ) {
127 /* Clear all features */
128 memset ( features, 0, sizeof ( *features ) );
130 /* Check that CPUID instruction is available */
131 if ( ! cpuid_is_supported() ) {
132 DBGC ( features, "CPUID instruction is not supported\n" );
136 /* Get Intel-defined features */
137 x86_intel_features ( features );
139 /* Get AMD-defined features */
140 x86_amd_features ( features );