blob: 0c80d5582eb1d3ba7a5468d083647d7040d461f3 [file] [log] [blame]
Marat Dukhan3045d4f2017-03-04 01:51:42 -05001#include <stdint.h>
2#include <stddef.h>
3#include <stdlib.h>
4#include <string.h>
Marat Dukhan3045d4f2017-03-04 01:51:42 -05005
6#include <cpuinfo.h>
7#include <x86/api.h>
Marat Dukhan4f70b9a2017-09-25 23:18:41 -07008#include <x86/linux/api.h>
Marat Dukhan3045d4f2017-03-04 01:51:42 -05009#include <linux/api.h>
Marat Dukhand0b37602018-12-09 01:59:26 -080010#include <cpuinfo/internal-api.h>
11#include <cpuinfo/log.h>
Marat Dukhan3045d4f2017-03-04 01:51:42 -050012
13
Marat Dukhan77eb9412017-03-04 21:01:16 -050014static inline uint32_t bit_mask(uint32_t bits) {
15 return (UINT32_C(1) << bits) - UINT32_C(1);
16}
17
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070018static inline bool bitmask_all(uint32_t bitfield, uint32_t mask) {
19 return (bitfield & mask) == mask;
20}
Marat Dukhan3045d4f2017-03-04 01:51:42 -050021
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070022static inline uint32_t min(uint32_t a, uint32_t b) {
23 return a < b ? a : b;
24}
25
26static inline int cmp(uint32_t a, uint32_t b) {
27 return (a > b) - (a < b);
28}
29
30static int cmp_x86_linux_processor(const void* ptr_a, const void* ptr_b) {
31 const struct cpuinfo_x86_linux_processor* processor_a = (const struct cpuinfo_x86_linux_processor*) ptr_a;
32 const struct cpuinfo_x86_linux_processor* processor_b = (const struct cpuinfo_x86_linux_processor*) ptr_b;
33
34 /* Move usable processors towards the start of the array */
35 const bool usable_a = bitmask_all(processor_a->flags, CPUINFO_LINUX_MASK_USABLE);
36 const bool usable_b = bitmask_all(processor_b->flags, CPUINFO_LINUX_MASK_USABLE);
37 if (usable_a != usable_b) {
38 return (int) usable_b - (int) usable_a;
Marat Dukhan3045d4f2017-03-04 01:51:42 -050039 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070040
41 /* Compare based on APIC ID (i.e. processor 0 < processor 1) */
42 const uint32_t id_a = processor_a->apic_id;
43 const uint32_t id_b = processor_b->apic_id;
44 return cmp(id_a, id_b);
Marat Dukhan3045d4f2017-03-04 01:51:42 -050045}
46
Marat Dukhan43a51cd2017-09-22 16:35:05 -070047static void cpuinfo_x86_count_objects(
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070048 uint32_t linux_processors_count,
49 const struct cpuinfo_x86_linux_processor linux_processors[restrict static linux_processors_count],
50 const struct cpuinfo_x86_processor processor[restrict static 1],
Marat Dukhan76583492018-04-01 02:19:15 -070051 uint32_t llc_apic_bits,
Marat Dukhan43a51cd2017-09-22 16:35:05 -070052 uint32_t cores_count_ptr[restrict static 1],
Marat Dukhan76583492018-04-01 02:19:15 -070053 uint32_t clusters_count_ptr[restrict static 1],
Marat Dukhan43a51cd2017-09-22 16:35:05 -070054 uint32_t packages_count_ptr[restrict static 1],
Marat Dukhan3045d4f2017-03-04 01:51:42 -050055 uint32_t l1i_count_ptr[restrict static 1],
56 uint32_t l1d_count_ptr[restrict static 1],
57 uint32_t l2_count_ptr[restrict static 1],
58 uint32_t l3_count_ptr[restrict static 1],
59 uint32_t l4_count_ptr[restrict static 1])
60{
Marat Dukhan76583492018-04-01 02:19:15 -070061 const uint32_t core_apic_mask =
62 ~(bit_mask(processor->topology.thread_bits_length) << processor->topology.thread_bits_offset);
63 const uint32_t package_apic_mask =
64 core_apic_mask & ~(bit_mask(processor->topology.core_bits_length) << processor->topology.core_bits_offset);
65 const uint32_t llc_apic_mask = ~bit_mask(llc_apic_bits);
66 const uint32_t cluster_apic_mask = package_apic_mask | llc_apic_mask;
67
68 uint32_t cores_count = 0, clusters_count = 0, packages_count = 0;
Marat Dukhan3045d4f2017-03-04 01:51:42 -050069 uint32_t l1i_count = 0, l1d_count = 0, l2_count = 0, l3_count = 0, l4_count = 0;
Marat Dukhan76583492018-04-01 02:19:15 -070070 uint32_t last_core_id = UINT32_MAX, last_cluster_id = UINT32_MAX, last_package_id = UINT32_MAX;
Marat Dukhan3045d4f2017-03-04 01:51:42 -050071 uint32_t last_l1i_id = UINT32_MAX, last_l1d_id = UINT32_MAX;
72 uint32_t last_l2_id = UINT32_MAX, last_l3_id = UINT32_MAX, last_l4_id = UINT32_MAX;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070073 for (uint32_t i = 0; i < linux_processors_count; i++) {
74 if (bitmask_all(linux_processors[i].flags, CPUINFO_LINUX_MASK_USABLE)) {
75 const uint32_t apic_id = linux_processors[i].apic_id;
76 cpuinfo_log_debug("APID ID %"PRIu32": system processor %"PRIu32, apic_id, linux_processors[i].linux_id);
77
78 /* All bits of APIC ID except thread ID mask */
Marat Dukhan76583492018-04-01 02:19:15 -070079 const uint32_t core_id = apic_id & core_apic_mask;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070080 if (core_id != last_core_id) {
81 last_core_id = core_id;
82 cores_count++;
Marat Dukhan3045d4f2017-03-04 01:51:42 -050083 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070084 /* All bits of APIC ID except thread ID and core ID masks */
Marat Dukhan76583492018-04-01 02:19:15 -070085 const uint32_t package_id = apic_id & package_apic_mask;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070086 if (package_id != last_package_id) {
87 last_package_id = package_id;
88 packages_count++;
Marat Dukhan3045d4f2017-03-04 01:51:42 -050089 }
Marat Dukhan76583492018-04-01 02:19:15 -070090 /* Bits of APIC ID which are part of either LLC or package ID mask */
91 const uint32_t cluster_id = apic_id & cluster_apic_mask;
92 if (cluster_id != last_cluster_id) {
93 last_cluster_id = cluster_id;
94 clusters_count++;
95 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -070096 if (processor->cache.l1i.size != 0) {
97 const uint32_t l1i_id = apic_id & ~bit_mask(processor->cache.l1i.apic_bits);
98 if (l1i_id != last_l1i_id) {
99 last_l1i_id = l1i_id;
100 l1i_count++;
101 }
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500102 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700103 if (processor->cache.l1d.size != 0) {
104 const uint32_t l1d_id = apic_id & ~bit_mask(processor->cache.l1d.apic_bits);
105 if (l1d_id != last_l1d_id) {
106 last_l1d_id = l1d_id;
107 l1d_count++;
108 }
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500109 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700110 if (processor->cache.l2.size != 0) {
111 const uint32_t l2_id = apic_id & ~bit_mask(processor->cache.l2.apic_bits);
112 if (l2_id != last_l2_id) {
113 last_l2_id = l2_id;
114 l2_count++;
115 }
116 }
117 if (processor->cache.l3.size != 0) {
118 const uint32_t l3_id = apic_id & ~bit_mask(processor->cache.l3.apic_bits);
119 if (l3_id != last_l3_id) {
120 last_l3_id = l3_id;
121 l3_count++;
122 }
123 }
124 if (processor->cache.l4.size != 0) {
125 const uint32_t l4_id = apic_id & ~bit_mask(processor->cache.l4.apic_bits);
126 if (l4_id != last_l4_id) {
127 last_l4_id = l4_id;
128 l4_count++;
129 }
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500130 }
131 }
132 }
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700133 *cores_count_ptr = cores_count;
Marat Dukhan76583492018-04-01 02:19:15 -0700134 *clusters_count_ptr = clusters_count;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700135 *packages_count_ptr = packages_count;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500136 *l1i_count_ptr = l1i_count;
137 *l1d_count_ptr = l1d_count;
138 *l2_count_ptr = l2_count;
139 *l3_count_ptr = l3_count;
140 *l4_count_ptr = l4_count;
141}
142
143void cpuinfo_x86_linux_init(void) {
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700144 struct cpuinfo_x86_linux_processor* x86_linux_processors = NULL;
145 struct cpuinfo_processor* processors = NULL;
146 struct cpuinfo_core* cores = NULL;
Marat Dukhan4d376c32018-03-18 11:36:39 -0700147 struct cpuinfo_cluster* clusters = NULL;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700148 struct cpuinfo_package* packages = NULL;
Marat Dukhand9f76e02017-09-24 22:46:08 -0700149 const struct cpuinfo_processor** linux_cpu_to_processor_map = NULL;
150 const struct cpuinfo_core** linux_cpu_to_core_map = NULL;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500151 struct cpuinfo_cache* l1i = NULL;
152 struct cpuinfo_cache* l1d = NULL;
153 struct cpuinfo_cache* l2 = NULL;
154 struct cpuinfo_cache* l3 = NULL;
155 struct cpuinfo_cache* l4 = NULL;
156
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700157 const uint32_t max_processors_count = cpuinfo_linux_get_max_processors_count();
158 cpuinfo_log_debug("system maximum processors count: %"PRIu32, max_processors_count);
159
160 const uint32_t max_possible_processors_count = 1 +
161 cpuinfo_linux_get_max_possible_processor(max_processors_count);
162 cpuinfo_log_debug("maximum possible processors count: %"PRIu32, max_possible_processors_count);
163 const uint32_t max_present_processors_count = 1 +
Marat Dukhan72c3c382018-01-08 17:33:40 -0800164 cpuinfo_linux_get_max_present_processor(max_processors_count);
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700165 cpuinfo_log_debug("maximum present processors count: %"PRIu32, max_present_processors_count);
166
167 const uint32_t x86_linux_processors_count = min(max_possible_processors_count, max_present_processors_count);
168 x86_linux_processors = calloc(x86_linux_processors_count, sizeof(struct cpuinfo_x86_linux_processor));
169 if (x86_linux_processors == NULL) {
170 cpuinfo_log_error(
171 "failed to allocate %zu bytes for descriptions of %"PRIu32" x86 logical processors",
172 x86_linux_processors_count * sizeof(struct cpuinfo_x86_linux_processor),
173 x86_linux_processors_count);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500174 return;
175 }
176
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700177 cpuinfo_linux_detect_possible_processors(
178 x86_linux_processors_count, &x86_linux_processors->flags,
179 sizeof(struct cpuinfo_x86_linux_processor),
180 CPUINFO_LINUX_FLAG_POSSIBLE);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500181
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700182 cpuinfo_linux_detect_present_processors(
183 x86_linux_processors_count, &x86_linux_processors->flags,
184 sizeof(struct cpuinfo_x86_linux_processor),
185 CPUINFO_LINUX_FLAG_PRESENT);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500186
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700187 if (!cpuinfo_x86_linux_parse_proc_cpuinfo(x86_linux_processors_count, x86_linux_processors)) {
188 cpuinfo_log_error("failed to parse processor information from /proc/cpuinfo");
189 return;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500190 }
191
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700192 struct cpuinfo_x86_processor x86_processor;
193 memset(&x86_processor, 0, sizeof(x86_processor));
194 cpuinfo_x86_init_processor(&x86_processor);
Marat Dukhan88346572018-03-11 15:28:17 -0700195 char brand_string[48];
196 cpuinfo_x86_normalize_brand_string(x86_processor.brand_string, brand_string);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500197
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700198 uint32_t processors_count = 0;
199 for (uint32_t i = 0; i < x86_linux_processors_count; i++) {
200 if (bitmask_all(x86_linux_processors[i].flags, CPUINFO_LINUX_MASK_USABLE)) {
201 x86_linux_processors[i].linux_id = i;
202 processors_count++;
203 }
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500204 }
205
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700206 qsort(x86_linux_processors, x86_linux_processors_count, sizeof(struct cpuinfo_x86_linux_processor),
207 cmp_x86_linux_processor);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500208
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700209 processors = calloc(processors_count, sizeof(struct cpuinfo_processor));
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500210 if (processors == NULL) {
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700211 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" logical processors",
212 processors_count * sizeof(struct cpuinfo_processor), processors_count);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500213 goto cleanup;
214 }
215
Marat Dukhan76583492018-04-01 02:19:15 -0700216 uint32_t llc_apic_bits = 0;
217 if (x86_processor.cache.l4.size != 0) {
218 llc_apic_bits = x86_processor.cache.l4.apic_bits;
219 } else if (x86_processor.cache.l3.size != 0) {
220 llc_apic_bits = x86_processor.cache.l3.apic_bits;
221 } else if (x86_processor.cache.l2.size != 0) {
222 llc_apic_bits = x86_processor.cache.l2.apic_bits;
223 } else if (x86_processor.cache.l1d.size != 0) {
224 llc_apic_bits = x86_processor.cache.l1d.apic_bits;
225 }
226 uint32_t packages_count = 0, clusters_count = 0, cores_count = 0;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500227 uint32_t l1i_count = 0, l1d_count = 0, l2_count = 0, l3_count = 0, l4_count = 0;
Marat Dukhan76583492018-04-01 02:19:15 -0700228 cpuinfo_x86_count_objects(x86_linux_processors_count, x86_linux_processors, &x86_processor, llc_apic_bits,
229 &cores_count, &clusters_count, &packages_count, &l1i_count, &l1d_count, &l2_count, &l3_count, &l4_count);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500230
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700231 cpuinfo_log_debug("detected %"PRIu32" cores", cores_count);
Marat Dukhan76583492018-04-01 02:19:15 -0700232 cpuinfo_log_debug("detected %"PRIu32" clusters", clusters_count);
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700233 cpuinfo_log_debug("detected %"PRIu32" packages", packages_count);
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700234 cpuinfo_log_debug("detected %"PRIu32" L1I caches", l1i_count);
235 cpuinfo_log_debug("detected %"PRIu32" L1D caches", l1d_count);
236 cpuinfo_log_debug("detected %"PRIu32" L2 caches", l2_count);
237 cpuinfo_log_debug("detected %"PRIu32" L3 caches", l3_count);
238 cpuinfo_log_debug("detected %"PRIu32" L4 caches", l4_count);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500239
Marat Dukhan02efbc92017-10-28 14:19:18 -0400240 linux_cpu_to_processor_map = calloc(x86_linux_processors_count, sizeof(struct cpuinfo_processor*));
Marat Dukhand9f76e02017-09-24 22:46:08 -0700241 if (linux_cpu_to_processor_map == NULL) {
242 cpuinfo_log_error("failed to allocate %zu bytes for mapping entries of %"PRIu32" logical processors",
Marat Dukhan02efbc92017-10-28 14:19:18 -0400243 x86_linux_processors_count * sizeof(struct cpuinfo_processor*),
244 x86_linux_processors_count);
Marat Dukhand9f76e02017-09-24 22:46:08 -0700245 goto cleanup;
246 }
247
Marat Dukhan02efbc92017-10-28 14:19:18 -0400248 linux_cpu_to_core_map = calloc(x86_linux_processors_count, sizeof(struct cpuinfo_core*));
Marat Dukhand9f76e02017-09-24 22:46:08 -0700249 if (linux_cpu_to_core_map == NULL) {
250 cpuinfo_log_error("failed to allocate %zu bytes for mapping entries of %"PRIu32" cores",
Marat Dukhan02efbc92017-10-28 14:19:18 -0400251 x86_linux_processors_count * sizeof(struct cpuinfo_core*),
252 x86_linux_processors_count);
Marat Dukhand9f76e02017-09-24 22:46:08 -0700253 goto cleanup;
254 }
255
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700256 cores = calloc(cores_count, sizeof(struct cpuinfo_core));
257 if (cores == NULL) {
258 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" cores",
259 cores_count * sizeof(struct cpuinfo_core), cores_count);
260 goto cleanup;
261 }
Marat Dukhan4d376c32018-03-18 11:36:39 -0700262
Marat Dukhan76583492018-04-01 02:19:15 -0700263 clusters = calloc(clusters_count, sizeof(struct cpuinfo_cluster));
Marat Dukhan4d376c32018-03-18 11:36:39 -0700264 if (clusters == NULL) {
265 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" core clusters",
Marat Dukhan76583492018-04-01 02:19:15 -0700266 clusters_count * sizeof(struct cpuinfo_cluster), clusters_count);
Marat Dukhan4d376c32018-03-18 11:36:39 -0700267 goto cleanup;
268 }
269
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700270 packages = calloc(packages_count, sizeof(struct cpuinfo_package));
271 if (packages == NULL) {
Marat Dukhan29000602018-03-18 00:29:39 -0700272 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" physical packages",
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700273 packages_count * sizeof(struct cpuinfo_package), packages_count);
274 goto cleanup;
275 }
Marat Dukhan29000602018-03-18 00:29:39 -0700276
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500277 if (l1i_count != 0) {
278 l1i = calloc(l1i_count, sizeof(struct cpuinfo_cache));
279 if (l1i == NULL) {
280 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1I caches",
281 l1i_count * sizeof(struct cpuinfo_cache), l1i_count);
282 goto cleanup;
283 }
284 }
285 if (l1d_count != 0) {
286 l1d = calloc(l1d_count, sizeof(struct cpuinfo_cache));
287 if (l1d == NULL) {
288 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1D caches",
289 l1d_count * sizeof(struct cpuinfo_cache), l1d_count);
290 goto cleanup;
291 }
292 }
293 if (l2_count != 0) {
294 l2 = calloc(l2_count, sizeof(struct cpuinfo_cache));
295 if (l2 == NULL) {
296 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L2 caches",
297 l2_count * sizeof(struct cpuinfo_cache), l2_count);
298 goto cleanup;
299 }
300 }
301 if (l3_count != 0) {
302 l3 = calloc(l3_count, sizeof(struct cpuinfo_cache));
303 if (l3 == NULL) {
304 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L3 caches",
305 l3_count * sizeof(struct cpuinfo_cache), l3_count);
306 goto cleanup;
307 }
308 }
309 if (l4_count != 0) {
310 l4 = calloc(l4_count, sizeof(struct cpuinfo_cache));
311 if (l4 == NULL) {
312 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L4 caches",
313 l4_count * sizeof(struct cpuinfo_cache), l4_count);
314 goto cleanup;
315 }
316 }
317
Marat Dukhan76583492018-04-01 02:19:15 -0700318 const uint32_t core_apic_mask =
319 ~(bit_mask(x86_processor.topology.thread_bits_length) << x86_processor.topology.thread_bits_offset);
320 const uint32_t package_apic_mask =
321 core_apic_mask & ~(bit_mask(x86_processor.topology.core_bits_length) << x86_processor.topology.core_bits_offset);
322 const uint32_t llc_apic_mask = ~bit_mask(llc_apic_bits);
323 const uint32_t cluster_apic_mask = package_apic_mask | llc_apic_mask;
324
325 uint32_t processor_index = UINT32_MAX, core_index = UINT32_MAX, cluster_index = UINT32_MAX, package_index = UINT32_MAX;
Marat Dukhanfbc4cfc2017-09-22 16:45:45 -0700326 uint32_t l1i_index = UINT32_MAX, l1d_index = UINT32_MAX, l2_index = UINT32_MAX, l3_index = UINT32_MAX, l4_index = UINT32_MAX;
Marat Dukhan76583492018-04-01 02:19:15 -0700327 uint32_t cluster_id = 0, core_id = 0, smt_id = 0;
328 uint32_t last_apic_core_id = UINT32_MAX, last_apic_cluster_id = UINT32_MAX, last_apic_package_id = UINT32_MAX;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500329 uint32_t last_l1i_id = UINT32_MAX, last_l1d_id = UINT32_MAX;
330 uint32_t last_l2_id = UINT32_MAX, last_l3_id = UINT32_MAX, last_l4_id = UINT32_MAX;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700331 for (uint32_t i = 0; i < x86_linux_processors_count; i++) {
332 if (bitmask_all(x86_linux_processors[i].flags, CPUINFO_LINUX_MASK_USABLE)) {
333 const uint32_t apic_id = x86_linux_processors[i].apic_id;
334 processor_index++;
335 smt_id++;
Marat Dukhan38dc29a2017-09-22 17:33:25 -0700336
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700337 /* All bits of APIC ID except thread ID mask */
Marat Dukhan76583492018-04-01 02:19:15 -0700338 const uint32_t apid_core_id = apic_id & core_apic_mask;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700339 if (apid_core_id != last_apic_core_id) {
340 core_index++;
341 core_id++;
342 smt_id = 0;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700343 }
Marat Dukhan76583492018-04-01 02:19:15 -0700344 /* Bits of APIC ID which are part of either LLC or package ID mask */
345 const uint32_t apic_cluster_id = apic_id & cluster_apic_mask;
346 if (apic_cluster_id != last_apic_cluster_id) {
347 cluster_index++;
348 cluster_id++;
349 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700350 /* All bits of APIC ID except thread ID and core ID masks */
Marat Dukhan76583492018-04-01 02:19:15 -0700351 const uint32_t apic_package_id = apic_id & package_apic_mask;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700352 if (apic_package_id != last_apic_package_id) {
353 package_index++;
354 core_id = 0;
Marat Dukhan76583492018-04-01 02:19:15 -0700355 cluster_id = 0;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500356 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700357
358 /* Initialize logical processor object */
359 processors[processor_index].smt_id = smt_id;
360 processors[processor_index].core = cores + core_index;
Marat Dukhan76583492018-04-01 02:19:15 -0700361 processors[processor_index].cluster = clusters + cluster_index;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700362 processors[processor_index].package = packages + package_index;
363 processors[processor_index].linux_id = x86_linux_processors[i].linux_id;
364 processors[processor_index].apic_id = x86_linux_processors[i].apic_id;
365
366 if (apid_core_id != last_apic_core_id) {
367 /* new core */
368 cores[core_index] = (struct cpuinfo_core) {
369 .processor_start = processor_index,
Marat Dukhanab3a1272017-08-25 23:20:07 -0700370 .processor_count = 1,
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700371 .core_id = core_id,
Marat Dukhan76583492018-04-01 02:19:15 -0700372 .cluster = clusters + cluster_index,
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700373 .package = packages + package_index,
374 .vendor = x86_processor.vendor,
375 .uarch = x86_processor.uarch,
376 .cpuid = x86_processor.cpuid,
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500377 };
Marat Dukhan76583492018-04-01 02:19:15 -0700378 clusters[cluster_index].core_count += 1;
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700379 packages[package_index].core_count += 1;
380 last_apic_core_id = apid_core_id;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500381 } else {
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700382 /* another logical processor on the same core */
383 cores[core_index].processor_count++;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500384 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700385
Marat Dukhan76583492018-04-01 02:19:15 -0700386 if (apic_cluster_id != last_apic_cluster_id) {
387 /* new cluster */
388 clusters[cluster_index].processor_start = processor_index;
389 clusters[cluster_index].processor_count = 1;
390 clusters[cluster_index].core_start = core_index;
391 clusters[cluster_index].cluster_id = cluster_id;
392 clusters[cluster_index].package = packages + package_index;
393 clusters[cluster_index].vendor = x86_processor.vendor;
394 clusters[cluster_index].uarch = x86_processor.uarch;
395 clusters[cluster_index].cpuid = x86_processor.cpuid;
396 packages[package_index].cluster_count += 1;
397 last_apic_cluster_id = apic_cluster_id;
398 } else {
399 /* another logical processor on the same cluster */
400 clusters[cluster_index].processor_count++;
401 }
Marat Dukhan4d376c32018-03-18 11:36:39 -0700402
Marat Dukhan76583492018-04-01 02:19:15 -0700403 if (apic_package_id != last_apic_package_id) {
404 /* new package */
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700405 packages[package_index].processor_start = processor_index;
406 packages[package_index].processor_count = 1;
407 packages[package_index].core_start = core_index;
Marat Dukhan76583492018-04-01 02:19:15 -0700408 packages[package_index].cluster_start = cluster_index;
Marat Dukhan88346572018-03-11 15:28:17 -0700409 cpuinfo_x86_format_package_name(x86_processor.vendor, brand_string, packages[package_index].name);
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700410 last_apic_package_id = apic_package_id;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500411 } else {
Marat Dukhan76583492018-04-01 02:19:15 -0700412 /* another logical processor on the same package */
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700413 packages[package_index].processor_count++;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500414 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700415
416 linux_cpu_to_processor_map[x86_linux_processors[i].linux_id] = processors + processor_index;
417 linux_cpu_to_core_map[x86_linux_processors[i].linux_id] = cores + core_index;
418
419 if (x86_processor.cache.l1i.size != 0) {
420 const uint32_t l1i_id = apic_id & ~bit_mask(x86_processor.cache.l1i.apic_bits);
421 processors[i].cache.l1i = &l1i[l1i_index];
422 if (l1i_id != last_l1i_id) {
423 /* new cache */
424 last_l1i_id = l1i_id;
425 l1i[++l1i_index] = (struct cpuinfo_cache) {
426 .size = x86_processor.cache.l1i.size,
427 .associativity = x86_processor.cache.l1i.associativity,
428 .sets = x86_processor.cache.l1i.sets,
429 .partitions = x86_processor.cache.l1i.partitions,
430 .line_size = x86_processor.cache.l1i.line_size,
431 .flags = x86_processor.cache.l1i.flags,
432 .processor_start = processor_index,
433 .processor_count = 1,
434 };
435 } else {
436 /* another processor sharing the same cache */
437 l1i[l1i_index].processor_count += 1;
438 }
439 processors[i].cache.l1i = &l1i[l1i_index];
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500440 } else {
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700441 /* reset cache id */
442 last_l1i_id = UINT32_MAX;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500443 }
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700444 if (x86_processor.cache.l1d.size != 0) {
445 const uint32_t l1d_id = apic_id & ~bit_mask(x86_processor.cache.l1d.apic_bits);
446 processors[i].cache.l1d = &l1d[l1d_index];
447 if (l1d_id != last_l1d_id) {
448 /* new cache */
449 last_l1d_id = l1d_id;
450 l1d[++l1d_index] = (struct cpuinfo_cache) {
451 .size = x86_processor.cache.l1d.size,
452 .associativity = x86_processor.cache.l1d.associativity,
453 .sets = x86_processor.cache.l1d.sets,
454 .partitions = x86_processor.cache.l1d.partitions,
455 .line_size = x86_processor.cache.l1d.line_size,
456 .flags = x86_processor.cache.l1d.flags,
457 .processor_start = processor_index,
458 .processor_count = 1,
459 };
460 } else {
461 /* another processor sharing the same cache */
462 l1d[l1d_index].processor_count += 1;
463 }
464 processors[i].cache.l1d = &l1d[l1d_index];
465 } else {
466 /* reset cache id */
467 last_l1d_id = UINT32_MAX;
468 }
469 if (x86_processor.cache.l2.size != 0) {
470 const uint32_t l2_id = apic_id & ~bit_mask(x86_processor.cache.l2.apic_bits);
471 processors[i].cache.l2 = &l2[l2_index];
472 if (l2_id != last_l2_id) {
473 /* new cache */
474 last_l2_id = l2_id;
475 l2[++l2_index] = (struct cpuinfo_cache) {
476 .size = x86_processor.cache.l2.size,
477 .associativity = x86_processor.cache.l2.associativity,
478 .sets = x86_processor.cache.l2.sets,
479 .partitions = x86_processor.cache.l2.partitions,
480 .line_size = x86_processor.cache.l2.line_size,
481 .flags = x86_processor.cache.l2.flags,
482 .processor_start = processor_index,
483 .processor_count = 1,
484 };
485 } else {
486 /* another processor sharing the same cache */
487 l2[l2_index].processor_count += 1;
488 }
489 processors[i].cache.l2 = &l2[l2_index];
490 } else {
491 /* reset cache id */
492 last_l2_id = UINT32_MAX;
493 }
494 if (x86_processor.cache.l3.size != 0) {
495 const uint32_t l3_id = apic_id & ~bit_mask(x86_processor.cache.l3.apic_bits);
496 processors[i].cache.l3 = &l3[l3_index];
497 if (l3_id != last_l3_id) {
498 /* new cache */
499 last_l3_id = l3_id;
500 l3[++l3_index] = (struct cpuinfo_cache) {
501 .size = x86_processor.cache.l3.size,
502 .associativity = x86_processor.cache.l3.associativity,
503 .sets = x86_processor.cache.l3.sets,
504 .partitions = x86_processor.cache.l3.partitions,
505 .line_size = x86_processor.cache.l3.line_size,
506 .flags = x86_processor.cache.l3.flags,
507 .processor_start = processor_index,
508 .processor_count = 1,
509 };
510 } else {
511 /* another processor sharing the same cache */
512 l3[l3_index].processor_count += 1;
513 }
514 processors[i].cache.l3 = &l3[l3_index];
515 } else {
516 /* reset cache id */
517 last_l3_id = UINT32_MAX;
518 }
519 if (x86_processor.cache.l4.size != 0) {
520 const uint32_t l4_id = apic_id & ~bit_mask(x86_processor.cache.l4.apic_bits);
521 processors[i].cache.l4 = &l4[l4_index];
522 if (l4_id != last_l4_id) {
523 /* new cache */
524 last_l4_id = l4_id;
525 l4[++l4_index] = (struct cpuinfo_cache) {
526 .size = x86_processor.cache.l4.size,
527 .associativity = x86_processor.cache.l4.associativity,
528 .sets = x86_processor.cache.l4.sets,
529 .partitions = x86_processor.cache.l4.partitions,
530 .line_size = x86_processor.cache.l4.line_size,
531 .flags = x86_processor.cache.l4.flags,
532 .processor_start = processor_index,
533 .processor_count = 1,
534 };
535 } else {
536 /* another processor sharing the same cache */
537 l4[l4_index].processor_count += 1;
538 }
539 processors[i].cache.l4 = &l4[l4_index];
540 } else {
541 /* reset cache id */
542 last_l4_id = UINT32_MAX;
543 }
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500544 }
545 }
546
547 /* Commit changes */
Marat Dukhand9f76e02017-09-24 22:46:08 -0700548 cpuinfo_linux_cpu_to_processor_map = linux_cpu_to_processor_map;
549 cpuinfo_linux_cpu_to_core_map = linux_cpu_to_core_map;
550
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500551 cpuinfo_processors = processors;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700552 cpuinfo_cores = cores;
Marat Dukhan4d376c32018-03-18 11:36:39 -0700553 cpuinfo_clusters = clusters;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700554 cpuinfo_packages = packages;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500555 cpuinfo_cache[cpuinfo_cache_level_1i] = l1i;
556 cpuinfo_cache[cpuinfo_cache_level_1d] = l1d;
557 cpuinfo_cache[cpuinfo_cache_level_2] = l2;
558 cpuinfo_cache[cpuinfo_cache_level_3] = l3;
559 cpuinfo_cache[cpuinfo_cache_level_4] = l4;
560
Marat Dukhan8ecad1a2017-05-08 07:21:57 +0000561 cpuinfo_processors_count = processors_count;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700562 cpuinfo_cores_count = cores_count;
Marat Dukhan76583492018-04-01 02:19:15 -0700563 cpuinfo_clusters_count = clusters_count;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700564 cpuinfo_packages_count = packages_count;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500565 cpuinfo_cache_count[cpuinfo_cache_level_1i] = l1i_count;
566 cpuinfo_cache_count[cpuinfo_cache_level_1d] = l1d_count;
567 cpuinfo_cache_count[cpuinfo_cache_level_2] = l2_count;
568 cpuinfo_cache_count[cpuinfo_cache_level_3] = l3_count;
569 cpuinfo_cache_count[cpuinfo_cache_level_4] = l4_count;
570
Marat Dukhancf70aee2018-03-24 23:21:02 -0700571 __sync_synchronize();
572
573 cpuinfo_is_initialized = true;
574
Marat Dukhand9f76e02017-09-24 22:46:08 -0700575 linux_cpu_to_processor_map = NULL;
576 linux_cpu_to_core_map = NULL;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500577 processors = NULL;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700578 cores = NULL;
Marat Dukhan4d376c32018-03-18 11:36:39 -0700579 clusters = NULL;
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700580 packages = NULL;
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500581 l1i = l1d = l2 = l3 = l4 = NULL;
582
583cleanup:
Marat Dukhand9f76e02017-09-24 22:46:08 -0700584 free(linux_cpu_to_processor_map);
585 free(linux_cpu_to_core_map);
Marat Dukhan4f70b9a2017-09-25 23:18:41 -0700586 free(x86_linux_processors);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500587 free(processors);
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700588 free(cores);
Marat Dukhan4d376c32018-03-18 11:36:39 -0700589 free(clusters);
Marat Dukhan43a51cd2017-09-22 16:35:05 -0700590 free(packages);
Marat Dukhan3045d4f2017-03-04 01:51:42 -0500591 free(l1i);
592 free(l1d);
593 free(l2);
594 free(l3);
595 free(l4);
596}