Also treat model numbers 0x5a/0x5d as Silvermont
[platform/upstream/glibc.git] / sysdeps / x86_64 / multiarch / init-arch.c
1 /* Initialize CPU feature data.
2    This file is part of the GNU C Library.
3    Copyright (C) 2008-2015 Free Software Foundation, Inc.
4    Contributed by Ulrich Drepper <drepper@redhat.com>.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <atomic.h>
21 #include <cpuid.h>
22 #include "init-arch.h"
23
24
25 struct cpu_features __cpu_features attribute_hidden;
26
27
28 static void
29 get_common_indeces (unsigned int *family, unsigned int *model)
30 {
31   __cpuid (1, __cpu_features.cpuid[COMMON_CPUID_INDEX_1].eax,
32            __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ebx,
33            __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx,
34            __cpu_features.cpuid[COMMON_CPUID_INDEX_1].edx);
35
36   unsigned int eax = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].eax;
37   *family = (eax >> 8) & 0x0f;
38   *model = (eax >> 4) & 0x0f;
39 }
40
41
42 void
43 __init_cpu_features (void)
44 {
45   unsigned int ebx;
46   unsigned int ecx;
47   unsigned int edx;
48   unsigned int family = 0;
49   unsigned int model = 0;
50   enum cpu_features_kind kind;
51
52   __cpuid (0, __cpu_features.max_cpuid, ebx, ecx, edx);
53
54   /* This spells out "GenuineIntel".  */
55   if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
56     {
57       kind = arch_kind_intel;
58
59       get_common_indeces (&family, &model);
60
61       unsigned int eax = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].eax;
62       unsigned int extended_family = (eax >> 20) & 0xff;
63       unsigned int extended_model = (eax >> 12) & 0xf0;
64       if (family == 0x0f)
65         {
66           family += extended_family;
67           model += extended_model;
68         }
69       else if (family == 0x06)
70         {
71           ecx = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx;
72           model += extended_model;
73           switch (model)
74             {
75             case 0x1c:
76             case 0x26:
77               /* BSF is slow on Atom.  */
78               __cpu_features.feature[index_Slow_BSF] |= bit_Slow_BSF;
79               break;
80
81             case 0x37:
82             case 0x4a:
83             case 0x4d:
84             case 0x5a:
85             case 0x5d:
86               /* Unaligned load versions are faster than SSSE3
87                  on Silvermont.  */
88 #if index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
89 # error index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
90 #endif
91 #if index_Fast_Unaligned_Load != index_Slow_SSE4_2
92 # error index_Fast_Unaligned_Load != index_Slow_SSE4_2
93 #endif
94               __cpu_features.feature[index_Fast_Unaligned_Load]
95                 |= (bit_Fast_Unaligned_Load
96                     | bit_Prefer_PMINUB_for_stringop
97                     | bit_Slow_SSE4_2);
98               break;
99
100             default:
101               /* Unknown family 0x06 processors.  Assuming this is one
102                  of Core i3/i5/i7 processors if AVX is available.  */
103               if ((ecx & bit_AVX) == 0)
104                 break;
105
106             case 0x1a:
107             case 0x1e:
108             case 0x1f:
109             case 0x25:
110             case 0x2c:
111             case 0x2e:
112             case 0x2f:
113               /* Rep string instructions, copy backward, unaligned loads
114                  and pminub are fast on Intel Core i3, i5 and i7.  */
115 #if index_Fast_Rep_String != index_Fast_Copy_Backward
116 # error index_Fast_Rep_String != index_Fast_Copy_Backward
117 #endif
118 #if index_Fast_Rep_String != index_Fast_Unaligned_Load
119 # error index_Fast_Rep_String != index_Fast_Unaligned_Load
120 #endif
121 #if index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
122 # error index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
123 #endif
124               __cpu_features.feature[index_Fast_Rep_String]
125                 |= (bit_Fast_Rep_String
126                     | bit_Fast_Copy_Backward
127                     | bit_Fast_Unaligned_Load
128                     | bit_Prefer_PMINUB_for_stringop);
129               break;
130             }
131         }
132     }
133   /* This spells out "AuthenticAMD".  */
134   else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
135     {
136       kind = arch_kind_amd;
137
138       get_common_indeces (&family, &model);
139
140       ecx = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx;
141
142       unsigned int eax;
143       __cpuid (0x80000000, eax, ebx, ecx, edx);
144       if (eax >= 0x80000001)
145         __cpuid (0x80000001,
146                  __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].eax,
147                  __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].ebx,
148                  __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].ecx,
149                  __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].edx);
150     }
151   else
152     kind = arch_kind_other;
153
154   if (__cpu_features.max_cpuid >= 7)
155     __cpuid_count (7, 0,
156                    __cpu_features.cpuid[COMMON_CPUID_INDEX_7].eax,
157                    __cpu_features.cpuid[COMMON_CPUID_INDEX_7].ebx,
158                    __cpu_features.cpuid[COMMON_CPUID_INDEX_7].ecx,
159                    __cpu_features.cpuid[COMMON_CPUID_INDEX_7].edx);
160
161   /* Can we call xgetbv?  */
162   if (CPUID_OSXSAVE)
163     {
164       unsigned int xcrlow;
165       unsigned int xcrhigh;
166       asm ("xgetbv" : "=a" (xcrlow), "=d" (xcrhigh) : "c" (0));
167       /* Is YMM and XMM state usable?  */
168       if ((xcrlow & (bit_YMM_state | bit_XMM_state)) ==
169           (bit_YMM_state | bit_XMM_state))
170         {
171           /* Determine if AVX is usable.  */
172           if (CPUID_AVX)
173             __cpu_features.feature[index_AVX_Usable] |= bit_AVX_Usable;
174           /* Determine if AVX2 is usable.  */
175           if (CPUID_AVX2)
176             __cpu_features.feature[index_AVX2_Usable] |= bit_AVX2_Usable;
177           /* Determine if FMA is usable.  */
178           if (CPUID_FMA)
179             __cpu_features.feature[index_FMA_Usable] |= bit_FMA_Usable;
180           /* Determine if FMA4 is usable.  */
181           if (CPUID_FMA4)
182             __cpu_features.feature[index_FMA4_Usable] |= bit_FMA4_Usable;
183         }
184     }
185
186   __cpu_features.family = family;
187   __cpu_features.model = model;
188   atomic_write_barrier ();
189   __cpu_features.kind = kind;
190 }
191
192 #undef __get_cpu_features
193
194 const struct cpu_features *
195 __get_cpu_features (void)
196 {
197   if (__cpu_features.kind == arch_kind_unknown)
198     __init_cpu_features ();
199
200   return &__cpu_features;
201 }