f2dbaf1f7d8be84972e57e7e57a6c350cca610b5
[vpp.git] / vppinfra / vppinfra / cpu.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vppinfra/clib.h>
16 #include <vppinfra/format.h>
17 #include <vppinfra/cpu.h>
18
19 #if __x86_64__
20 #include <cpuid.h>
21 #endif
22
23 #define foreach_x86_cpu_uarch \
24  _(0x06, 0x4f, "Broadwell", "Broadwell-EP/EX") \
25  _(0x06, 0x3d, "Broadwell", "Broadwell") \
26  _(0x06, 0x3f, "Haswell", "Haswell-E") \
27  _(0x06, 0x3c, "Haswell", "Haswell") \
28  _(0x06, 0x3e, "IvyBridge", "IvyBridge-E/EN/EP") \
29  _(0x06, 0x3a, "IvyBridge", "IvyBridge") \
30  _(0x06, 0x2a, "SandyBridge", "SandyBridge") \
31  _(0x06, 0x2d, "SandyBridge", "SandyBridge-E/EN/EP") \
32  _(0x06, 0x25, "Westmere", "Arrandale,Clarksdale") \
33  _(0x06, 0x2c, "Westmere", "Westmere-EP/EX,Gulftown") \
34  _(0x06, 0x2f, "Westmere", "Westmere-EX") \
35  _(0x06, 0x1e, "Nehalem", "Clarksfield,Lynnfield,Jasper Forest") \
36  _(0x06, 0x1a, "Nehalem", "Nehalem-EP,Bloomfield)") \
37  _(0x06, 0x2e, "Nehalem", "Nehalem-EX") \
38  _(0x06, 0x17, "Penryn", "Yorkfield,Wolfdale,Penryn,Harpertown (DP)") \
39  _(0x06, 0x1d, "Penryn", "Dunnington (MP)") \
40  _(0x06, 0x37, "Atom", "Bay Trail") \
41  _(0x06, 0x36, "Atom", "Cedarview") \
42  _(0x06, 0x26, "Atom", "Lincroft") \
43  _(0x06, 0x1c, "Atom", "Pineview/Silverthorne")
44
45 u8 *
46 format_cpu_uarch (u8 * s, va_list * args)
47 {
48 #if __x86_64__
49   u32 __attribute__((unused)) eax, ebx, ecx, edx;
50   u8 model, family;
51
52   if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0)
53     return format (s, "unknown (missing cpuid)");
54
55   model = ((eax >> 4) & 0x0f) | ((eax >> 12) & 0xf0);
56   family = (eax >> 8) & 0x0f;
57
58 #define _(f,m,a,c) if ((model == m) && (family == f)) return format(s, "%s (%s)", a, c);
59   foreach_x86_cpu_uarch
60 #undef _
61   return format (s, "unknown (family 0x%02x model 0x%02x)", family, model);
62
63 #else /* ! __x86_64__ */
64   return format (s, "unknown");
65 #endif
66 }
67
68 u8 *
69 format_cpu_model_name (u8 * s, va_list * args)
70 {
71 #if __x86_64__
72   u32 __attribute__((unused)) eax, ebx, ecx, edx;
73   u8 * name = 0;
74   u32 * name_u32;
75
76   if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) == 0)
77     return format (s, "unknown (missing cpuid)");
78
79   __get_cpuid (0x80000000, &eax, &ebx, &ecx, &edx);
80   if (eax < 0x80000004)
81     return format (s, "unknown (missing ext feature)");
82
83   vec_validate(name, 48);
84   name_u32 = (u32 *) name;
85
86   __get_cpuid (0x80000002, &eax, &ebx, &ecx, &edx);
87   name_u32[0] = eax;
88   name_u32[1] = ebx;
89   name_u32[2] = ecx;
90   name_u32[3] = edx;
91
92   __get_cpuid (0x80000003, &eax, &ebx, &ecx, &edx);
93   name_u32[4] = eax;
94   name_u32[5] = ebx;
95   name_u32[6] = ecx;
96   name_u32[7] = edx;
97
98   __get_cpuid (0x80000004, &eax, &ebx, &ecx, &edx);
99   name_u32[8] = eax;
100   name_u32[9] = ebx;
101   name_u32[10] = ecx;
102   name_u32[11] = edx;
103
104   s = format (s, "%s", name);
105   vec_free(name);
106   return s;
107
108 #else /* ! __x86_64__ */
109   return format (s, "unknown");
110 #endif
111 }