blob: 648952eee378ea28c26fc8b9749aa400481163fc [file] [log] [blame]
Hao Lu922070c2017-10-18 16:29:02 -07001#include <stdio.h>
2#include <stdint.h>
3#include <stdlib.h>
4#include <string.h>
5#include <alloca.h>
6
7#include <errno.h>
8#include <sys/types.h>
9#include <sys/sysctl.h>
10#include <mach/host_info.h>
11#include <mach/mach_host.h>
12#include <mach/machine.h>
13
14#include <cpuinfo.h>
15#include <mach/api.h>
16#include <api.h>
17#include <log.h>
18
19
Marat Dukhanec862142017-10-18 17:24:46 -070020struct cpuinfo_arm_isa cpuinfo_isa = {
21#if CPUINFO_ARCH_ARM
22 .thumb = true,
23 .thumb2 = true,
24 .thumbee = false,
25 .jazelle = false,
26 .armv5e = true,
27 .armv6 = true,
28 .armv6k = true,
29 .armv7 = true,
30 .vfpv2 = false,
31 .vfpv3 = true,
32 .d32 = true,
33 .wmmx = false,
34 .wmmx2 = false,
35 .neon = true,
36#endif
37#if CPUINFO_ARCH_ARM64
38 .aes = true,
39 .sha1 = true,
40 .sha2 = true,
41 .pmull = true,
42 .crc32 = true,
43#endif
44};
Marat Dukhan7b738882017-10-18 16:59:28 -070045
Hao Lu922070c2017-10-18 16:29:02 -070046static uint32_t get_sys_info(int type_specifier, char* name) {
47 size_t size = 0;
48 uint32_t result = 0;
49 int mib[2] = { CTL_HW, type_specifier };
50 if (sysctl(mib, 2, NULL, &size, NULL, 0) != 0) {
51 cpuinfo_log_error("sysctl(\"%s\") failed: %s", name, strerror(errno));
52 } else if (size == sizeof(uint32_t)) {
53 sysctl(mib, 2, &result, &size, NULL, 0);
54 cpuinfo_log_debug("%s: %"PRIu32 ", size = %lu", name, result, size);
55 } else {
56 cpuinfo_log_warning("sysctl does not support non-integer lookup for (\"%s\")", name);
57 }
58 return result;
59}
60
61static uint64_t get_sys_info_by_name(char* type_specifier) {
62 size_t size = 0;
63 uint32_t result = 0;
64 if (sysctlbyname(type_specifier, NULL, &size, NULL, 0) != 0) {
65 cpuinfo_log_error("sysctlbyname(\"%s\") failed: %s", type_specifier, strerror(errno));
66 } else if (size == sizeof(uint32_t)) {
67 sysctlbyname(type_specifier, &result, &size, NULL, 0);
68 cpuinfo_log_debug("%s: %"PRIu32 ", size = %lu", type_specifier, result, size);
69 } else {
70 cpuinfo_log_warning("sysctl does not support non-integer lookup for (\"%s\")", type_specifier);
71 }
72 return result;
73}
74
75static enum cpuinfo_uarch decode_uarch(uint32_t cpu_family, uint32_t cpu_subtype, uint32_t core_index) {
76 switch (cpu_family) {
77 case CPUFAMILY_ARM_SWIFT:
78 return cpuinfo_uarch_swift;
79 case CPUFAMILY_ARM_CYCLONE:
80 return cpuinfo_uarch_cyclone;
81 case CPUFAMILY_ARM_TYPHOON:
82 return cpuinfo_uarch_typhoon;
83 case CPUFAMILY_ARM_TWISTER:
84 return cpuinfo_uarch_twister;
85 case CPUFAMILY_ARM_HURRICANE:
86 return cpuinfo_uarch_hurricane;
Marat Dukhan7b738882017-10-18 16:59:28 -070087#ifdef CPUFAMILY_ARM_MONSOON_MISTRAL
Hao Lu922070c2017-10-18 16:29:02 -070088 case CPUFAMILY_ARM_MONSOON_MISTRAL:
Marat Dukhan7b738882017-10-18 16:59:28 -070089#else
90 case 0xe81e7ef6:
91 /* Hard-coded value for older SDKs which do not define CPUFAMILY_ARM_MONSOON_MISTRAL */
92#endif
Hao Lu922070c2017-10-18 16:29:02 -070093 /* 2x Monsoon + 4x Mistral cores */
94 return core_index < 2 ? cpuinfo_uarch_monsoon : cpuinfo_uarch_mistral;
95 default:
96 /* Use hw.cpusubtype for detection */
97 break;
98 }
99
100 switch (cpu_subtype) {
101 case CPU_SUBTYPE_ARM_V7:
102 return cpuinfo_uarch_cortex_a8;
103 case CPU_SUBTYPE_ARM_V7F:
104 return cpuinfo_uarch_cortex_a9;
105 case CPU_SUBTYPE_ARM_V7K:
106 return cpuinfo_uarch_cortex_a7;
107 default:
108 return cpuinfo_uarch_unknown;
109 }
110}
111
112static void decode_package_name(char* package_name) {
113 size_t size;
114 if (sysctlbyname("hw.machine", NULL, &size, NULL, 0) != 0) {
115 cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
116 return;
117 }
118
119 char *machine_name = alloca(size);
120 if (sysctlbyname("hw.machine", machine_name, &size, NULL, 0) != 0) {
121 cpuinfo_log_warning("sysctlbyname(\"hw.machine\") failed: %s", strerror(errno));
122 return;
123 }
124 cpuinfo_log_debug("hw.machine: %s", machine_name);
125
126 char name[10];
127 uint32_t major = 0, minor = 0;
128 if (sscanf(machine_name, "%9[^,0123456789]%"SCNu32",%"SCNu32, name, &major, &minor) != 3) {
129 cpuinfo_log_warning("parsing \"hw.machine\" failed: %s", strerror(errno));
130 return;
131 }
132
133 uint32_t chip_model = 0;
134 char suffix = '\0';
135 if (strcmp(name, "iPhone") == 0) {
136 /*
137 * iPhone 4 and up are supported:
138 * - iPhone 4 [A4]: iPhone3,1, iPhone3,2, iPhone3,3
139 * - iPhone 4S [A5]: iPhone4,1
140 * - iPhone 5 [A6]: iPhone5,1, iPhone5,2
141 * - iPhone 5c [A6]: iPhone5,3, iPhone5,4
142 * - iPhone 5s [A7]: iPhone6,1, iPhone6,2
143 * - iPhone 6 [A8]: iPhone7,2
144 * - iPhone 6 Plus [A8]: iPhone7,1
145 * - iPhone 6s [A9]: iPhone8,1
146 * - iPhone 6s Plus [A9]: iPhone8,2
147 * - iPhone SE [A9]: iPhone8,4
148 * - iPhone 7 [A10]: iPhone9,1, iPhone9,3
149 * - iPhone 7 Plus [A10]: iPhone9,2, iPhone9,4
150 * - iPhone 8 [A11]: iPhone10,1, iPhone10,4
151 * - iPhone 8 Plus [A11]: iPhone10,2, iPhone10,5
152 * - iPhone X [A11]: iPhone10,3, iPhone10,6
153 */
154 chip_model = major + 1;
155 } else if (strcmp(name, "iPad") == 0) {
156 switch (major) {
157 /* iPad 2 and up are supported */
158 case 2:
159 /*
160 * iPad 2 [A5]: iPad2,1, iPad2,2, iPad2,3, iPad2,4
161 * iPad mini [A5]: iPad2,5, iPad2,6, iPad2,7
162 */
163 chip_model = major + 3;
164 break;
165 case 3:
166 /*
167 * iPad 3rd Gen [A5X]: iPad3,1, iPad3,2, iPad3,3
168 * iPad 4th Gen [A6X]: iPad3,4, iPad3,5, iPad3,6
169 */
170 chip_model = (minor <= 3) ? 5 : 6;
171 suffix = 'X';
172 break;
173 case 4:
174 /*
175 * iPad Air [A7]: iPad4,1, iPad4,2, iPad4,3
176 * iPad mini Retina [A7]: iPad4,4, iPad4,5, iPad4,6
177 * iPad mini 3 [A7]: iPad4,7, iPad4,8, iPad4,9
178 */
179 chip_model = major + 3;
180 break;
181 case 5:
182 /*
183 * iPad mini 4 [A8]: iPad5,1, iPad5,2
184 * iPad Air 2 [A8X]: iPad5,3, iPad5,4
185 */
186 chip_model = major + 3;
187 suffix = (minor <= 2) ? '\0' : 'X';
188 break;
189 case 6:
190 /*
191 * iPad Pro 9.7" [A9X]: iPad6,3, iPad6,4
192 * iPad Pro [A9X]: iPad6,7, iPad6,8
193 * iPad 5th Gen [A9]: iPad6,11, iPad6,12
194 */
195 chip_model = major + 3;
196 suffix = minor <= 8 ? 'X' : '\0';
197 break;
198 case 7:
199 /*
200 * iPad Pro 12.9" [A10X]: iPad7,1, iPad7,2
201 * iPad Pro 10.5" [A10X]: iPad7,3, iPad7,4
202 */
203 chip_model = major + 3;
204 suffix = 'X';
205 break;
206 default:
207 cpuinfo_log_info("unknown iPad: %s", machine_name);
208 break;
209 }
210 } else if (strcmp(name, "iPod") == 0) {
211 switch (major) {
212 case 5:
213 chip_model = 5;
214 break;
215 /* iPod touch (5th Gen) [A5]: iPod5,1 */
216 case 7:
217 /* iPod touch (6th Gen, 2015) [A8]: iPod7,1 */
218 chip_model = 8;
219 break;
220 default:
221 cpuinfo_log_info("unknown iPod: %s", machine_name);
222 break;
223 }
224 } else {
225 cpuinfo_log_info("unknown device: %s", machine_name);
226 }
227 if (chip_model != 0) {
228 snprintf(package_name, CPUINFO_PACKAGE_NAME_MAX, "Apple A%"PRIu32"%c", chip_model, suffix);
229 }
230}
231
232void cpuinfo_arm_mach_init(void) {
233 struct cpuinfo_processor* processors = NULL;
234 struct cpuinfo_core* cores = NULL;
235 struct cpuinfo_package* packages = NULL;
236 struct cpuinfo_cache* l1i = NULL;
237 struct cpuinfo_cache* l1d = NULL;
238 struct cpuinfo_cache* l2 = NULL;
239 struct cpuinfo_cache* l3 = NULL;
240
241 struct cpuinfo_mach_topology mach_topology = cpuinfo_mach_detect_topology();
242 processors = calloc(mach_topology.threads, sizeof(struct cpuinfo_processor));
243 if (processors == NULL) {
244 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" logical processors",
245 mach_topology.threads * sizeof(struct cpuinfo_processor), mach_topology.threads);
246 goto cleanup;
247 }
248 cores = calloc(mach_topology.cores, sizeof(struct cpuinfo_core));
249 if (cores == NULL) {
250 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" cores",
251 mach_topology.cores * sizeof(struct cpuinfo_core), mach_topology.cores);
252 goto cleanup;
253 }
254 packages = calloc(mach_topology.packages, sizeof(struct cpuinfo_package));
255 if (packages == NULL) {
256 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" packages",
257 mach_topology.packages * sizeof(struct cpuinfo_package), mach_topology.packages);
258 goto cleanup;
259 }
260
261 const uint32_t threads_per_core = mach_topology.threads / mach_topology.cores;
262 const uint32_t threads_per_package = mach_topology.threads / mach_topology.packages;
263 const uint32_t cores_per_package = mach_topology.cores / mach_topology.packages;
264
265 for (uint32_t i = 0; i < mach_topology.packages; i++) {
266 packages[i] = (struct cpuinfo_package) {
267 .processor_start = i * threads_per_package,
268 .processor_count = threads_per_package,
269 .core_start = i * cores_per_package,
270 .core_count = cores_per_package,
271 };
272 decode_package_name(packages[i].name);
273 }
Marat Dukhanec862142017-10-18 17:24:46 -0700274
275
Hao Lu922070c2017-10-18 16:29:02 -0700276 const uint32_t cpu_family = get_sys_info_by_name("hw.cpufamily");
Marat Dukhanec862142017-10-18 17:24:46 -0700277 const uint32_t cpu_type = get_sys_info_by_name("hw.cputype");
Hao Lu922070c2017-10-18 16:29:02 -0700278 const uint32_t cpu_subtype = get_sys_info_by_name("hw.cpusubtype");
Marat Dukhanec862142017-10-18 17:24:46 -0700279 switch (cpu_type) {
280 case CPU_TYPE_ARM64:
281 cpuinfo_isa.aes = true;
282 cpuinfo_isa.sha1 = true;
283 cpuinfo_isa.sha2 = true;
284 cpuinfo_isa.pmull = true;
285 cpuinfo_isa.crc32 = true;
286 break;
287#if CPUINFO_ARCH_ARM
288 case CPU_TYPE_ARM:
289 switch (cpu_subtype) {
290 case CPU_SUBTYPE_ARM_V8:
291 cpuinfo_isa.aes = true;
292 cpuinfo_isa.sha1 = true;
293 cpuinfo_isa.sha2 = true;
294 cpuinfo_isa.pmull = true;
295 cpuinfo_isa.crc32 = true;
296 /* Fall-through to add ARMv7S features */
297 case CPU_SUBTYPE_ARM_V7S:
298 case CPU_SUBTYPE_ARM_V7K:
299 cpuinfo_isa.fma = true;
300 /* Fall-through to add ARMv7F features */
301 case CPU_SUBTYPE_ARM_V7F:
302 cpuinfo_isa.armv7mp = true;
303 cpuinfo_isa.fp16 = true;
304 /* Fall-through to add ARMv7 features */
305 case CPU_SUBTYPE_ARM_V7:
306 break;
307 default:
308 break;
309 }
310 break;
311#endif
312 }
313
Hao Lu922070c2017-10-18 16:29:02 -0700314 for (uint32_t i = 0; i < mach_topology.cores; i++) {
315 cores[i] = (struct cpuinfo_core) {
316 .processor_start = i * threads_per_core,
317 .processor_count = threads_per_core,
318 .core_id = i % cores_per_package,
319 .package = packages + i / cores_per_package,
320 .vendor = cpuinfo_vendor_apple,
321 .uarch = decode_uarch(cpu_family, cpu_subtype, i),
322 };
323 }
324 for (uint32_t i = 0; i < mach_topology.threads; i++) {
325 const uint32_t smt_id = i % threads_per_core;
326 const uint32_t core_id = i / threads_per_core;
327 const uint32_t package_id = i / threads_per_package;
328
329 processors[i].smt_id = smt_id;
330 processors[i].core = cores + i / threads_per_core;
331 processors[i].package = packages + i / threads_per_package;
332 }
333
334 const uint32_t cacheline_size = get_sys_info(HW_CACHELINE, "HW_CACHELINE");
335 const uint32_t l1d_cache_size = get_sys_info(HW_L1DCACHESIZE, "HW_L1DCACHESIZE");
336 const uint32_t l1i_cache_size = get_sys_info(HW_L1ICACHESIZE, "HW_L1ICACHESIZE");
337 const uint32_t l2_cache_size = get_sys_info(HW_L2CACHESIZE, "HW_L2CACHESIZE");
338 const uint32_t l3_cache_size = get_sys_info(HW_L3CACHESIZE, "HW_L3CACHESIZE");
339 const uint32_t l1_cache_associativity = 4;
340 const uint32_t l2_cache_associativity = 8;
341 const uint32_t l3_cache_associativity = 16;
342 const uint32_t cache_partitions = 1;
343 const uint32_t cache_flags = 0;
344
345 uint32_t threads_per_l1 = 0, l1_count = 0;
346 if (l1i_cache_size != 0 || l1d_cache_size != 0) {
347 /* Assume that L1 caches are private to each core */
348 threads_per_l1 = 1;
349 l1_count = mach_topology.threads / threads_per_l1;
350 cpuinfo_log_debug("detected %"PRIu32" L1 caches", l1_count);
351 }
352
353 uint32_t threads_per_l2 = 0, l2_count = 0;
354 if (l2_cache_size != 0) {
355 /* L2 cache is shared between all cores */
356 threads_per_l2 = mach_topology.cores;
357 l2_count = 1;
358 cpuinfo_log_debug("detected %"PRIu32" L2 caches", l2_count);
359 }
360
361 uint32_t threads_per_l3 = 0, l3_count = 0;
362 if (l3_cache_size != 0) {
363 /* L3 cache is shared between all cores */
364 threads_per_l3 = mach_topology.cores;
365 l3_count = 1;
366 cpuinfo_log_debug("detected %"PRIu32" L3 caches", l3_count);
367 }
368
369 if (l1i_cache_size != 0) {
370 l1i = calloc(l1_count, sizeof(struct cpuinfo_cache));
371 if (l1i == NULL) {
372 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1I caches",
373 l1_count * sizeof(struct cpuinfo_cache), l1_count);
374 goto cleanup;
375 }
376 for (uint32_t c = 0; c < l1_count; c++) {
377 l1i[c] = (struct cpuinfo_cache) {
378 .size = l1i_cache_size,
379 .associativity = l1_cache_associativity,
380 .sets = l1i_cache_size / (l1_cache_associativity * cacheline_size),
381 .partitions = cache_partitions,
382 .line_size = cacheline_size,
383 .flags = cache_flags,
384 .processor_start = c * threads_per_l1,
385 .processor_count = threads_per_l1,
386 };
387 }
388 for (uint32_t t = 0; t < mach_topology.threads; t++) {
389 processors[t].cache.l1i = &l1i[t / threads_per_l1];
390 }
391 }
392
393 if (l1d_cache_size != 0) {
394 l1d = calloc(l1_count, sizeof(struct cpuinfo_cache));
395 if (l1d == NULL) {
396 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L1D caches",
397 l1_count * sizeof(struct cpuinfo_cache), l1_count);
398 goto cleanup;
399 }
400 for (uint32_t c = 0; c < l1_count; c++) {
401 l1d[c] = (struct cpuinfo_cache) {
402 .size = l1d_cache_size,
403 .associativity = l1_cache_associativity,
404 .sets = l1d_cache_size / (l1_cache_associativity * cacheline_size),
405 .partitions = cache_partitions,
406 .line_size = cacheline_size,
407 .flags = cache_flags,
408 .processor_start = c * threads_per_l1,
409 .processor_count = threads_per_l1,
410 };
411 }
412 for (uint32_t t = 0; t < mach_topology.threads; t++) {
413 processors[t].cache.l1d = &l1d[t / threads_per_l1];
414 }
415 }
416
417 if (l2_count != 0) {
418 l2 = calloc(l2_count, sizeof(struct cpuinfo_cache));
419 if (l2 == NULL) {
420 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L2 caches",
421 l2_count * sizeof(struct cpuinfo_cache), l2_count);
422 goto cleanup;
423 }
424 for (uint32_t c = 0; c < l2_count; c++) {
425 l2[c] = (struct cpuinfo_cache) {
426 .size = l2_cache_size,
427 .associativity = l2_cache_associativity,
428 .sets = l2_cache_size / (l2_cache_associativity * cacheline_size),
429 .partitions = cache_partitions,
430 .line_size = cacheline_size,
431 .flags = cache_flags,
432 .processor_start = c * threads_per_l2,
433 .processor_count = threads_per_l2,
434 };
435 }
436 for (uint32_t t = 0; t < mach_topology.threads; t++) {
437 processors[t].cache.l2 = &l2[0];
438 }
439 }
440
441 if (l3_count != 0) {
442 l3 = calloc(l3_count, sizeof(struct cpuinfo_cache));
443 if (l3 == NULL) {
444 cpuinfo_log_error("failed to allocate %zu bytes for descriptions of %"PRIu32" L3 caches",
445 l3_count * sizeof(struct cpuinfo_cache), l3_count);
446 goto cleanup;
447 }
448 for (uint32_t c = 0; c < l3_count; c++) {
449 l3[c] = (struct cpuinfo_cache) {
450 .size = l3_cache_size,
451 .associativity = l3_cache_associativity,
452 .sets = l3_cache_size / (l3_cache_associativity * cacheline_size),
453 .partitions = cache_partitions,
454 .line_size = cacheline_size,
455 .flags = cache_flags,
456 .processor_start = c * threads_per_l3,
457 .processor_count = threads_per_l3,
458 };
459 }
460 for (uint32_t t = 0; t < mach_topology.threads; t++) {
461 processors[t].cache.l3 = &l3[0];
462 }
463 }
464
465 /* Commit changes */
466 cpuinfo_cache[cpuinfo_cache_level_1i] = l1i;
467 cpuinfo_cache[cpuinfo_cache_level_1d] = l1d;
468 cpuinfo_cache[cpuinfo_cache_level_2] = l2;
469 cpuinfo_cache[cpuinfo_cache_level_3] = l3;
470
471 cpuinfo_processors = processors;
472 cpuinfo_cores = cores;
473 cpuinfo_packages = packages;
474
475 cpuinfo_cache_count[cpuinfo_cache_level_1i] = l1_count;
476 cpuinfo_cache_count[cpuinfo_cache_level_1d] = l1_count;
477 cpuinfo_cache_count[cpuinfo_cache_level_2] = l2_count;
478 cpuinfo_cache_count[cpuinfo_cache_level_3] = l3_count;
479
480 cpuinfo_processors_count = mach_topology.threads;
481 cpuinfo_cores_count = mach_topology.cores;
482 cpuinfo_packages_count = mach_topology.packages;
483
484 processors = NULL;
485 cores = NULL;
486 packages = NULL;
487 l1i = l1d = l2 = l3 = NULL;
488
489cleanup:
490 free(processors);
491 free(cores);
492 free(packages);
493 free(l1i);
494 free(l1d);
495 free(l2);
496 free(l3);
497}