Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / x86 / core / cpuid.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <string.h>
23 #include <ipxe/cpuid.h>
24
25 /** @file
26  *
27  * x86 CPU feature detection
28  *
29  */
30
31 /**
32  * Check whether or not CPUID instruction is supported
33  *
34  * @ret is_supported    CPUID instruction is supported
35  */
36 int cpuid_is_supported ( void ) {
37         unsigned long original;
38         unsigned long inverted;
39
40         __asm__ ( "pushf\n\t"
41                   "pushf\n\t"
42                   "pop %0\n\t"
43                   "mov %0,%1\n\t"
44                   "xor %2,%1\n\t"
45                   "push %1\n\t"
46                   "popf\n\t"
47                   "pushf\n\t"
48                   "pop %1\n\t"
49                   "popf\n\t"
50                   : "=&r" ( original ), "=&r" ( inverted )
51                   : "ir" ( CPUID_FLAG ) );
52         return ( ( original ^ inverted ) & CPUID_FLAG );
53 }
54
55 /**
56  * Get Intel-defined x86 CPU features
57  *
58  * @v features          x86 CPU features to fill in
59  */
60 static void x86_intel_features ( struct x86_features *features ) {
61         uint32_t max_level;
62         uint32_t discard_a;
63         uint32_t discard_b;
64         uint32_t discard_c;
65         uint32_t discard_d;
66
67         /* Check that features are available via CPUID */
68         cpuid ( CPUID_VENDOR_ID, &max_level, &discard_b, &discard_c,
69                 &discard_d );
70         if ( max_level < CPUID_FEATURES ) {
71                 DBGC ( features, "CPUID has no Intel-defined features (max "
72                        "level %08x)\n", max_level );
73                 return;
74         }
75
76         /* Get features */
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 );
81
82 }
83
84 /**
85  * Get AMD-defined x86 CPU features
86  *
87  * @v features          x86 CPU features to fill in
88  */
89 static void x86_amd_features ( struct x86_features *features ) {
90         uint32_t max_level;
91         uint32_t discard_a;
92         uint32_t discard_b;
93         uint32_t discard_c;
94         uint32_t discard_d;
95
96         /* Check that features are available via CPUID */
97         cpuid ( CPUID_AMD_MAX_FN, &max_level, &discard_b, &discard_c,
98                 &discard_d );
99         if ( ( max_level & CPUID_AMD_CHECK_MASK ) != CPUID_AMD_CHECK ) {
100                 DBGC ( features, "CPUID has no extended functions\n" );
101                 return;
102         }
103         if ( max_level < CPUID_AMD_FEATURES ) {
104                 DBGC ( features, "CPUID has no AMD-defined features (max "
105                        "level %08x)\n", max_level );
106                 return;
107         }
108
109         /* Get features */
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 );
114 }
115
116 /**
117  * Get x86 CPU features
118  *
119  * @v features          x86 CPU features to fill in
120  */
121 void x86_features ( struct x86_features *features ) {
122
123         /* Clear all features */
124         memset ( features, 0, sizeof ( *features ) );
125
126         /* Check that CPUID instruction is available */
127         if ( ! cpuid_is_supported() ) {
128                 DBGC ( features, "CPUID instruction is not supported\n" );
129                 return;
130         }
131
132         /* Get Intel-defined features */
133         x86_intel_features ( features );
134
135         /* Get AMD-defined features */
136         x86_amd_features ( features );
137 }