Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2017 Intel Corporation
3 : : */
4 : :
5 : : #include <fcntl.h>
6 : : #include <unistd.h>
7 : : #ifdef RTE_TOOLCHAIN_MSVC
8 : : #define bit_AVX (1 << 28)
9 : : #else
10 : : #include <cpuid.h>
11 : : #endif
12 : :
13 : :
14 : : #include "eal_private.h"
15 : :
16 : : static unsigned int
17 : : rte_cpu_get_model(uint32_t fam_mod_step)
18 : : {
19 : : uint32_t family, model, ext_model;
20 : :
21 : 144 : family = (fam_mod_step >> 8) & 0xf;
22 : 144 : model = (fam_mod_step >> 4) & 0xf;
23 : :
24 [ + - ]: 144 : if (family == 6 || family == 15) {
25 : 144 : ext_model = (fam_mod_step >> 16) & 0xf;
26 : 144 : model += (ext_model << 4);
27 : : }
28 : :
29 : : return model;
30 : : }
31 : :
32 : : static int32_t
33 : 144 : rdmsr(int msr, uint64_t *val)
34 : : {
35 : : #ifdef RTE_EXEC_ENV_LINUX
36 : : int fd;
37 : : int ret;
38 : :
39 : : fd = open("/dev/cpu/0/msr", O_RDONLY);
40 [ + - ]: 144 : if (fd < 0)
41 : : return fd;
42 : :
43 [ + - ]: 144 : ret = pread(fd, val, sizeof(uint64_t), msr);
44 : :
45 : 144 : close(fd);
46 : :
47 : 144 : return ret;
48 : : #else
49 : : RTE_SET_USED(msr);
50 : : RTE_SET_USED(val);
51 : :
52 : : return -1;
53 : : #endif
54 : : }
55 : :
56 : : static uint32_t
57 : : check_model_wsm_nhm(uint8_t model)
58 : : {
59 : 144 : switch (model) {
60 : : /* Westmere */
61 : : case 0x25:
62 : : case 0x2C:
63 : : case 0x2F:
64 : : /* Nehalem */
65 : : case 0x1E:
66 : : case 0x1F:
67 : : case 0x1A:
68 : : case 0x2E:
69 : : return 1;
70 : : }
71 : :
72 : : return 0;
73 : : }
74 : :
75 : : static uint32_t
76 : : check_model_gdm_dnv(uint8_t model)
77 : : {
78 [ # # ]: 0 : switch (model) {
79 : : /* Goldmont */
80 : : case 0x5C:
81 : : /* Denverton */
82 : : case 0x5F:
83 : : return 1;
84 : : }
85 : :
86 : : return 0;
87 : : }
88 : :
89 : : #ifdef RTE_TOOLCHAIN_MSVC
90 : : int
91 : : __get_cpuid_max(unsigned int e, unsigned int *s)
92 : : {
93 : : uint32_t cpuinfo[4];
94 : :
95 : : __cpuid(cpuinfo, e);
96 : : if (s)
97 : : *s = cpuinfo[1];
98 : : return cpuinfo[0];
99 : : }
100 : : #endif
101 : :
102 : : uint64_t
103 : 144 : get_tsc_freq_arch(void)
104 : : {
105 : : #ifdef RTE_TOOLCHAIN_MSVC
106 : : int cpuinfo[4];
107 : : #endif
108 : 144 : uint64_t tsc_hz = 0;
109 : : uint32_t a, b, c, d, maxleaf;
110 : : uint8_t mult, model;
111 : : int32_t ret;
112 : :
113 : : /*
114 : : * Time Stamp Counter and Nominal Core Crystal Clock
115 : : * Information Leaf
116 : : */
117 : : maxleaf = __get_cpuid_max(0, NULL);
118 : :
119 [ + - ]: 144 : if (maxleaf >= 0x15) {
120 : : #ifdef RTE_TOOLCHAIN_MSVC
121 : : __cpuid(cpuinfo, 0x15);
122 : : a = cpuinfo[0];
123 : : b = cpuinfo[1];
124 : : c = cpuinfo[2];
125 : : d = cpuinfo[3];
126 : : #else
127 : 144 : __cpuid(0x15, a, b, c, d);
128 : : #endif
129 : :
130 : : /* EBX : TSC/Crystal ratio, ECX : Crystal Hz */
131 [ - + ]: 144 : if (b && c)
132 : 0 : return c * (b / a);
133 : : }
134 : :
135 : : #ifdef RTE_TOOLCHAIN_MSVC
136 : : __cpuid(cpuinfo, 0x1);
137 : : a = cpuinfo[0];
138 : : b = cpuinfo[1];
139 : : c = cpuinfo[2];
140 : : d = cpuinfo[3];
141 : : #else
142 : 144 : __cpuid(0x1, a, b, c, d);
143 : : #endif
144 [ + - ]: 144 : model = rte_cpu_get_model(a);
145 : :
146 : : if (check_model_wsm_nhm(model))
147 : : mult = 133;
148 [ - + ]: 144 : else if ((c & bit_AVX) || check_model_gdm_dnv(model))
149 : : mult = 100;
150 : : else
151 : : return 0;
152 : :
153 : 144 : ret = rdmsr(0xCE, &tsc_hz);
154 [ + - ]: 144 : if (ret < 0)
155 : : return 0;
156 : :
157 : 144 : return ((tsc_hz >> 8) & 0xff) * mult * 1E6;
158 : : }
|