These changes are the raw update to qemu-2.6.
[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  * 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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <ipxe/cpuid.h>
28
29 /** @file
30  *
31  * x86 CPU feature detection
32  *
33  */
34
35 /**
36  * Check whether or not CPUID instruction is supported
37  *
38  * @ret is_supported    CPUID instruction is supported
39  */
40 int cpuid_is_supported ( void ) {
41         unsigned long original;
42         unsigned long inverted;
43
44         __asm__ ( "pushf\n\t"
45                   "pushf\n\t"
46                   "pop %0\n\t"
47                   "mov %0,%1\n\t"
48                   "xor %2,%1\n\t"
49                   "push %1\n\t"
50                   "popf\n\t"
51                   "pushf\n\t"
52                   "pop %1\n\t"
53                   "popf\n\t"
54                   : "=&r" ( original ), "=&r" ( inverted )
55                   : "ir" ( CPUID_FLAG ) );
56         return ( ( original ^ inverted ) & CPUID_FLAG );
57 }
58
59 /**
60  * Get Intel-defined x86 CPU features
61  *
62  * @v features          x86 CPU features to fill in
63  */
64 static void x86_intel_features ( struct x86_features *features ) {
65         uint32_t max_level;
66         uint32_t discard_a;
67         uint32_t discard_b;
68         uint32_t discard_c;
69         uint32_t discard_d;
70
71         /* Check that features are available via CPUID */
72         cpuid ( CPUID_VENDOR_ID, &max_level, &discard_b, &discard_c,
73                 &discard_d );
74         if ( max_level < CPUID_FEATURES ) {
75                 DBGC ( features, "CPUID has no Intel-defined features (max "
76                        "level %08x)\n", max_level );
77                 return;
78         }
79
80         /* Get features */
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 );
85
86 }
87
88 /**
89  * Get AMD-defined x86 CPU features
90  *
91  * @v features          x86 CPU features to fill in
92  */
93 static void x86_amd_features ( struct x86_features *features ) {
94         uint32_t max_level;
95         uint32_t discard_a;
96         uint32_t discard_b;
97         uint32_t discard_c;
98         uint32_t discard_d;
99
100         /* Check that features are available via CPUID */
101         cpuid ( CPUID_AMD_MAX_FN, &max_level, &discard_b, &discard_c,
102                 &discard_d );
103         if ( ( max_level & CPUID_AMD_CHECK_MASK ) != CPUID_AMD_CHECK ) {
104                 DBGC ( features, "CPUID has no extended functions\n" );
105                 return;
106         }
107         if ( max_level < CPUID_AMD_FEATURES ) {
108                 DBGC ( features, "CPUID has no AMD-defined features (max "
109                        "level %08x)\n", max_level );
110                 return;
111         }
112
113         /* Get features */
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 );
118 }
119
120 /**
121  * Get x86 CPU features
122  *
123  * @v features          x86 CPU features to fill in
124  */
125 void x86_features ( struct x86_features *features ) {
126
127         /* Clear all features */
128         memset ( features, 0, sizeof ( *features ) );
129
130         /* Check that CPUID instruction is available */
131         if ( ! cpuid_is_supported() ) {
132                 DBGC ( features, "CPUID instruction is not supported\n" );
133                 return;
134         }
135
136         /* Get Intel-defined features */
137         x86_intel_features ( features );
138
139         /* Get AMD-defined features */
140         x86_amd_features ( features );
141 }