blob: 8141d605ac203f00850448b6a69387c79ade6415 [file] [log] [blame]
Marat Dukhan006461a2017-08-24 16:10:46 -07001#include <stdbool.h>
2#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5
Marat Dukhan3c634552017-10-16 16:46:45 +00006#include <arm/linux/api.h>
7#ifdef __ANDROID__
8 #include <arm/android/api.h>
9#endif
Marat Dukhan006461a2017-08-24 16:10:46 -070010#include <log.h>
11
12
13#define CPUINFO_COUNT_OF(x) (sizeof(x) / sizeof(0[x]))
14
15
16static inline bool is_ascii_whitespace(char c) {
17 switch (c) {
18 case ' ':
19 case '\t':
20 case '\r':
21 case '\n':
22 return true;
23 default:
24 return false;
25 }
26}
27
28static inline bool is_ascii_alphabetic(char c) {
29 const char lower_c = c | '\x20';
30 return (uint8_t) (lower_c - 'a') <= (uint8_t) ('z' - 'a');
31}
32
33static inline bool is_ascii_alphabetic_uppercase(char c) {
34 return (uint8_t) (c - 'A') <= (uint8_t) ('Z' - 'A');
35}
36
37static inline bool is_ascii_numeric(char c) {
38 return (uint8_t) (c - '0') < 10;
39}
40
Marat Dukhan006461a2017-08-24 16:10:46 -070041static inline uint16_t load_u16le(const void* ptr) {
42#if defined(__ARM_ARCH_7A__) || defined(__aarch64__)
43 return *((const uint16_t*) ptr);
44#else
45 const uint8_t* byte_ptr = (const uint8_t*) ptr;
46 return ((uint16_t) byte_ptr[1] << 8) | (uint16_t) byte_ptr[0];
47#endif
48}
49
50static inline uint32_t load_u24le(const void* ptr) {
51#if defined(__ARM_ARCH_7A__) || defined(__aarch64__)
52 return ((uint32_t) ((const uint8_t*) ptr)[2] << 16) | ((uint32_t) *((const uint16_t*) ptr));
53#else
54 const uint8_t* byte_ptr = (const uint8_t*) ptr;
55 return ((uint32_t) byte_ptr[2] << 16) | ((uint32_t) byte_ptr[1] << 8) | (uint32_t) byte_ptr[0];
56#endif
57}
58
59static inline uint32_t load_u32le(const void* ptr) {
60#if defined(__ARM_ARCH_7A__) || defined(__aarch64__)
61 return *((const uint32_t*) ptr);
62#else
63 return ((uint32_t) ((const uint8_t*) ptr)[3] << 24) | load_u24le(ptr);
64#endif
65}
66
67/*
68 * Map from ARM chipset series ID to ARM chipset vendor ID.
69 * This map is used to avoid storing vendor IDs in tables.
70 */
71static enum cpuinfo_arm_chipset_vendor chipset_series_vendor[cpuinfo_arm_chipset_series_max] = {
72 [cpuinfo_arm_chipset_series_unknown] = cpuinfo_arm_chipset_vendor_unknown,
73 [cpuinfo_arm_chipset_series_qualcomm_qsd] = cpuinfo_arm_chipset_vendor_qualcomm,
74 [cpuinfo_arm_chipset_series_qualcomm_msm] = cpuinfo_arm_chipset_vendor_qualcomm,
75 [cpuinfo_arm_chipset_series_qualcomm_apq] = cpuinfo_arm_chipset_vendor_qualcomm,
76 [cpuinfo_arm_chipset_series_qualcomm_snapdragon] = cpuinfo_arm_chipset_vendor_qualcomm,
77 [cpuinfo_arm_chipset_series_mediatek_mt] = cpuinfo_arm_chipset_vendor_mediatek,
78 [cpuinfo_arm_chipset_series_samsung_exynos] = cpuinfo_arm_chipset_vendor_samsung,
79 [cpuinfo_arm_chipset_series_hisilicon_k3v] = cpuinfo_arm_chipset_vendor_hisilicon,
80 [cpuinfo_arm_chipset_series_hisilicon_hi] = cpuinfo_arm_chipset_vendor_hisilicon,
81 [cpuinfo_arm_chipset_series_hisilicon_kirin] = cpuinfo_arm_chipset_vendor_hisilicon,
82 [cpuinfo_arm_chipset_series_actions_atm] = cpuinfo_arm_chipset_vendor_actions,
83 [cpuinfo_arm_chipset_series_allwinner_a] = cpuinfo_arm_chipset_vendor_allwinner,
84 [cpuinfo_arm_chipset_series_amlogic_aml] = cpuinfo_arm_chipset_vendor_amlogic,
85 [cpuinfo_arm_chipset_series_amlogic_s] = cpuinfo_arm_chipset_vendor_amlogic,
86 [cpuinfo_arm_chipset_series_broadcom_bcm] = cpuinfo_arm_chipset_vendor_broadcom,
87 [cpuinfo_arm_chipset_series_lg_nuclun] = cpuinfo_arm_chipset_vendor_lg,
88 [cpuinfo_arm_chipset_series_leadcore_lc] = cpuinfo_arm_chipset_vendor_leadcore,
89 [cpuinfo_arm_chipset_series_marvell_pxa] = cpuinfo_arm_chipset_vendor_marvell,
90 [cpuinfo_arm_chipset_series_mstar_6a] = cpuinfo_arm_chipset_vendor_mstar,
91 [cpuinfo_arm_chipset_series_novathor_u] = cpuinfo_arm_chipset_vendor_novathor,
92 [cpuinfo_arm_chipset_series_nvidia_tegra_t] = cpuinfo_arm_chipset_vendor_nvidia,
93 [cpuinfo_arm_chipset_series_nvidia_tegra_ap] = cpuinfo_arm_chipset_vendor_nvidia,
94 [cpuinfo_arm_chipset_series_nvidia_tegra_sl] = cpuinfo_arm_chipset_vendor_nvidia,
95 [cpuinfo_arm_chipset_series_pinecone_surge_s] = cpuinfo_arm_chipset_vendor_pinecone,
96 [cpuinfo_arm_chipset_series_renesas_mp] = cpuinfo_arm_chipset_vendor_renesas,
97 [cpuinfo_arm_chipset_series_rockchip_rk] = cpuinfo_arm_chipset_vendor_rockchip,
98 [cpuinfo_arm_chipset_series_spreadtrum_sc] = cpuinfo_arm_chipset_vendor_spreadtrum,
99 [cpuinfo_arm_chipset_series_telechips_tcc] = cpuinfo_arm_chipset_vendor_telechips,
100 [cpuinfo_arm_chipset_series_texas_instruments_omap] = cpuinfo_arm_chipset_vendor_texas_instruments,
101 [cpuinfo_arm_chipset_series_wondermedia_wm] = cpuinfo_arm_chipset_vendor_wondermedia,
102};
103
104/**
105 * Tries to match /(MSM|APQ)\d{4}([A-Z\-]*)/ signature (case-insensitive) for Qualcomm MSM and APQ chipsets.
106 * If match successful, extracts model information into \p chipset argument.
107 *
108 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform
109 * or ro.chipname) to match.
110 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform or
111 * ro.chipname) to match.
112 * @param[out] chipset - location where chipset information will be stored upon a successful match.
113 *
114 * @returns true if signature matched, false otherwise.
115 */
116static bool match_msm_apq(
117 const char* start, const char* end,
118 struct cpuinfo_arm_chipset chipset[restrict static 1])
119{
120 /* Expect at least 7 symbols: 3 symbols "MSM" or "APQ" + 4 digits */
121 if (start + 7 > end) {
122 return false;
123 }
124
125 /* Check that string starts with "MSM" or "APQ", case-insensitive.
126 * The first three characters are loaded as 24-bit little endian word, binary ORed with 0x20 to convert to lower
127 * case, and compared to "MSM" and "APQ" strings as integers.
128 */
129 const uint32_t series_signature = UINT32_C(0x00202020) | load_u24le(start);
130 enum cpuinfo_arm_chipset_series series;
131 switch (series_signature) {
132 case UINT32_C(0x6D736D): /* "msm" = reverse("msm") */
133 series = cpuinfo_arm_chipset_series_qualcomm_msm;
134 break;
135 case UINT32_C(0x717061): /* "qpa" = reverse("apq") */
136 series = cpuinfo_arm_chipset_series_qualcomm_apq;
137 break;
138 default:
139 return false;
140 }
141
142 /* Sometimes there is a space ' ' following the MSM/APQ series */
143 const char* pos = start + 3;
144 if (*pos == ' ') {
145 pos++;
146
147 /* Expect at least 4 more symbols (4-digit model number) */
148 if (pos + 4 > end) {
149 return false;
150 }
151 }
152
153 /* Validate and parse 4-digit model number */
154 uint32_t model = 0;
155 for (uint32_t i = 0; i < 4; i++) {
156 const uint32_t digit = (uint32_t) (uint8_t) (*pos++) - '0';
157 if (digit >= 10) {
158 /* Not really a digit */
159 return false;
160 }
161 model = model * 10 + digit;
162 }
163
164 /* Suffix is optional, so if we got to this point, parsing is successful. Commit parsed chipset. */
165 *chipset = (struct cpuinfo_arm_chipset) {
166 .vendor = cpuinfo_arm_chipset_vendor_qualcomm,
167 .series = series,
168 .model = model,
169 };
170
171 /* Parse as many suffix characters as match the pattern [A-Za-z\-] */
172 for (uint32_t i = 0; i < CPUINFO_ARM_CHIPSET_SUFFIX_MAX; i++) {
173 if (pos + i == end) {
174 break;
175 }
176
177 const char c = pos[i];
178 if (is_ascii_alphabetic(c)) {
179 /* Matched a letter [A-Za-z] */
180 chipset->suffix[i] = c & '\xDF';
181 } else if (c == '-') {
182 /* Matched a dash '-' */
183 chipset->suffix[i] = c;
184 } else {
185 /* Neither of [A-Za-z\-] */
186 break;
187 }
188 }
189 return true;
190}
191
192/**
193 * Tries to match /SDM\d{3}$/ signature for Qualcomm Snapdragon chipsets.
194 * If match successful, extracts model information into \p chipset argument.
195 *
196 * @param start - start of the /proc/cpuinfo Hardware string to match.
197 * @param end - end of the /proc/cpuinfo Hardware string to match.
198 * @param[out] chipset - location where chipset information will be stored upon a successful match.
199 *
200 * @returns true if signature matched, false otherwise.
201 */
202static bool match_sdm(
203 const char* start, const char* end,
204 struct cpuinfo_arm_chipset chipset[restrict static 1])
205{
206 /* Expect exactly 6 symbols: 3 symbols "SDM" + 3 digits */
207 if (start + 6 != end) {
208 return false;
209 }
210
211 /* Check that string starts with "SDM".
212 * The first three characters are loaded and compared as 24-bit little endian word.
213 */
214 const uint32_t expected_sdm = load_u24le(start);
215 if (expected_sdm != UINT32_C(0x004D4453) /* "MDS" = reverse("SDM") */) {
216 return false;
217 }
218
219 /* Validate and parse 3-digit model number */
220 uint32_t model = 0;
221 for (uint32_t i = 3; i < 6; i++) {
222 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
223 if (digit >= 10) {
224 /* Not really a digit */
225 return false;
226 }
227 model = model * 10 + digit;
228 }
229
230 /* Return parsed chipset. */
231 *chipset = (struct cpuinfo_arm_chipset) {
232 .vendor = cpuinfo_arm_chipset_vendor_qualcomm,
233 .series = cpuinfo_arm_chipset_series_qualcomm_snapdragon,
234 .model = model,
235 };
236 return true;
237}
238
239/**
240 * Tries to match /Samsung Exynos\d{4}$/ signature (case-insensitive) for Samsung Exynos chipsets.
241 * If match successful, extracts model information into \p chipset argument.
242 *
243 * @param start - start of the /proc/cpuinfo Hardware string to match.
244 * @param end - end of the /proc/cpuinfo Hardware string to match.
245 * @param[out] chipset - location where chipset information will be stored upon a successful match.
246 *
247 * @returns true if signature matched, false otherwise.
248 */
249static bool match_samsung_exynos(
250 const char* start, const char* end,
251 struct cpuinfo_arm_chipset chipset[restrict static 1])
252{
253 /*
254 * Expect at 18-19 symbols:
255 * - "Samsung" (7 symbols) + space + "Exynos" (6 symbols) + optional space 4-digit model number
256 */
257 const size_t length = end - start;
258 switch (length) {
259 case 18:
260 case 19:
261 break;
262 default:
263 return false;
264 }
265
266 /*
267 * Check that the string starts with "samsung exynos", case-insensitive.
268 * Blocks of 4 characters are loaded and compared as little-endian 32-bit word.
269 * Case-insensitive characters are binary ORed with 0x20 to convert them to lowercase.
270 */
271 const uint32_t expected_sams = UINT32_C(0x20202000) | load_u32le(start);
272 if (expected_sams != UINT32_C(0x736D6153) /* "smaS" = reverse("Sams") */) {
273 return false;
274 }
275 const uint32_t expected_ung = UINT32_C(0x00202020) | load_u32le(start + 4);
276 if (expected_ung != UINT32_C(0x20676E75) /* " ung" = reverse("ung ") */) {
277 return false;
278 }
279 const uint32_t expected_exyn = UINT32_C(0x20202000) | load_u32le(start + 8);
280 if (expected_exyn != UINT32_C(0x6E797845) /* "nyxE" = reverse("Exyn") */) {
281 return false;
282 }
283 const uint16_t expected_os = UINT16_C(0x2020) | load_u16le(start + 12);
284 if (expected_os != UINT16_C(0x736F) /* "so" = reverse("os") */) {
285 return false;
286 }
287
288 const char* pos = start + 14;
289
290 /* There can be a space ' ' following the "Exynos" string */
291 if (*pos == ' ') {
292 pos++;
293
294 /* If optional space if present, we expect exactly 19 characters */
295 if (length != 19) {
296 return false;
297 }
298 }
299
300 /* Validate and parse 4-digit model number */
301 uint32_t model = 0;
302 for (uint32_t i = 0; i < 4; i++) {
303 const uint32_t digit = (uint32_t) (uint8_t) (*pos++) - '0';
304 if (digit >= 10) {
305 /* Not really a digit */
306 return start;
307 }
308 model = model * 10 + digit;
309 }
310
311 /* Return parsed chipset */
312 *chipset = (struct cpuinfo_arm_chipset) {
313 .vendor = cpuinfo_arm_chipset_vendor_samsung,
314 .series = cpuinfo_arm_chipset_series_samsung_exynos,
315 .model = model,
316 };
317 return pos;
318}
319
320/**
321 * Tries to match /exynos\d{4}$/ signature for Samsung Exynos chipsets.
322 * If match successful, extracts model information into \p chipset argument.
323 *
324 * @param start - start of the platform identifier (ro.board.platform or ro.chipname) to match.
325 * @param end - end of the platform identifier (ro.board.platform or ro.chipname) to match.
326 * @param[out] chipset - location where chipset information will be stored upon a successful match.
327 *
328 * @returns true if signature matched, false otherwise.
329 */
330static bool match_exynos(
331 const char* start, const char* end,
332 struct cpuinfo_arm_chipset chipset[restrict static 1])
333{
334 /* Expect exactly 10 symbols: "exynos" (6 symbols) + 4-digit model number */
335 if (start + 10 != end) {
336 return false;
337 }
338
339 /* Load first 4 bytes as little endian 32-bit word */
340 const uint32_t expected_exyn = load_u32le(start);
341 if (expected_exyn != UINT32_C(0x6E797865) /* "nyxe" = reverse("exyn") */ ) {
342 return false;
343 }
344
345 /* Load next 2 bytes as little endian 16-bit word */
346 const uint16_t expected_os = load_u16le(start + 4);
347 if (expected_os != UINT16_C(0x736F) /* "so" = reverse("os") */ ) {
348 return false;
349 }
350
351 /* Check and parse 4-digit model number */
352 uint32_t model = 0;
353 for (uint32_t i = 6; i < 10; i++) {
354 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
355 if (digit >= 10) {
356 /* Not really a digit */
357 return false;
358 }
359 model = model * 10 + digit;
360 }
361
362 /* Return parsed chipset. */
363 *chipset = (struct cpuinfo_arm_chipset) {
364 .vendor = cpuinfo_arm_chipset_vendor_samsung,
365 .series = cpuinfo_arm_chipset_series_samsung_exynos,
366 .model = model,
367 };
368 return true;
369}
370
371/**
372 * Tries to match /universal\d{4}$/ signature for Samsung Exynos chipsets.
373 * If match successful, extracts model information into \p chipset argument.
374 *
375 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board or ro.chipname)
376 * to match.
377 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board or ro.chipname)
378 * to match.
379 * @param[out] chipset - location where chipset information will be stored upon a successful match.
380 *
381 * @returns true if signature matched, false otherwise.
382 */
383static bool match_universal(
384 const char* start, const char* end,
385 struct cpuinfo_arm_chipset chipset[restrict static 1])
386{
387 /* Expect exactly 13 symbols: "universal" (9 symbols) + 4-digit model number */
388 if (start + 13 != end) {
389 return false;
390 }
391
392 /*
393 * Check that the string starts with "universal".
394 * Blocks of 4 characters are loaded and compared as little-endian 32-bit word.
395 * Case-insensitive characters are binary ORed with 0x20 to convert them to lowercase.
396 */
397 const uint8_t expected_u = UINT8_C(0x20) | (uint8_t) start[0];
398 if (expected_u != UINT8_C(0x75) /* "u" */) {
399 return false;
400 }
401 const uint32_t expected_nive = UINT32_C(0x20202020) | load_u32le(start + 1);
402 if (expected_nive != UINT32_C(0x6576696E) /* "evin" = reverse("nive") */ ) {
403 return false;
404 }
405 const uint32_t expected_ersa = UINT32_C(0x20202020) | load_u32le(start + 5);
406 if (expected_ersa != UINT32_C(0x6C617372) /* "lasr" = reverse("rsal") */) {
407 return false;
408 }
409
410 /* Validate and parse 4-digit model number */
411 uint32_t model = 0;
412 for (uint32_t i = 9; i < 13; i++) {
413 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
414 if (digit >= 10) {
415 /* Not really a digit */
416 return false;
417 }
418 model = model * 10 + digit;
419 }
420
421 /* Return parsed chipset. */
422 *chipset = (struct cpuinfo_arm_chipset) {
423 .vendor = cpuinfo_arm_chipset_vendor_samsung,
424 .series = cpuinfo_arm_chipset_series_samsung_exynos,
425 .model = model,
426 };
427 return true;
428}
429
430/**
431 * Compares, case insensitively, a string to known values "SMDK4210" and "SMDK4x12" for Samsung Exynos chipsets.
432 * If platform identifier matches one of the SMDK* values, extracts model information into \p chipset argument.
433 * For "SMDK4x12" match, decodes the chipset name using number of cores.
434 *
435 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string or ro.product.board) to match.
436 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string or ro.product.board) to match.
437 * @param cores - number of cores in the chipset.
438 * @param[out] chipset - location where chipset information will be stored upon a successful match.
439 *
440 * @returns true if signature matched, false otherwise.
441 */
442static bool match_and_parse_smdk(
443 const char* start, const char* end, uint32_t cores,
444 struct cpuinfo_arm_chipset chipset[restrict static 1])
445{
446 /* Expect exactly 8 symbols: "SMDK" (4 symbols) + 4-digit model number */
447 if (start + 8 != end) {
448 return false;
449 }
450
451 /*
452 * Check that string starts with "MT" (case-insensitive).
453 * The first four characters are loaded as a 32-bit little endian word and converted to lowercase.
454 */
455 const uint32_t expected_smdk = UINT32_C(0x20202020) | load_u32le(start);
456 if (expected_smdk != UINT32_C(0x6B646D73) /* "kdms" = reverse("smdk") */) {
457 return false;
458 }
459
460 /*
461 * Check that string ends with "4210" or "4x12".
462 * The last four characters are loaded and compared as a 32-bit little endian word.
463 */
464 uint32_t model = 0;
465 const uint32_t expected_model = load_u32le(start + 4);
466 switch (expected_model) {
467 case UINT32_C(0x30313234): /* "0124" = reverse("4210") */
468 model = 4210;
469 break;
470 case UINT32_C(0x32317834): /* "21x4" = reverse("4x12") */
471 switch (cores) {
472 case 2:
473 model = 4212;
474 break;
475 case 4:
476 model = 4412;
477 break;
478 default:
479 cpuinfo_log_warning("system reported invalid %"PRIu32"-core Exynos 4x12 chipset", cores);
480 }
481 }
482
483 if (model == 0) {
484 return false;
485 }
486
487 *chipset = (struct cpuinfo_arm_chipset) {
488 .vendor = cpuinfo_arm_chipset_vendor_samsung,
489 .series = cpuinfo_arm_chipset_series_samsung_exynos,
490 .model = model,
491 };
492 return true;
493}
494
495/**
496 * Tries to match /MTK?\d{4}[A-Z]*$/ signature for MediaTek MT chipsets.
497 * If match successful, extracts model information into \p chipset argument.
498 *
499 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform,
500 * ro.mediatek.platform, or ro.chipname) to match.
501 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform,
502 * ro.mediatek.platform, or ro.chipname) to match.
503 * @param match_end - indicates if the function should attempt to match through the end of the string and fail if there
504 * are unparsed characters in the end, or match only MTK signature, model number, and some of the
505 * suffix characters (the ones that pass validation).
506 * @param[out] chipset - location where chipset information will be stored upon a successful match.
507 *
508 * @returns true if signature matched, false otherwise.
509 */
510static bool match_mt(
511 const char* start, const char* end, bool match_end,
512 struct cpuinfo_arm_chipset chipset[restrict static 1])
513{
514 /* Expect at least 6 symbols: "MT" (2 symbols) + 4-digit model number */
515 if (start + 6 > end) {
516 return false;
517 }
518
519 /*
520 * Check that string starts with "MT" (case-insensitive).
521 * The first two characters are loaded as 16-bit little endian word and converted to lowercase.
522 */
523 const uint16_t mt = UINT16_C(0x2020) | load_u16le(start);
524 if (mt != UINT16_C(0x746D) /* "tm" */) {
525 return false;
526 }
527
528
529 /* Some images report "MTK" rather than "MT" */
530 const char* pos = start + 2;
531 if (((uint8_t) *pos | UINT8_C(0x20)) == (uint8_t) 'k') {
532 pos++;
533
534 /* Expect 4 more symbols after "MTK" (4-digit model number) */
535 if (pos + 4 > end) {
536 return false;
537 }
538 }
539
540 /* Validate and parse 4-digit model number */
541 uint32_t model = 0;
542 for (uint32_t i = 0; i < 4; i++) {
543 const uint32_t digit = (uint32_t) (uint8_t) (*pos++) - '0';
544 if (digit >= 10) {
545 /* Not really a digit */
546 return false;
547 }
548 model = model * 10 + digit;
549 }
550
551 /* Record parsed chipset. This implicitly zeroes-out suffix, which will be parsed later. */
552 *chipset = (struct cpuinfo_arm_chipset) {
553 .vendor = cpuinfo_arm_chipset_vendor_mediatek,
554 .series = cpuinfo_arm_chipset_series_mediatek_mt,
555 .model = model,
556 };
557
558 if (match_end) {
559 /* Check that the potential suffix does not exceed maximum length */
560 const size_t suffix_length = end - pos;
561 if (suffix_length > CPUINFO_ARM_CHIPSET_SUFFIX_MAX) {
562 return false;
563 }
564
565 /* Validate suffix characters and copy them to chipset structure */
566 for (size_t i = 0; i < suffix_length; i++) {
567 const char c = (*pos++);
568 if (is_ascii_alphabetic(c)) {
569 /* Matched a letter [A-Za-z], convert to uppercase */
570 chipset->suffix[i] = c & '\xDF';
571 } else if (c == '/') {
572 /* Matched a slash '/' */
573 chipset->suffix[i] = c;
574 } else {
575 /* Invalid suffix character (neither of [A-Za-z/]) */
576 return false;
577 }
578 }
579 } else {
580 /* Validate and parse as many suffix characters as we can */
581 for (size_t i = 0; i < CPUINFO_ARM_CHIPSET_SUFFIX_MAX; i++) {
582 if (pos + i == end) {
583 break;
584 }
585
586 const char c = pos[i];
587 if (is_ascii_alphabetic(c)) {
588 /* Matched a letter [A-Za-z], convert to uppercase */
589 chipset->suffix[i] = c & '\xDF';
590 } else if (c == '/') {
591 /* Matched a slash '/' */
592 chipset->suffix[i] = c;
593 } else {
594 /* Invalid suffix character (neither of [A-Za-z/]). This marks the end of the suffix. */
595 break;
596 }
597 }
598 }
599 /* All suffix characters successfully validated and copied to chipset data */
600 return true;
601}
602
603/**
Marat Dukhan166ce4c2017-11-27 14:54:43 -0800604 * Tries to match /[Kk]irin\s?\d{3}$/ signature for HiSilicon Kirin chipsets.
Marat Dukhan006461a2017-08-24 16:10:46 -0700605 * If match successful, extracts model information into \p chipset argument.
606 *
607 * @param start - start of the /proc/cpuinfo Hardware string to match.
608 * @param end - end of the /proc/cpuinfo Hardware string to match.
609 * @param[out] chipset - location where chipset information will be stored upon a successful match.
610 *
611 * @returns true if signature matched, false otherwise.
612 */
613static bool match_kirin(
614 const char* start, const char* end,
615 struct cpuinfo_arm_chipset chipset[restrict static 1])
616{
617 /* Expect 8-9 symbols: "Kirin" (5 symbols) + optional whitespace (1 symbol) + 3-digit model number */
618 const size_t length = end - start;
619 switch (length) {
620 case 8:
621 case 9:
622 break;
623 default:
624 return false;
625 }
626
Marat Dukhan166ce4c2017-11-27 14:54:43 -0800627 /* Check that the string starts with "Kirin" or "kirin". */
628 if (((uint8_t) start[0] | UINT8_C(0x20)) != (uint8_t) 'k') {
Marat Dukhan006461a2017-08-24 16:10:46 -0700629 return false;
630 }
Marat Dukhan166ce4c2017-11-27 14:54:43 -0800631 /* Symbols 1-5 are loaded and compared as little-endian 32-bit word. */
Marat Dukhan006461a2017-08-24 16:10:46 -0700632 const uint32_t irin = load_u32le(start + 1);
633 if (irin != UINT32_C(0x6E697269) /* "niri" = reverse("irin") */) {
634 return false;
635 }
636
637 /* Check for optional whitespace after "Kirin" */
638 if (is_ascii_whitespace(start[5])) {
639 /* When whitespace is present after "Kirin", expect 9 symbols total */
640 if (length != 9) {
641 return false;
642 }
643 }
644
645 /* Validate and parse 3-digit model number */
646 uint32_t model = 0;
647 for (int32_t i = 0; i < 3; i++) {
648 const uint32_t digit = (uint32_t) (uint8_t) end[i - 3] - '0';
649 if (digit >= 10) {
650 /* Not really a digit */
651 return start;
652 }
653 model = model * 10 + digit;
654 }
655
656 /*
657 * Thats it, return parsed chipset.
658 * Technically, Kirin 910T has a suffix, but it never appears in the form of "910T" string.
659 * Instead, Kirin 910T devices report "hi6620oem" string (handled outside of this function).
660 */
661 *chipset = (struct cpuinfo_arm_chipset) {
662 .vendor = cpuinfo_arm_chipset_vendor_hisilicon,
663 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
664 .model = model,
665 };
666 return true;
667}
668
669/**
670 * Tries to match /rk\d{4}[a-z]?$/ signature for Rockchip RK chipsets.
671 * If match successful, extracts model information into \p chipset argument.
672 *
673 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string or ro.board.platform) to match.
674 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string or ro.board.platform) to match.
675 * @param[out] chipset - location where chipset information will be stored upon a successful match.
676 *
677 * @returns true if signature matched, false otherwise.
678 */
679static bool match_rk(
680 const char* start, const char* end,
681 struct cpuinfo_arm_chipset chipset[restrict static 1])
682{
683 /* Expect 6-7 symbols: "RK" (2 symbols) + 4-digit model number + optional 1-letter suffix */
684 const size_t length = end - start;
685 switch (length) {
686 case 6:
687 case 7:
688 break;
689 default:
690 return false;
691 }
692
693 /*
694 * Check that string starts with "RK" (case-insensitive).
695 * The first two characters are loaded as 16-bit little endian word and converted to lowercase.
696 */
697 const uint16_t expected_rk = UINT16_C(0x2020) | load_u16le(start);
698 if (expected_rk != UINT16_C(0x6B72) /* "kr" = reverse("rk") */) {
699 return false;
700 }
701
702 /* Validate and parse 4-digit model number */
703 uint32_t model = 0;
704 for (uint32_t i = 2; i < 6; i++) {
705 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
706 if (digit >= 10) {
707 /* Not really a digit */
708 return start;
709 }
710 model = model * 10 + digit;
711 }
712
713 /* Parse optional suffix */
714 char suffix = 0;
715 if (length == 7) {
716 /* Parse the suffix letter */
717 const char c = start[6];
718 if (is_ascii_alphabetic(c)) {
719 /* Convert to upper case */
720 suffix = c & '\xDF';
721 } else {
722 /* Invalid suffix character */
723 return false;
724 }
725 }
726
727 /* Return parsed chipset */
728 *chipset = (struct cpuinfo_arm_chipset) {
729 .vendor = cpuinfo_arm_chipset_vendor_rockchip,
730 .series = cpuinfo_arm_chipset_series_rockchip_rk,
731 .model = model,
732 .suffix = {
733 [0] = suffix,
734 },
735 };
736 return true;
737}
738
739/**
740 * Tries to match, case-insentitively, /sc\d{4}[a-z]*|scx15$/ signature for Spreadtrum SC chipsets.
741 * If match successful, extracts model information into \p chipset argument.
742 *
743 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board,
744 * ro.board.platform, or ro.chipname) to match.
745 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board,
746 * ro.board.platform, or ro.chipname) to match.
747 * @param[out] chipset - location where chipset information will be stored upon a successful match.
748 *
749 * @returns true if signature matched, false otherwise.
750 */
751static bool match_sc(
752 const char* start, const char* end,
753 struct cpuinfo_arm_chipset chipset[restrict static 1])
754{
755 /* Expect at least 5 symbols: "scx15" */
756 if (start + 5 > end) {
757 return false;
758 }
759
760 /*
761 * Check that string starts with "SC" (case-insensitive).
762 * The first two characters are loaded as 16-bit little endian word and converted to lowercase.
763 */
764 const uint16_t expected_sc = UINT16_C(0x2020) | load_u16le(start);
765 if (expected_sc != UINT16_C(0x6373) /* "cs" = reverse("sc") */) {
766 return false;
767 }
768
769 /* Special case: "scx" prefix (SC7715 reported as "scx15") */
770 if ((start[2] | '\x20') == 'x') {
771 /* Expect exactly 5 characters: "scx15" */
772 if (start + 5 != end) {
773 return false;
774 }
775
776 /* Check that string ends with "15" */
777 const uint16_t expected_15 = load_u16le(start + 3);
778 if (expected_15 != UINT16_C(0x3531) /* "51" = reverse("15") */ ) {
779 return false;
780 }
781
782 *chipset = (struct cpuinfo_arm_chipset) {
783 .vendor = cpuinfo_arm_chipset_vendor_spreadtrum,
784 .series = cpuinfo_arm_chipset_series_spreadtrum_sc,
785 .model = 7715,
786 };
787 return true;
788 }
789
790 /* Expect at least 6 symbols: "SC" (2 symbols) + 4-digit model number */
791 if (start + 6 > end) {
792 return false;
793 }
794
795 /* Validate and parse 4-digit model number */
796 uint32_t model = 0;
797 for (uint32_t i = 2; i < 6; i++) {
798 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
799 if (digit >= 10) {
800 /* Not really a digit */
801 return false;
802 }
803 model = model * 10 + digit;
804 }
805
806 /* Write parsed chipset */
807 *chipset = (struct cpuinfo_arm_chipset) {
808 .vendor = cpuinfo_arm_chipset_vendor_spreadtrum,
809 .series = cpuinfo_arm_chipset_series_spreadtrum_sc,
810 .model = model,
811 };
812
813 /* Validate and copy suffix letters. If suffix is too long, truncate at CPUINFO_ARM_CHIPSET_SUFFIX_MAX letters. */
814 const char* suffix = start + 6;
815 for (size_t i = 0; i < CPUINFO_ARM_CHIPSET_SUFFIX_MAX; i++) {
816 if (suffix + i == end) {
817 break;
818 }
819
820 const char c = suffix[i];
821 if (!is_ascii_alphabetic(c)) {
822 /* Invalid suffix character */
823 return false;
824 }
825 /* Convert suffix letter to uppercase */
826 chipset->suffix[i] = c & '\xDF';
827 }
828 return true;
829}
830
831/**
832 * Tries to match /lc\d{4}[a-z]?$/ signature for Leadcore LC chipsets.
833 * If match successful, extracts model information into \p chipset argument.
834 *
835 * @param start - start of the platform identifier (ro.product.board or ro.board.platform) to match.
836 * @param end - end of the platform identifier (ro.product.board or ro.board.platform) to match.
837 * @param[out] chipset - location where chipset information will be stored upon a successful match.
838 *
839 * @returns true if signature matched, false otherwise.
840 */
841static bool match_lc(
842 const char* start, const char* end,
843 struct cpuinfo_arm_chipset chipset[restrict static 1])
844{
845 /* Expect at 6-7 symbols: "lc" (2 symbols) + 4-digit model number + optional 1-letter suffix */
846 const size_t length = end - start;
847 switch (length) {
848 case 6:
849 case 7:
850 break;
851 default:
852 return false;
853 }
854
855 /* Check that string starts with "lc". The first two characters are loaded as 16-bit little endian word */
856 const uint16_t expected_lc = load_u16le(start);
857 if (expected_lc != UINT16_C(0x636C) /* "cl" = reverse("lc") */) {
858 return false;
859 }
860
861 /* Validate and parse 4-digit model number */
862 uint32_t model = 0;
863 for (uint32_t i = 2; i < 6; i++) {
864 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
865 if (digit >= 10) {
866 /* Not really a digit */
867 return false;
868 }
869 model = model * 10 + digit;
870 }
871
872 /* Parse optional suffix letter */
873 char suffix = 0;
874 if (length == 7) {
875 const char c = start[6];
876 if (is_ascii_alphabetic(c)) {
877 /* Convert to uppercase */
878 chipset->suffix[0] = c & '\xDF';
879 } else {
880 /* Invalid suffix character */
881 return false;
882 }
883 }
884
885 /* Return parsed chipset */
886 *chipset = (struct cpuinfo_arm_chipset) {
887 .vendor = cpuinfo_arm_chipset_vendor_leadcore,
888 .series = cpuinfo_arm_chipset_series_leadcore_lc,
889 .model = model,
890 .suffix = {
891 [0] = suffix,
892 },
893 };
894 return true;
895}
896
897/**
898 * Tries to match /PXA(\d{3,4}|1L88)$/ signature for Marvell PXA chipsets.
899 * If match successful, extracts model information into \p chipset argument.
900 *
901 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board or ro.chipname)
902 * to match.
903 * @param end - end of the platform identifier (/proc/cpuinfo Hardaware string, ro.product.board or ro.chipname) to
904 * match.
905 * @param[out] chipset - location where chipset information will be stored upon a successful match.
906 *
907 * @returns true if signature matched, false otherwise.
908 */
909static bool match_pxa(
910 const char* start, const char* end,
911 struct cpuinfo_arm_chipset chipset[restrict static 1])
912{
913 /* Expect 6-7 symbols: "PXA" (3 symbols) + 3-4 digit model number */
914 const size_t length = end - start;
915 switch (length) {
916 case 6:
917 case 7:
918 break;
919 default:
920 return false;
921 }
922
923 /* Check that the string starts with "PXA". Symbols 1-3 are loaded and compared as little-endian 16-bit word. */
924 if (start[0] != 'P') {
925 return false;
926 }
927 const uint16_t expected_xa = load_u16le(start + 1);
928 if (expected_xa != UINT16_C(0x4158) /* "AX" = reverse("XA") */) {
929 return false;
930 }
931
932 uint32_t model = 0;
933
934
935 /* Check for a very common typo: "PXA1L88" for "PXA1088" */
936 if (length == 7) {
937 /* Load 4 model "number" symbols as a little endian 32-bit word and compare to "1L88" */
938 const uint32_t expected_1L88 = load_u32le(start + 3);
939 if (expected_1L88 == UINT32_C(0x38384C31) /* "88L1" = reverse("1L88") */) {
940 model = 1088;
941 goto write_chipset;
942 }
943 }
944
945 /* Check and parse 3-4 digit model number */
946 for (uint32_t i = 3; i < length; i++) {
947 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
948 if (digit >= 10) {
949 /* Not really a digit */
950 return false;
951 }
952 model = model * 10 + digit;
953 }
954
955 /* Return parsed chipset. */
956write_chipset:
957 *chipset = (struct cpuinfo_arm_chipset) {
958 .vendor = cpuinfo_arm_chipset_vendor_marvell,
959 .series = cpuinfo_arm_chipset_series_marvell_pxa,
960 .model = model,
961 };
962 return true;
963}
964
965/**
966 * Tries to match /OMAP\d{4}$/ signature for Texas Instruments OMAP chipsets.
967 * If match successful, extracts model information into \p chipset argument.
968 *
969 * @param start - start of the /proc/cpuinfo Hardware string to match.
970 * @param end - end of the /proc/cpuinfo Hardaware string to match.
971 * @param[out] chipset - location where chipset information will be stored upon a successful match.
972 *
973 * @returns true if signature matched, false otherwise.
974 */
975static bool match_omap(
976 const char* start, const char* end,
977 struct cpuinfo_arm_chipset chipset[restrict static 1])
978{
979 /* Expect exactly 8 symbols: "OMAP" (4 symbols) + 4-digit model number */
980 if (start + 8 != end) {
981 return false;
982 }
983
984 /* Check that the string starts with "OMAP". Symbols 0-4 are loaded and compared as little-endian 32-bit word. */
985 const uint32_t expected_omap = load_u32le(start);
986 if (expected_omap != UINT32_C(0x50414D4F) /* "PAMO" = reverse("OMAP") */) {
987 return false;
988 }
989
990 /* Validate and parse 4-digit model number */
991 uint32_t model = 0;
992 for (uint32_t i = 4; i < 8; i++) {
993 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
994 if (digit >= 10) {
995 /* Not really a digit */
996 return false;
997 }
998 model = model * 10 + digit;
999 }
1000
1001 /* Return parsed chipset. */
1002 *chipset = (struct cpuinfo_arm_chipset) {
1003 .vendor = cpuinfo_arm_chipset_vendor_texas_instruments,
1004 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
1005 .model = model,
1006 };
1007 return true;
1008}
1009
1010/**
1011 * Compares platform identifier string to known values for Broadcom chipsets.
1012 * If the string matches one of the known values, the function decodes Broadcom chipset from frequency and number of
1013 * cores into \p chipset argument.
1014 *
1015 * @param start - start of the platform identifier (ro.product.board or ro.board.platform) to match.
1016 * @param end - end of the platform identifier (ro.product.board or ro.board.platform) to match.
1017 * @param cores - number of cores in the chipset.
1018 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
1019 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1020 *
1021 * @returns true if signature matched (even if exact model can't be decoded), false otherwise.
1022 */
1023static bool match_and_parse_broadcom(
1024 const char* start, const char* end, uint32_t cores, uint32_t max_cpu_freq_max,
1025 struct cpuinfo_arm_chipset chipset[restrict static 1])
1026{
1027 /* Expect 4-6 symbols: "java" (4 symbols), "rhea" (4 symbols), "capri" (5 symbols), or "hawaii" (6 symbols) */
1028 const size_t length = end - start;
1029 switch (length) {
1030 case 4:
1031 case 5:
1032 case 6:
1033 break;
1034 default:
1035 return false;
1036 }
1037
1038 /*
1039 * Compare the platform identifier to known values for Broadcom chipsets:
1040 * - "rhea"
1041 * - "java"
1042 * - "capri"
1043 * - "hawaii"
1044 * Upon a successful match, decode chipset name from frequency and number of cores.
1045 */
1046 uint32_t model = 0;
1047 char suffix = 0;
1048 const uint32_t expected_platform = load_u32le(start);
1049 switch (expected_platform) {
1050 case UINT32_C(0x61656872): /* "aehr" = reverse("rhea") */
1051 if (length == 4) {
1052 /*
1053 * Detected "rhea" platform:
1054 * - 1 core @ 849999 KHz -> BCM21654
1055 * - 1 core @ 999999 KHz -> BCM21654G
1056 */
1057 if (cores == 1) {
1058 model = 21654;
1059 if (max_cpu_freq_max >= 999999) {
1060 suffix = 'G';
1061 }
1062 }
1063 }
1064 break;
1065 case UINT32_C(0x6176616A): /* "avaj" = reverse("java") */
1066 if (length == 4) {
1067 /*
1068 * Detected "java" platform:
1069 * - 4 cores -> BCM23550
1070 */
1071 if (cores == 4) {
1072 model = 23550;
1073 }
1074 }
1075 break;
1076 case UINT32_C(0x61776168): /* "awah" = reverse("hawa") */
1077 if (length == 6) {
1078 /* Check that string equals "hawaii" */
1079 const uint16_t expected_ii = load_u16le(start + 4);
1080 if (expected_ii == UINT16_C(0x6969) /* "ii" */ ) {
1081 /*
1082 * Detected "hawaii" platform:
1083 * - 1 core -> BCM21663
1084 * - 2 cores @ 999999 KHz -> BCM21664
1085 * - 2 cores @ 1200000 KHz -> BCM21664T
1086 */
1087 switch (cores) {
1088 case 1:
1089 model = 21663;
1090 break;
1091 case 2:
1092 model = 21664;
1093 if (max_cpu_freq_max >= 1200000) {
1094 suffix = 'T';
1095 }
1096 break;
1097 }
1098 }
1099 }
1100 break;
1101 case UINT32_C(0x72706163): /* "rpac" = reverse("capr") */
1102 if (length == 5) {
1103 /* Check that string equals "capri" */
1104 if (start[4] == 'i') {
1105 /*
1106 * Detected "capri" platform:
1107 * - 2 cores -> BCM28155
1108 */
1109 if (cores == 2) {
1110 model = 28155;
1111 }
1112 }
1113 }
1114 break;
1115 }
1116
1117 if (model != 0) {
1118 /* Chipset was successfully decoded */
1119 *chipset = (struct cpuinfo_arm_chipset) {
1120 .vendor = cpuinfo_arm_chipset_vendor_broadcom,
1121 .series = cpuinfo_arm_chipset_series_broadcom_bcm,
1122 .model = model,
1123 .suffix = {
1124 [0] = suffix,
1125 },
1126 };
1127 }
1128 return model != 0;
1129}
1130
1131struct sunxi_map_entry {
1132 uint8_t sunxi;
1133 uint8_t cores;
1134 uint8_t model;
1135 char suffix;
1136};
1137
1138static const struct sunxi_map_entry sunxi_map_entries[] = {
1139 {
1140 /* ("sun4i", 1) -> "A10" */
1141 .sunxi = 4,
1142 .cores = 1,
1143 .model = 10,
1144 },
1145 {
1146 /* ("sun5i", 1) -> "A13" */
1147 .sunxi = 5,
1148 .cores = 1,
1149 .model = 13,
1150 },
1151 {
1152 /* ("sun6i", 4) -> "A31" */
1153 .sunxi = 6,
1154 .cores = 4,
1155 .model = 31,
1156 },
1157 {
1158 /* ("sun7i", 2) -> "A20" */
1159 .sunxi = 7,
1160 .cores = 2,
1161 .model = 20,
1162
1163 },
1164 {
1165 /* ("sun8i", 2) -> "A23" */
1166 .sunxi = 8,
1167 .cores = 2,
1168 .model = 23,
1169 },
1170 {
1171 /* ("sun8i", 4) -> "A33" */
1172 .sunxi = 8,
1173 .cores = 4,
1174 .model = 33,
1175 },
1176 {
1177 /* ("sun8i", 8) -> "A83T" */
1178 .sunxi = 8,
1179 .cores = 8,
1180 .model = 83,
1181 .suffix = 'T',
1182 },
1183 {
1184 /* ("sun9i", 8) -> "A80" */
1185 .sunxi = 9,
1186 .cores = 8,
1187 .model = 80,
1188 },
1189 {
1190 /* ("sun50i", 4) -> "A64" */
1191 .sunxi = 50,
1192 .cores = 4,
1193 .model = 64,
1194 },
1195};
1196
1197/**
1198 * Tries to match /proc/cpuinfo Hardware string to Allwinner /sun\d+i/ signature.
1199 * If the string matches signature, the function decodes Allwinner chipset from the number in the signature and the
1200 * number of cores, and stores it in \p chipset argument.
1201 *
1202 * @param start - start of the /proc/cpuinfo Hardware string to match.
1203 * @param end - end of the /proc/cpuinfo Hardware string to match.
1204 * @param cores - number of cores in the chipset.
1205 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1206 *
1207 * @returns true if signature matched (even if exact model can't be decoded), false otherwise.
1208 */
1209static bool match_and_parse_sunxi(
1210 const char* start, const char* end, uint32_t cores,
1211 struct cpuinfo_arm_chipset chipset[restrict static 1])
1212{
1213 /* Expect at least 5 symbols: "sun" (3 symbols) + platform id (1-2 digits) + "i" (1 symbol) */
1214 if (start + 5 > end) {
1215 return false;
1216 }
1217
1218 /* Compare the first 3 characters to "sun" */
1219 if (start[0] != 's') {
1220 return false;
1221 }
1222 const uint16_t expected_un = load_u16le(start + 1);
1223 if (expected_un != UINT16_C(0x6E75) /* "nu" = reverse("un") */) {
1224 return false;
1225 }
1226
1227 /* Check and parse the first (required) digit of the sunXi platform id */
1228 uint32_t sunxi_platform = 0;
1229 {
1230 const uint32_t digit = (uint32_t) (uint8_t) start[3] - '0';
1231 if (digit >= 10) {
1232 /* Not really a digit */
1233 return false;
1234 }
1235 sunxi_platform = digit;
1236 }
1237
1238 /* Parse optional second digit of the sunXi platform id */
1239 const char* pos = start + 4;
1240 {
1241 const uint32_t digit = (uint32_t) (uint8_t) (*pos) - '0';
1242 if (digit < 10) {
1243 sunxi_platform = sunxi_platform * 10 + digit;
1244 if (++pos == end) {
1245 /* Expected one more character, final 'i' letter */
1246 return false;
1247 }
1248 }
1249 }
1250
1251 /* Validate the final 'i' letter */
1252 if (*pos != 'i') {
1253 return false;
1254 }
1255
1256 /* Compare sunXi platform id and number of cores to tabluted values to decode chipset name */
1257 uint32_t model = 0;
1258 char suffix = 0;
1259 for (size_t i = 0; i < CPUINFO_COUNT_OF(sunxi_map_entries); i++) {
1260 if (sunxi_platform == sunxi_map_entries[i].sunxi && cores == sunxi_map_entries[i].cores) {
1261 model = sunxi_map_entries[i].model;
1262 suffix = sunxi_map_entries[i].suffix;
1263 break;
1264 }
1265 }
1266
1267 if (model == 0) {
1268 cpuinfo_log_info("unrecognized %"PRIu32"-core Allwinner sun%"PRIu32" platform", cores, sunxi_platform);
1269 }
1270 /* Create chipset name from decoded data */
1271 *chipset = (struct cpuinfo_arm_chipset) {
1272 .vendor = cpuinfo_arm_chipset_vendor_allwinner,
1273 .series = cpuinfo_arm_chipset_series_allwinner_a,
1274 .model = model,
1275 .suffix = {
1276 [0] = suffix,
1277 },
1278 };
1279 return true;
1280}
1281
1282/**
1283 * Compares /proc/cpuinfo Hardware string to "WMT" signature.
1284 * If the string matches signature, the function decodes WonderMedia chipset from frequency and number of cores into
1285 * \p chipset argument.
1286 *
1287 * @param start - start of the /proc/cpuinfo Hardware string to match.
1288 * @param end - end of the /proc/cpuinfo Hardware string to match.
1289 * @param cores - number of cores in the chipset.
1290 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
1291 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1292 *
1293 * @returns true if signature matched (even if exact model can't be decoded), false otherwise.
1294 */
1295static bool match_and_parse_wmt(
1296 const char* start, const char* end, uint32_t cores, uint32_t max_cpu_freq_max,
1297 struct cpuinfo_arm_chipset chipset[restrict static 1])
1298{
1299 /* Expected 3 symbols: "WMT" */
1300 if (start + 3 != end) {
1301 return false;
1302 }
1303
1304 /* Compare string to "WMT" */
1305 if (start[0] != 'W') {
1306 return false;
1307 }
1308 const uint16_t expected_mt = load_u16le(start + 1);
1309 if (expected_mt != UINT16_C(0x544D) /* "TM" = reverse("MT") */) {
1310 return false;
1311 }
1312
1313 /* Decode chipset name from frequency and number of cores */
1314 uint32_t model = 0;
1315 switch (cores) {
1316 case 1:
1317 switch (max_cpu_freq_max) {
1318 case 1008000:
1319 /* 1 core @ 1008000 KHz -> WM8950 */
1320 model = 8950;
1321 break;
1322 case 1200000:
1323 /* 1 core @ 1200000 KHz -> WM8850 */
1324 model = 8850;
1325 break;
1326 }
1327 break;
1328 case 2:
1329 if (max_cpu_freq_max == 1500000) {
1330 /* 2 cores @ 1500000 KHz -> WM8880 */
1331 model = 8880;
1332 }
1333 break;
1334 }
1335
1336 if (model == 0) {
1337 cpuinfo_log_info("unrecognized WonderMedia platform with %"PRIu32" cores at %"PRIu32" KHz",
1338 cores, max_cpu_freq_max);
1339 }
1340 *chipset = (struct cpuinfo_arm_chipset) {
1341 .vendor = cpuinfo_arm_chipset_vendor_wondermedia,
1342 .series = cpuinfo_arm_chipset_series_wondermedia_wm,
1343 .model = model,
1344 };
1345 return true;
1346}
1347
1348struct huawei_map_entry {
1349 uint32_t platform;
1350 uint32_t model;
1351};
1352
1353static const struct huawei_map_entry huawei_platform_map[] = {
1354 {
1355 /* "BAC" -> Kirin 659 */
1356 .platform = UINT32_C(0x00434142), /* "\0CAB" = reverse("BAC\0") */
1357 .model = 659,
1358 },
1359 {
1360 /* "DUK" -> Kirin 960 */
1361 .platform = UINT32_C(0x004B5544), /* "\0KUD" = reverse("DUK\0") */
1362 .model = 960,
1363 },
1364 {
1365 /* "EVA" -> Kirin 955 */
1366 .platform = UINT32_C(0x00415645), /* "\0AVE" = reverse("EVA\0") */
1367 .model = 955,
1368 },
1369 {
1370 /* "FRD" -> Kirin 950 */
1371 .platform = UINT32_C(0x00445246), /* "\0DRF" = reverse("FRD\0") */
1372 .model = 950,
1373 },
1374 {
1375 /* "KNT" -> Kirin 950 */
1376 .platform = UINT32_C(0x00544E4B), /* "\0TNK" = reverse("KNT\0") */
1377 .model = 950,
1378 },
1379 {
1380 /* "LON" -> Kirin 960 */
1381 .platform = UINT32_C(0x004E4F4C), /* "\0NOL" = reverse("LON\0") */
1382 .model = 960,
1383 },
1384 {
1385 /* "MHA" -> Kirin 960 */
1386 .platform = UINT32_C(0x0041484D), /* "\0AHM" = reverse("MHA\0") */
1387 .model = 960,
1388 },
1389 {
1390 /* "NXT" -> Kirin 950 */
1391 .platform = UINT32_C(0x0054584E), /* "\0TXN" = reverse("NXT\0") */
1392 .model = 950,
1393 },
1394 {
1395 /* "STF" -> Kirin 960 */
1396 .platform = UINT32_C(0x00465453), /* "\0FTS" = reverse("STF\0") */
1397 .model = 960,
1398 },
1399 {
1400 /* "VIE" -> Kirin 955 */
1401 .platform = UINT32_C(0x00454956), /* "\0EIV" = reverse("VIE\0") */
1402 .model = 955,
1403 },
1404 {
1405 /* "VKY" -> Kirin 960 */
1406 .platform = UINT32_C(0x00594B56), /* "\0YKV" = reverse("VKY\0") */
1407 .model = 960,
1408 },
1409 {
1410 /* "VTR" -> Kirin 960 */
1411 .platform = UINT32_C(0x00525456), /* "\0RTV" = reverse("VTR\0") */
1412 .model = 960,
1413 },
1414};
1415
1416/**
1417 * Tries to match ro.product.board string to Huawei /([A-Z]{3})(\-[A-Z]?L\d{2})$/ signature where \1 is one of the
1418 * known values for Huawei devices, which do not report chipset name elsewhere.
1419 * If the string matches signature, the function decodes chipset (always HiSilicon Kirin for matched devices) from
1420 * the number in the signature and stores it in \p chipset argument.
1421 *
1422 * @param start - start of the ro.product.board string to match.
1423 * @param end - end of the ro.product.board string to match.
1424 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1425 *
1426 * @returns true if signature matched, false otherwise.
1427 */
1428static bool match_and_parse_huawei(
1429 const char* start, const char* end,
1430 struct cpuinfo_arm_chipset chipset[restrict static 1])
1431{
1432 /*
1433 * Expect length of either 3, 7 or 8, exactly:
1434 * - 3-letter platform identifier (see huawei_platform_map)
1435 * - 3-letter platform identifier + '-' + 'L' + two digits
1436 * - 3-letter platform identifier + '-' + capital letter + 'L' + two digits
1437 */
1438 const size_t length = end - start;
1439 switch (length) {
1440 case 3:
1441 case 7:
1442 case 8:
1443 break;
1444 default:
1445 return false;
1446 }
1447
1448 /*
1449 * Try to find the first three-letter substring in among the tabulated entries for Huawei devices.
1450 * The first three letters are loaded and compared as a little-endian 24-bit word.
1451 */
1452 uint32_t model = 0;
1453 const uint32_t target_platform_id = load_u24le(start);
1454 for (uint32_t i = 0; i < CPUINFO_COUNT_OF(huawei_platform_map); i++) {
1455 if (huawei_platform_map[i].platform == target_platform_id) {
1456 model = huawei_platform_map[i].model;
1457 break;
1458 }
1459 }
1460
1461 if (model == 0) {
1462 /* Platform does not match the tabulated Huawei entries */
1463 return false;
1464 }
1465
1466 if (length > 3) {
1467 /*
1468 * Check that:
1469 * - The symbol after platform id is a dash
1470 * - The symbol after it is an uppercase letter. For 7-symbol strings, the symbol is just 'L'.
1471 */
1472 if (start[3] != '-' || !is_ascii_alphabetic_uppercase(start[4])) {
1473 return false;
1474 }
1475
1476 /* Check that the last 3 entries are /L\d\d/ */
1477 if (end[-3] != 'L' || !is_ascii_numeric(end[-2]) || !is_ascii_numeric(end[-1])) {
1478 return false;
1479 }
1480 }
1481
1482 /* All checks succeeded, commit chipset name */
1483 *chipset = (struct cpuinfo_arm_chipset) {
1484 .vendor = cpuinfo_arm_chipset_vendor_hisilicon,
1485 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1486 .model = model,
1487 };
1488 return true;
1489}
1490
1491/**
1492 * Tries to match /tcc\d{3}x$/ signature for Telechips TCCXXXx chipsets.
1493 * If match successful, extracts model information into \p chipset argument.
1494 *
1495 * @param start - start of the /proc/cpuinfo Hardware string to match.
1496 * @param end - end of the /proc/cpuinfo Hardware string to match.
1497 * @param[out] chipset - location where chipset information will be stored upon a successful match.
1498 *
1499 * @returns true if signature matched, false otherwise.
1500 */
1501static bool match_tcc(
1502 const char* start, const char* end,
1503 struct cpuinfo_arm_chipset chipset[restrict static 1])
1504{
1505 /* Expect exactly 7 symbols: "tcc" (3 symbols) + 3-digit model number + fixed "x" suffix */
1506 if (start + 7 != end) {
1507 return false;
1508 }
1509
1510 /* Quick check for the first character */
1511 if (start[0] != 't') {
1512 return false;
1513 }
1514
1515 /* Load the next 2 bytes as little endian 16-bit word */
1516 const uint16_t expected_cc = load_u16le(start + 1);
1517 if (expected_cc != UINT16_C(0x6363) /* "cc" */ ) {
1518 return false;
1519 }
1520
1521 /* Check and parse 3-digit model number */
1522 uint32_t model = 0;
1523 for (uint32_t i = 3; i < 6; i++) {
1524 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
1525 if (digit >= 10) {
1526 /* Not really a digit */
1527 return false;
1528 }
1529 model = model * 10 + digit;
1530 }
1531
1532 /* Check the fixed 'x' suffix in the end */
1533 if (start[6] != 'x') {
1534 return false;
1535 }
1536
1537 /* Commit parsed chipset. */
1538 *chipset = (struct cpuinfo_arm_chipset) {
1539 .vendor = cpuinfo_arm_chipset_vendor_telechips,
1540 .series = cpuinfo_arm_chipset_series_telechips_tcc,
1541 .model = model,
Marat Dukhan63a6a102017-08-24 21:28:02 -07001542 .suffix = {
1543 [0] = 'X'
1544 },
Marat Dukhan006461a2017-08-24 16:10:46 -07001545 };
1546 return true;
1547}
1548
1549/*
Marat Dukhan93982f22017-10-20 13:10:23 -07001550 * Compares ro.board.platform string to Nvidia Tegra signatures ("tegra" and "tegra3")
Marat Dukhan006461a2017-08-24 16:10:46 -07001551 * This check has effect on how /proc/cpuinfo Hardware string is interpreted.
1552 *
1553 * @param start - start of the ro.board.platform string to check.
1554 * @param end - end of the ro.board.platform string to check.
1555 *
Marat Dukhan93982f22017-10-20 13:10:23 -07001556 * @returns true if the string matches an Nvidia Tegra signature, and false otherwise
Marat Dukhan006461a2017-08-24 16:10:46 -07001557 */
1558static bool is_tegra(const char* start, const char* end) {
1559 /* Expect 5 ("tegra") or 6 ("tegra3") symbols */
1560 const size_t length = end - start;
1561 switch (length) {
1562 case 5:
1563 case 6:
1564 break;
1565 default:
1566 return false;
1567 }
1568
1569 /* Check that the first 5 characters match "tegra" */
1570 if (start[0] != 't') {
1571 return false;
1572 }
1573 const uint32_t expected_egra = load_u32le(start + 1);
1574 if (expected_egra != UINT32_C(0x61726765) /* "arge" = reverse("egra") */) {
1575 return false;
1576 }
1577
1578 /* Check if the string is either "tegra" (length = 5) or "tegra3" (length != 5) and last character is '3' */
1579 return (length == 5 || start[5] == '3');
1580}
1581
1582struct special_map_entry {
1583 const char* platform;
1584 uint16_t model;
1585 uint8_t series;
1586 char suffix;
1587};
1588
1589static const struct special_map_entry special_hardware_map_entries[] = {
1590 {
1591 /* "k3v2oem1" -> HiSilicon K3V2 */
1592 .platform = "k3v2oem1",
1593 .series = cpuinfo_arm_chipset_series_hisilicon_k3v,
1594 .model = 2,
1595 },
1596 {
1597 /* "hi6620oem" -> HiSilicon Kirin 910T */
1598 .platform = "hi6620oem",
1599 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1600 .model = 910,
1601 .suffix = 'T'
1602 },
1603 {
1604 /* "hi6250" -> HiSilicon Kirin 650 */
1605 .platform = "hi6250",
1606 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1607 .model = 650,
1608 },
1609 {
1610 /* "hi6210sft" -> HiSilicon Kirin 620 */
1611 .platform = "hi6210sft",
1612 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1613 .model = 620,
1614 },
1615 {
1616 /* "hi3751" -> HiSilicon Hi3751 */
1617 .platform = "hi3751",
1618 .series = cpuinfo_arm_chipset_series_hisilicon_hi,
1619 .model = 3751,
1620 },
1621 {
1622 /* "hi3635" -> HiSilicon Kirin 930 */
1623 .platform = "hi3635",
1624 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1625 .model = 930,
1626 },
1627 {
1628 /* "hi3630" -> HiSilicon Kirin 920 */
1629 .platform = "hi3630",
1630 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1631 .model = 920,
1632 },
1633 {
Marat Dukhan23401882017-11-28 11:07:46 -08001634 /* "gs702a" -> Actions ATM7029 (Cortex-A5 + GC1000) */
Marat Dukhan006461a2017-08-24 16:10:46 -07001635 .platform = "gs702a",
1636 .series = cpuinfo_arm_chipset_series_actions_atm,
1637 .model = 7029,
1638 },
1639 {
Marat Dukhan23401882017-11-28 11:07:46 -08001640 /* "gs702c" -> Actions ATM7029B (Cortex-A5 + SGX540) */
Marat Dukhan006461a2017-08-24 16:10:46 -07001641 .platform = "gs702c",
1642 .series = cpuinfo_arm_chipset_series_actions_atm,
1643 .model = 7029,
Marat Dukhan23401882017-11-28 11:07:46 -08001644 .suffix = 'B',
1645 },
1646 {
1647 /* "gs703d" -> Actionns ATM7039S */
1648 .platform = "gs703d",
1649 .series = cpuinfo_arm_chipset_series_actions_atm,
1650 .model = 7039,
1651 .suffix = 'S',
1652 },
1653 {
1654 /* "gs705a" -> Actions ATM7059A */
1655 .platform = "gs705a",
1656 .series = cpuinfo_arm_chipset_series_actions_atm,
1657 .model = 7059,
1658 .suffix = 'A',
Marat Dukhan006461a2017-08-24 16:10:46 -07001659 },
1660 {
1661 /* "Amlogic Meson8" -> Amlogic S812 */
1662 .platform = "Amlogic Meson8",
1663 .series = cpuinfo_arm_chipset_series_amlogic_s,
1664 .model = 812,
1665 },
1666 {
1667 /* "Amlogic Meson8B" -> Amlogic S805 */
1668 .platform = "Amlogic Meson8B",
1669 .series = cpuinfo_arm_chipset_series_amlogic_s,
1670 .model = 805,
1671 },
1672 {
1673 /* "mapphone_CDMA" -> Texas Instruments OMAP4430 */
1674 .platform = "mapphone_CDMA",
1675 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
1676 .model = 4430,
1677 },
1678 {
1679 /* "Tuna" (Samsung Galaxy Nexus) -> Texas Instruments OMAP4460 */
1680 .platform = "Tuna",
1681 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
1682 .model = 4460,
1683 },
1684 {
1685 /* "Manta" (Samsung Nexus 10) -> Samsung Exynos 5250 */
1686 .platform = "Manta",
1687 .series = cpuinfo_arm_chipset_series_samsung_exynos,
1688 .model = 5250,
1689 },
1690 {
1691 /* "Odin" -> LG Nuclun 7111 */
1692 .platform = "Odin",
1693 .series = cpuinfo_arm_chipset_series_lg_nuclun,
1694 .model = 7111,
1695 },
1696 {
1697 /* "Madison" -> MStar 6A338 */
1698 .platform = "Madison",
1699 .series = cpuinfo_arm_chipset_series_mstar_6a,
1700 .model = 338,
1701 },
1702};
1703
1704static const struct special_map_entry tegra_hardware_map_entries[] = {
1705 {
Marat Dukhan93982f22017-10-20 13:10:23 -07001706 /* "cardhu" (Nvidia Cardhu developer tablet) -> Tegra T30 */
Marat Dukhan006461a2017-08-24 16:10:46 -07001707 .platform = "cardhu",
1708 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1709 .model = 30,
1710 },
1711 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001712 /* "kai" -> Tegra T30L */
1713 .platform = "kai",
1714 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1715 .model = 30,
1716 .suffix = 'L',
1717 },
1718 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001719 /* "p3" (Samsung Galaxy Tab 8.9) -> Tegra T20 */
1720 .platform = "p3",
1721 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1722 .model = 20,
1723 },
1724 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001725 /* "n1" (Samsung Galaxy R / Samsung Captivate Glide) -> Tegra AP20H */
1726 .platform = "n1",
1727 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1728 .model = 20,
1729 .suffix = 'H',
1730 },
1731 {
1732 /* "SHW-M380S" (Samsung Galaxy Tab 10.1) -> Tegra T20 */
1733 .platform = "SHW-M380S",
1734 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1735 .model = 20,
1736 },
1737 {
1738 /* "m470" (Hisense Sero 7 Pro) -> Tegra T30L */
1739 .platform = "m470",
1740 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1741 .model = 30,
1742 .suffix = 'L',
1743 },
1744 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001745 /* "endeavoru" (HTC One X) -> Tegra AP33 */
1746 .platform = "endeavoru",
1747 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1748 .model = 33,
1749 },
1750 {
1751 /* "evitareul" (HTC One X+) -> Tegra T33 */
1752 .platform = "evitareul",
1753 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1754 .model = 33,
1755 },
1756 {
1757 /* "enrc2b" (HTC One X+) -> Tegra T33 */
1758 .platform = "enrc2b",
1759 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1760 .model = 33,
1761 },
1762 {
1763 /* "mozart" (Asus Transformer Pad TF701T) -> Tegra T114 */
1764 .platform = "mozart",
1765 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1766 .model = 114,
1767 },
1768 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001769 /* "tegratab" (Tegra Note 7) -> Tegra T114 */
1770 .platform = "tegratab",
1771 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1772 .model = 114,
1773 },
1774 {
Marat Dukhan93982f22017-10-20 13:10:23 -07001775 /* "tn8" (Nvidia Shield Tablet K1) -> Tegra T124 */
Marat Dukhan006461a2017-08-24 16:10:46 -07001776 .platform = "tn8",
1777 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1778 .model = 124,
1779 },
1780 {
Marat Dukhan93982f22017-10-20 13:10:23 -07001781 /* "roth" (Nvidia Shield Portable) -> Tegra T114 */
Marat Dukhan56b24032017-09-05 18:40:20 -07001782 .platform = "roth",
1783 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1784 .model = 114,
1785 },
1786 {
Marat Dukhan93982f22017-10-20 13:10:23 -07001787 /* "foster_e" (Nvidia Shield TV, Flash) -> Tegra T210 */
Marat Dukhan56b24032017-09-05 18:40:20 -07001788 .platform = "foster_e",
1789 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1790 .model = 210,
1791 },
1792 {
Marat Dukhan93982f22017-10-20 13:10:23 -07001793 /* "foster_e_hdd" (Nvidia Shield TV, HDD) -> Tegra T210 */
Marat Dukhan56b24032017-09-05 18:40:20 -07001794 .platform = "foster_e_hdd",
1795 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1796 .model = 210,
1797 },
1798 {
Marat Dukhan93982f22017-10-20 13:10:23 -07001799 /* "darcy" (Nvidia Shield TV 2017) -> Tegra T210 */
Marat Dukhan56b24032017-09-05 18:40:20 -07001800 .platform = "darcy",
1801 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1802 .model = 210,
1803 },
1804 {
1805 /* "pisces" (Xiaomi Mi 3) -> Tegra T114 */
1806 .platform = "pisces",
1807 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1808 .model = 114,
1809 },
1810 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001811 /* "mocha" (Xiaomi Mi Pad) -> Tegra T124 */
1812 .platform = "mocha",
1813 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1814 .model = 124,
1815 },
1816 {
1817 /* "stingray" (Motorola XOOM) -> Tegra AP20H */
1818 .platform = "stingray",
1819 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1820 .model = 20,
1821 .suffix = 'H',
1822 },
1823 {
1824 /* "Ceres" (Wiko Highway 4G) -> Tegra SL460N */
1825 .platform = "Ceres",
1826 .series = cpuinfo_arm_chipset_series_nvidia_tegra_sl,
1827 .model = 460,
1828 .suffix = 'N',
1829 },
1830 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001831 /* "MT799" (nabi 2 Tablet) -> Tegra T30 */
1832 .platform = "MT799",
1833 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1834 .model = 30,
1835 },
1836 {
1837 /* "t8400n" (nabi DreamTab HD8) -> Tegra T114 */
1838 .platform = "t8400n",
1839 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1840 .model = 114,
1841 },
1842 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001843 /* "chagall" (Fujitsu Stylistic M532) -> Tegra T30 */
1844 .platform = "chagall",
1845 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1846 .model = 30,
1847 },
1848 {
1849 /* "ventana" (Asus Transformer TF101) -> Tegra T20 */
1850 .platform = "ventana",
1851 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1852 .model = 20,
1853 },
1854 {
1855 /* "bobsleigh" (Fujitsu Arrows Tab F-05E) -> Tegra T33 */
1856 .platform = "bobsleigh",
1857 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1858 .model = 33,
1859 },
1860 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001861 /* "tegra_fjdev101" (Fujitsu Arrows X F-10D) -> Tegra AP33 */
1862 .platform = "tegra_fjdev101",
1863 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1864 .model = 33,
1865 },
1866 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001867 /* "tegra_fjdev103" (Fujitsu Arrows V F-04E) -> Tegra T33 */
1868 .platform = "tegra_fjdev103",
1869 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1870 .model = 33,
1871 },
1872 {
1873 /* "nbx03" (Sony Tablet S) -> Tegra T20 */
1874 .platform = "nbx03",
1875 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1876 .model = 20,
1877 },
1878 {
1879 /* "txs03" (Sony Xperia Tablet S) -> Tegra T30L */
1880 .platform = "txs03",
1881 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1882 .model = 30,
1883 .suffix = 'L',
1884 },
1885 {
1886 /* "x3" (LG Optimus 4X HD P880) -> Tegra AP33 */
1887 .platform = "x3",
1888 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1889 .model = 33,
1890 },
1891 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001892 /* "vu10" (LG Optimus Vu P895) -> Tegra AP33 */
1893 .platform = "vu10",
1894 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1895 .model = 33,
1896 },
1897 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001898 /* "BIRCH" (HP Slate 7 Plus) -> Tegra T30L */
1899 .platform = "BIRCH",
1900 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1901 .model = 30,
1902 .suffix = 'L',
1903 },
1904 {
1905 /* "macallan" (HP Slate 8 Pro) -> Tegra T114 */
1906 .platform = "macallan",
1907 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1908 .model = 114,
1909 },
1910 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001911 /* "maya" (HP SlateBook 10 x2) -> Tegra T114 */
1912 .platform = "maya",
1913 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1914 .model = 114,
1915 },
1916 {
1917 /* "antares" (Toshiba AT100) -> Tegra T20 */
1918 .platform = "antares",
1919 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1920 .model = 20,
1921 },
1922 {
1923 /* "tostab12AL" (Toshiba AT300SE "Excite 10 SE") -> Tegra T30L */
1924 .platform = "tostab12AL",
1925 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1926 .model = 30,
1927 .suffix = 'L',
1928 },
1929 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001930 /* "tostab12BL" (Toshiba AT10-A "Excite Pure") -> Tegra T30L */
1931 .platform = "tostab12BL",
1932 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1933 .model = 30,
1934 .suffix = 'L',
1935 },
1936 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001937 /* "sphinx" (Toshiba AT270 "Excite 7.7") -> Tegra T30 */
1938 .platform = "sphinx",
1939 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1940 .model = 30,
1941 },
1942 {
1943 /* "tostab11BS" (Toshiba AT570 "Regza 7.7") -> Tegra T30 */
1944 .platform = "tostab11BS",
1945 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1946 .model = 30,
1947 },
1948 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001949 /* "tostab12BA" (Toshiba AT10-LE-A "Excite Pro") -> Tegra T114 */
1950 .platform = "tostab12BA",
1951 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1952 .model = 114,
1953 },
1954 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001955 /* "vangogh" (Acer Iconia Tab A100) -> Tegra T20 */
1956 .platform = "vangogh",
1957 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
Marat Dukhan006461a2017-08-24 16:10:46 -07001958 .model = 20,
Marat Dukhan56b24032017-09-05 18:40:20 -07001959 },
1960 {
1961 /* "a110" (Acer Iconia Tab A110) -> Tegra T30L */
1962 .platform = "a110",
1963 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1964 .model = 30,
1965 .suffix = 'L',
Marat Dukhan006461a2017-08-24 16:10:46 -07001966 },
1967 {
1968 /* "picasso_e" (Acer Iconia Tab A200) -> Tegra AP20H */
1969 .platform = "picasso_e",
1970 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1971 .model = 20,
1972 .suffix = 'H',
1973 },
1974 {
1975 /* "picasso_e2" (Acer Iconia Tab A210) -> Tegra T30L */
1976 .platform = "picasso_e2",
1977 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1978 .model = 30,
1979 .suffix = 'L',
1980 },
1981 {
Marat Dukhan56b24032017-09-05 18:40:20 -07001982 /* "picasso" (Acer Iconia Tab A500) -> Tegra AP20H */
1983 .platform = "picasso",
1984 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1985 .model = 20,
1986 .suffix = 'H',
1987 },
1988 {
Marat Dukhan006461a2017-08-24 16:10:46 -07001989 /* "picasso_m" (Acer Iconia Tab A510) -> Tegra T30 */
1990 .platform = "picasso_m",
1991 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1992 .model = 30,
1993 },
1994 {
1995 /* "picasso_mf" (Acer Iconia Tab A700) -> Tegra T30 */
1996 .platform = "picasso_mf",
1997 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1998 .model = 30,
1999 },
2000 {
2001 /* "avalon" (Toshiba AT300 "Excite 10") -> Tegra T30L */
2002 .platform = "avalon",
2003 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2004 .model = 30,
2005 .suffix = 'L',
2006 },
2007 {
2008 /* "NS_14T004" (iRiver NS-14T004) -> Tegra T30L */
2009 .platform = "NS_14T004",
2010 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2011 .model = 30,
2012 .suffix = 'L',
2013 },
Marat Dukhan56b24032017-09-05 18:40:20 -07002014 {
2015 /* "WIKIPAD" (Wikipad) -> Tegra T30 */
2016 .platform = "WIKIPAD",
2017 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2018 .model = 30,
2019 },
2020 {
2021 /* "kb" (Pegatron Q00Q) -> Tegra T114 */
2022 .platform = "kb",
2023 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2024 .model = 114,
2025 },
Marat Dukhan006461a2017-08-24 16:10:46 -07002026};
2027
2028/*
2029 * Decodes chipset name from /proc/cpuinfo Hardware string.
2030 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
2031 *
2032 * @param[in] platform - /proc/cpuinfo Hardware string.
2033 * @param cores - number of cores in the chipset.
2034 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2035 *
2036 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2037 * and series identifiers.
2038 */
Marat Dukhan1415d7d2017-10-16 09:40:15 -07002039struct cpuinfo_arm_chipset cpuinfo_arm_linux_decode_chipset_from_proc_cpuinfo_hardware(
Marat Dukhan006461a2017-08-24 16:10:46 -07002040 const char hardware[restrict static CPUINFO_HARDWARE_VALUE_MAX],
2041 uint32_t cores, uint32_t max_cpu_freq_max, bool is_tegra)
2042{
2043 struct cpuinfo_arm_chipset chipset;
2044 const size_t hardware_length = strnlen(hardware, CPUINFO_HARDWARE_VALUE_MAX);
2045 const char* hardware_end = hardware + hardware_length;
2046
2047 if (is_tegra) {
2048 /*
Marat Dukhan93982f22017-10-20 13:10:23 -07002049 * Nvidia Tegra-specific path: compare /proc/cpuinfo Hardware string to
Marat Dukhan006461a2017-08-24 16:10:46 -07002050 * tabulated Hardware values for popular chipsets/devices with Tegra chipsets.
2051 * This path is only used when ro.board.platform indicates a Tegra chipset
2052 * (albeit does not indicate which exactly Tegra chipset).
2053 */
2054 for (size_t i = 0; i < CPUINFO_COUNT_OF(tegra_hardware_map_entries); i++) {
2055 if (strncmp(tegra_hardware_map_entries[i].platform, hardware, hardware_length) == 0 &&
2056 tegra_hardware_map_entries[i].platform[hardware_length] == 0)
2057 {
2058 cpuinfo_log_debug(
Marat Dukhan93982f22017-10-20 13:10:23 -07002059 "found /proc/cpuinfo Hardware string \"%.*s\" in Nvidia Tegra chipset table",
Marat Dukhan006461a2017-08-24 16:10:46 -07002060 (int) hardware_length, hardware);
2061 /* Create chipset name from entry */
2062 return (struct cpuinfo_arm_chipset) {
2063 .vendor = chipset_series_vendor[tegra_hardware_map_entries[i].series],
2064 .series = (enum cpuinfo_arm_chipset_series) tegra_hardware_map_entries[i].series,
2065 .model = tegra_hardware_map_entries[i].model,
2066 .suffix = {
2067 [0] = tegra_hardware_map_entries[i].suffix,
2068 },
2069 };
2070 }
2071 }
2072 } else {
2073 /* Generic path: consider all other vendors */
2074
2075 bool word_start = true;
2076 for (const char* pos = hardware; pos != hardware_end; pos++) {
2077 const char c = *pos;
2078 switch (c) {
2079 case ' ':
2080 case '\t':
2081 case ',':
2082 word_start = true;
2083 break;
2084 default:
2085 if (word_start && is_ascii_alphabetic(c)) {
2086 /* Check Qualcomm MSM/APQ signature */
2087 if (match_msm_apq(pos, hardware_end, &chipset)) {
2088 cpuinfo_log_debug(
2089 "matched Qualcomm MSM/APQ signature in /proc/cpuinfo Hardware string \"%.*s\"",
2090 (int) hardware_length, hardware);
2091 return chipset;
2092 }
2093
2094 /* Check SDMxxx (Qualcomm Snapdragon) signature */
2095 if (match_sdm(pos, hardware_end, &chipset)) {
2096 cpuinfo_log_debug(
2097 "matched Qualcomm SDM signature in /proc/cpuinfo Hardware string \"%.*s\"",
2098 (int) hardware_length, hardware);
2099 return chipset;
2100 }
2101
2102 /* Check MediaTek MT signature */
2103 if (match_mt(pos, hardware_end, true, &chipset)) {
2104 cpuinfo_log_debug(
2105 "matched MediaTek MT signature in /proc/cpuinfo Hardware string \"%.*s\"",
2106 (int) hardware_length, hardware);
2107 return chipset;
2108 }
2109
2110 /* Check HiSilicon Kirin signature */
2111 if (match_kirin(pos, hardware_end, &chipset)) {
2112 cpuinfo_log_debug(
2113 "matched HiSilicon Kirin signature in /proc/cpuinfo Hardware string \"%.*s\"",
2114 (int) hardware_length, hardware);
2115 return chipset;
2116 }
2117
2118 /* Check Rockchip RK signature */
2119 if (match_rk(pos, hardware_end, &chipset)) {
2120 cpuinfo_log_debug(
2121 "matched Rockchip RK signature in /proc/cpuinfo Hardware string \"%.*s\"",
2122 (int) hardware_length, hardware);
2123 return chipset;
2124 }
2125 }
2126 word_start = false;
2127 break;
2128 }
2129 }
2130
2131 /* Check Samsung Exynos signature */
2132 if (match_samsung_exynos(hardware, hardware_end, &chipset)) {
2133 cpuinfo_log_debug(
2134 "matched Samsung Exynos signature in /proc/cpuinfo Hardware string \"%.*s\"",
2135 (int) hardware_length, hardware);
2136 return chipset;
2137 }
2138
2139 /* Check universalXXXX (Samsung Exynos) signature */
2140 if (match_universal(hardware, hardware_end, &chipset)) {
2141 cpuinfo_log_debug(
2142 "matched UNIVERSAL (Samsung Exynos) signature in /proc/cpuinfo Hardware string \"%.*s\"",
2143 (int) hardware_length, hardware);
2144 return chipset;
2145 }
2146
2147 /* Match /SMDK(4410|4x12)$/ */
2148 if (match_and_parse_smdk(hardware, hardware_end, cores, &chipset)) {
2149 cpuinfo_log_debug(
2150 "matched SMDK (Samsung Exynos) signature in /proc/cpuinfo Hardware string \"%.*s\"",
2151 (int) hardware_length, hardware);
2152 return chipset;
2153 }
2154
2155 /* Check Spreadtrum SC signature */
2156 if (match_sc(hardware, hardware_end, &chipset)) {
2157 cpuinfo_log_debug(
2158 "matched Spreadtrum SC signature in /proc/cpuinfo Hardware string \"%.*s\"",
2159 (int) hardware_length, hardware);
2160 return chipset;
2161 }
2162
2163 /* Check Marvell PXA signature */
2164 if (match_pxa(hardware, hardware_end, &chipset)) {
2165 cpuinfo_log_debug(
2166 "matched Marvell PXA signature in /proc/cpuinfo Hardware string \"%.*s\"",
2167 (int) hardware_length, hardware);
2168 return chipset;
2169 }
2170
2171 /* Match /sun\d+i/ signature and map to Allwinner chipset name */
2172 if (match_and_parse_sunxi(hardware, hardware_end, cores, &chipset)) {
2173 cpuinfo_log_debug(
2174 "matched sunxi (Allwinner Ax) signature in /proc/cpuinfo Hardware string \"%.*s\"",
2175 (int) hardware_length, hardware);
2176 return chipset;
2177 }
2178
2179 /* Check Texas Instruments OMAP signature */
2180 if (match_omap(hardware, hardware_end, &chipset)) {
2181 cpuinfo_log_debug(
2182 "matched Texas Instruments OMAP signature in /proc/cpuinfo Hardware string \"%.*s\"",
2183 (int) hardware_length, hardware);
2184 return chipset;
2185 }
2186
2187 /* Check WonderMedia WMT signature and decode chipset from frequency and number of cores */
2188 if (match_and_parse_wmt(hardware, hardware_end, cores, max_cpu_freq_max, &chipset)) {
2189 cpuinfo_log_debug(
2190 "matched WonderMedia WMT signature in /proc/cpuinfo Hardware string \"%.*s\"",
2191 (int) hardware_length, hardware);
2192 return chipset;
2193 }
2194
2195 /* Check Telechips TCC signature */
2196 if (match_tcc(hardware, hardware_end, &chipset)) {
2197 cpuinfo_log_debug(
2198 "matched Telechips TCC signature in /proc/cpuinfo Hardware string \"%.*s\"",
2199 (int) hardware_length, hardware);
2200 return chipset;
2201 }
2202
2203 /* Compare to tabulated Hardware values for popular chipsets/devices which can't be otherwise detected */
2204 for (size_t i = 0; i < CPUINFO_COUNT_OF(special_hardware_map_entries); i++) {
2205 if (strncmp(special_hardware_map_entries[i].platform, hardware, hardware_length) == 0 &&
2206 special_hardware_map_entries[i].platform[hardware_length] == 0)
2207 {
2208 cpuinfo_log_debug(
2209 "found /proc/cpuinfo Hardware string \"%.*s\" in special chipset table",
2210 (int) hardware_length, hardware);
2211 /* Create chipset name from entry */
2212 return (struct cpuinfo_arm_chipset) {
2213 .vendor = chipset_series_vendor[special_hardware_map_entries[i].series],
2214 .series = (enum cpuinfo_arm_chipset_series) special_hardware_map_entries[i].series,
2215 .model = special_hardware_map_entries[i].model,
2216 .suffix = {
2217 [0] = special_hardware_map_entries[i].suffix,
2218 },
2219 };
2220 }
2221 }
2222 }
2223
2224 return (struct cpuinfo_arm_chipset) {
2225 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2226 .series = cpuinfo_arm_chipset_series_unknown,
2227 };
2228}
2229
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002230#ifdef __ANDROID__
2231 static const struct special_map_entry special_board_map_entries[] = {
Marat Dukhan006461a2017-08-24 16:10:46 -07002232 {
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002233 /* "hi6250" -> HiSilicon Kirin 650 */
2234 .platform = "hi6250",
2235 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2236 .model = 650,
2237 },
2238 {
2239 /* "hi6210sft" -> HiSilicon Kirin 620 */
2240 .platform = "hi6210sft",
2241 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2242 .model = 620,
2243 },
2244 {
2245 /* "hi3650" -> HiSilicon Kirin 950 */
2246 .platform = "hi3650",
2247 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2248 .model = 950,
2249 },
2250 {
2251 /* "hi3635" -> HiSilicon Kirin 930 */
2252 .platform = "hi3635",
2253 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2254 .model = 930,
2255 },
2256 {
2257 /* "hi3630" -> HiSilicon Kirin 920 */
2258 .platform = "hi3630",
2259 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2260 .model = 920,
2261 },
2262 {
2263 /* "mp523x" -> Renesas MP5232 */
2264 .platform = "mp523x",
2265 .series = cpuinfo_arm_chipset_series_renesas_mp,
2266 .model = 5232,
2267 },
2268 {
2269 /* "piranha" -> Texas Instruments OMAP4430 */
2270 .platform = "piranha",
Marat Dukhan006461a2017-08-24 16:10:46 -07002271 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
2272 .model = 4430,
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002273 },
2274 {
2275 /* "BEETHOVEN" (Huawei MadiaPad M3) -> HiSilicon Kirin 950 */
2276 .platform = "BEETHOVEN",
2277 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2278 .model = 950,
2279 },
2280 {
2281 /* "hws7701u" (Huawei MediaPad 7 Youth) -> Rockchip RK3168 */
2282 .platform = "hws7701u",
2283 .series = cpuinfo_arm_chipset_series_rockchip_rk,
2284 .model = 3168,
2285 },
2286 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002287 /* "g2mv" (LG G2 mini LTE) -> Nvidia Tegra SL460N */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002288 .platform = "g2mv",
2289 .series = cpuinfo_arm_chipset_series_nvidia_tegra_sl,
2290 .model = 460,
2291 .suffix = 'N',
2292 },
2293 {
2294 /* "K00F" (Asus MeMO Pad 10) -> Rockchip RK3188 */
2295 .platform = "K00F",
2296 .series = cpuinfo_arm_chipset_series_rockchip_rk,
2297 .model = 3188,
2298 },
2299 {
2300 /* "T7H" (HP Slate 7) -> Rockchip RK3066 */
2301 .platform = "T7H",
2302 .series = cpuinfo_arm_chipset_series_rockchip_rk,
2303 .model = 3066,
2304 },
2305 {
2306 /* "tuna" (Samsung Galaxy Nexus) -> Texas Instruments OMAP4460 */
2307 .platform = "tuna",
2308 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
2309 .model = 4460,
2310 },
2311 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002312 /* "grouper" (Asus Nexus 7 2012) -> Nvidia Tegra T30L */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002313 .platform = "grouper",
2314 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2315 .model = 30,
2316 .suffix = 'L',
2317 },
2318 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002319 /* "flounder" (HTC Nexus 9) -> Nvidia Tegra T132 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002320 .platform = "flounder",
2321 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2322 .model = 132,
2323 },
2324 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002325 /* "dragon" (Google Pixel C) -> Nvidia Tegra T210 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002326 .platform = "dragon",
2327 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2328 .model = 210,
2329 },
2330 {
2331 /* "sailfish" (Google Pixel) -> Qualcomm MSM8996PRO */
2332 .platform = "sailfish",
2333 .series = cpuinfo_arm_chipset_series_qualcomm_msm,
2334 .model = 8996,
2335 .suffix = 'P',
2336 },
2337 {
2338 /* "marlin" (Google Pixel XL) -> Qualcomm MSM8996PRO */
2339 .platform = "marlin",
2340 .series = cpuinfo_arm_chipset_series_qualcomm_msm,
2341 .model = 8996,
2342 .suffix = 'P',
2343 },
2344 };
Marat Dukhan006461a2017-08-24 16:10:46 -07002345
2346 /*
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002347 * Decodes chipset name from ro.product.board Android system property.
2348 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
2349 *
2350 * @param[in] platform - ro.product.board value.
2351 * @param cores - number of cores in the chipset.
2352 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2353 *
2354 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2355 * and series identifiers.
Marat Dukhan006461a2017-08-24 16:10:46 -07002356 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002357 struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_product_board(
2358 const char ro_product_board[restrict static CPUINFO_BUILD_PROP_VALUE_MAX],
2359 uint32_t cores, uint32_t max_cpu_freq_max)
2360 {
2361 struct cpuinfo_arm_chipset chipset;
2362 const char* board = ro_product_board;
2363 const size_t board_length = strnlen(ro_product_board, CPUINFO_BUILD_PROP_VALUE_MAX);
2364 const char* board_end = ro_product_board + board_length;
2365
2366 /* Check Qualcomm MSM/APQ signature */
2367 if (match_msm_apq(board, board_end, &chipset)) {
2368 cpuinfo_log_debug(
2369 "matched Qualcomm MSM/APQ signature in ro.product.board string \"%.*s\"", (int) board_length, board);
2370 return chipset;
2371 }
2372
2373 /* Check universaXXXX (Samsung Exynos) signature */
2374 if (match_universal(board, board_end, &chipset)) {
2375 cpuinfo_log_debug(
2376 "matched UNIVERSAL (Samsung Exynos) signature in ro.product.board string \"%.*s\"",
2377 (int) board_length, board);
2378 return chipset;
2379 }
2380
2381 /* Check SMDK (Samsung Exynos) signature */
2382 if (match_and_parse_smdk(board, board_end, cores, &chipset)) {
2383 cpuinfo_log_debug(
2384 "matched SMDK (Samsung Exynos) signature in ro.product.board string \"%.*s\"",
2385 (int) board_length, board);
2386 return chipset;
2387 }
2388
2389 /* Check MediaTek MT signature */
2390 if (match_mt(board, board_end, true, &chipset)) {
2391 cpuinfo_log_debug(
2392 "matched MediaTek MT signature in ro.product.board string \"%.*s\"",
2393 (int) board_length, board);
2394 return chipset;
2395 }
2396
2397 /* Check Spreadtrum SC signature */
2398 if (match_sc(board, board_end, &chipset)) {
2399 cpuinfo_log_debug(
2400 "matched Spreadtrum SC signature in ro.product.board string \"%.*s\"",
2401 (int) board_length, board);
2402 return chipset;
2403 }
2404
2405 /* Check Marvell PXA signature */
2406 if (match_pxa(board, board_end, &chipset)) {
2407 cpuinfo_log_debug(
2408 "matched Marvell PXA signature in ro.product.board string \"%.*s\"",
2409 (int) board_length, board);
2410 return chipset;
2411 }
2412
2413 /* Check Leadcore LCxxxx signature */
2414 if (match_lc(board, board_end, &chipset)) {
2415 cpuinfo_log_debug(
2416 "matched Leadcore LC signature in ro.product.board string \"%.*s\"",
2417 (int) board_length, board);
2418 return chipset;
2419 }
2420
2421 /*
2422 * Compare to tabulated ro.product.board values for Broadcom chipsets and decode chipset from frequency and
2423 * number of cores.
2424 */
2425 if (match_and_parse_broadcom(board, board_end, cores, max_cpu_freq_max, &chipset)) {
2426 cpuinfo_log_debug(
2427 "found ro.product.board string \"%.*s\" in Broadcom chipset table",
2428 (int) board_length, board);
2429 return chipset;
2430 }
2431
2432 /* Compare to tabulated ro.product.board values for Huawei devices which don't report chipset elsewhere */
2433 if (match_and_parse_huawei(board, board_end, &chipset)) {
2434 cpuinfo_log_debug(
2435 "found ro.product.board string \"%.*s\" in Huawei chipset table",
2436 (int) board_length, board);
2437 return chipset;
2438 }
2439
2440 /* Compare to tabulated ro.product.board values for popular chipsets/devices which can't be otherwise detected */
2441 for (size_t i = 0; i < CPUINFO_COUNT_OF(special_board_map_entries); i++) {
2442 if (strncmp(special_board_map_entries[i].platform, board, board_length) == 0 &&
2443 special_board_map_entries[i].platform[board_length] == 0)
2444 {
Marat Dukhan006461a2017-08-24 16:10:46 -07002445 cpuinfo_log_debug(
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002446 "found ro.product.board string \"%.*s\" in special chipset table",
2447 (int) board_length, board);
Marat Dukhan006461a2017-08-24 16:10:46 -07002448 /* Create chipset name from entry */
2449 return (struct cpuinfo_arm_chipset) {
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002450 .vendor = chipset_series_vendor[special_board_map_entries[i].series],
2451 .series = (enum cpuinfo_arm_chipset_series) special_board_map_entries[i].series,
2452 .model = special_board_map_entries[i].model,
Marat Dukhan006461a2017-08-24 16:10:46 -07002453 .suffix = {
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002454 [0] = special_board_map_entries[i].suffix,
2455 /* The suffix of MSM8996PRO is truncated at the first letter, reconstruct it here. */
2456 [1] = special_board_map_entries[i].suffix == 'P' ? 'R' : 0,
2457 [2] = special_board_map_entries[i].suffix == 'P' ? 'O' : 0,
Marat Dukhan006461a2017-08-24 16:10:46 -07002458 },
2459 };
2460 }
2461 }
Marat Dukhan006461a2017-08-24 16:10:46 -07002462
2463 return (struct cpuinfo_arm_chipset) {
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002464 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2465 .series = cpuinfo_arm_chipset_series_unknown,
Marat Dukhan006461a2017-08-24 16:10:46 -07002466 };
2467 }
2468
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002469 struct amlogic_map_entry {
2470 char ro_board_platform[6];
2471 uint16_t model;
2472 uint8_t series;
2473 char suffix[3];
Marat Dukhan006461a2017-08-24 16:10:46 -07002474 };
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002475
2476 static const struct amlogic_map_entry amlogic_map_entries[] = {
2477 {
2478 /* "meson3" -> Amlogic AML8726-M */
2479 .ro_board_platform = "meson3",
2480 .series = cpuinfo_arm_chipset_series_amlogic_aml,
2481 .model = 8726,
2482 .suffix = "-M",
2483 },
2484 {
2485 /* "meson6" -> Amlogic AML8726-MX */
2486 .ro_board_platform = "meson6",
2487 .series = cpuinfo_arm_chipset_series_amlogic_aml,
2488 .model = 8726,
2489 .suffix = "-MX",
2490 },
2491 {
2492 /* "meson8" -> Amlogic S805 */
2493 .ro_board_platform = "meson8",
2494 .series = cpuinfo_arm_chipset_series_amlogic_s,
2495 .model = 805,
2496 },
2497 {
2498 /* "gxbaby" -> Amlogic S905 */
2499 .ro_board_platform = "gxbaby",
2500 .series = cpuinfo_arm_chipset_series_amlogic_s,
2501 .model = 905,
2502 },
2503 {
2504 /* "gxl" -> Amlogic S905X */
2505 .ro_board_platform = "gxl",
2506 .series = cpuinfo_arm_chipset_series_amlogic_s,
2507 .model = 905,
2508 .suffix = "X",
2509 },
2510 {
2511 /* "gxm" -> Amlogic S912 */
2512 .ro_board_platform = "gxm",
2513 .series = cpuinfo_arm_chipset_series_amlogic_s,
2514 .model = 912,
2515 },
2516 };
2517
2518 static const struct special_map_entry special_platform_map_entries[] = {
2519 {
2520 /* "hi6620oem" -> HiSilicon Kirin 910T */
2521 .platform = "hi6620oem",
2522 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2523 .model = 910,
2524 .suffix = 'T',
2525 },
2526 {
2527 /* "hi6250" -> HiSilicon Kirin 650 */
2528 .platform = "hi6250",
2529 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2530 .model = 650,
2531 },
2532 {
2533 /* "hi6210sft" -> HiSilicon Kirin 620 */
2534 .platform = "hi6210sft",
2535 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2536 .model = 620,
2537 },
2538 {
2539 /* "hi3650" -> HiSilicon Kirin 950 */
2540 .platform = "hi3650",
2541 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2542 .model = 950,
2543 },
2544 {
2545 /* "hi3635" -> HiSilicon Kirin 930 */
2546 .platform = "hi3635",
2547 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2548 .model = 930,
2549 },
2550 {
2551 /* "hi3630" -> HiSilicon Kirin 920 */
2552 .platform = "hi3630",
2553 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2554 .model = 920,
2555 },
2556 {
2557 /* "k3v2oem1" -> HiSilicon K3V2 */
2558 .platform = "k3v2oem1",
2559 .series = cpuinfo_arm_chipset_series_hisilicon_k3v,
2560 .model = 2,
2561 },
2562 {
2563 /* "k3v200" -> HiSilicon K3V2 */
2564 .platform = "k3v200",
2565 .series = cpuinfo_arm_chipset_series_hisilicon_k3v,
2566 .model = 2,
2567 },
2568 {
2569 /* "montblanc" -> NovaThor U8500 */
2570 .platform = "montblanc",
2571 .series = cpuinfo_arm_chipset_series_novathor_u,
2572 .model = 8500,
2573 },
2574 {
2575 /* "song" -> Pinecone Surge S1 */
2576 .platform = "song",
2577 .series = cpuinfo_arm_chipset_series_pinecone_surge_s,
2578 .model = 1,
2579 },
2580 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002581 /* "tegra132" -> Nvidia Tegra T132 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002582 .platform = "tegra132",
2583 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2584 .model = 132,
2585 },
2586 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002587 /* "tegra210_dragon" -> Nvidia Tegra T210 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002588 .platform = "tegra210_dragon",
2589 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2590 .model = 210,
2591 },
2592 {
Marat Dukhan93982f22017-10-20 13:10:23 -07002593 /* "tegra4" -> Nvidia Tegra T114 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002594 .platform = "tegra4",
2595 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2596 .model = 114,
2597 },
2598 {
2599 /* "s5pc110" -> Samsung Exynos 3110 */
2600 .platform = "s5pc110",
2601 .series = cpuinfo_arm_chipset_series_samsung_exynos,
2602 .model = 3110,
2603 },
2604 };
2605
2606 /*
2607 * Decodes chipset name from ro.board.platform Android system property.
2608 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
2609 *
2610 * @param[in] platform - ro.board.platform value.
2611 * @param cores - number of cores in the chipset.
2612 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2613 *
2614 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2615 * and series identifiers.
2616 */
2617 struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_board_platform(
2618 const char platform[restrict static CPUINFO_BUILD_PROP_VALUE_MAX],
2619 uint32_t cores, uint32_t max_cpu_freq_max)
2620 {
2621 struct cpuinfo_arm_chipset chipset;
2622 const size_t platform_length = strnlen(platform, CPUINFO_BUILD_PROP_VALUE_MAX);
2623 const char* platform_end = platform + platform_length;
2624
2625 /* Check Qualcomm MSM/APQ signature */
2626 if (match_msm_apq(platform, platform_end, &chipset)) {
2627 cpuinfo_log_debug(
2628 "matched Qualcomm MSM/APQ signature in ro.board.platform string \"%.*s\"",
2629 (int) platform_length, platform);
2630 return chipset;
2631 }
2632
2633 /* Check exynosXXXX (Samsung Exynos) signature */
2634 if (match_exynos(platform, platform_end, &chipset)) {
2635 cpuinfo_log_debug(
2636 "matched exynosXXXX (Samsung Exynos) signature in ro.board.platform string \"%.*s\"",
2637 (int) platform_length, platform);
2638 return chipset;
2639 }
2640
2641 /* Check MediaTek MT signature */
2642 if (match_mt(platform, platform_end, true, &chipset)) {
2643 cpuinfo_log_debug(
2644 "matched MediaTek MT signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2645 return chipset;
2646 }
2647
Marat Dukhan166ce4c2017-11-27 14:54:43 -08002648 /* Check HiSilicon Kirin signature */
2649 if (match_kirin(platform, platform_end, &chipset)) {
2650 cpuinfo_log_debug(
2651 "matched HiSilicon Kirin signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2652 return chipset;
2653 }
2654
Marat Dukhan7c775ab2017-10-15 21:50:11 +00002655 /* Check Spreadtrum SC signature */
2656 if (match_sc(platform, platform_end, &chipset)) {
2657 cpuinfo_log_debug(
2658 "matched Spreadtrum SC signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2659 return chipset;
2660 }
2661
2662 /* Check Rockchip RK signature */
2663 if (match_rk(platform, platform_end, &chipset)) {
2664 cpuinfo_log_debug(
2665 "matched Rockchip RK signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2666 return chipset;
2667 }
2668
2669 /* Check Leadcore LCxxxx signature */
2670 if (match_lc(platform, platform_end, &chipset)) {
2671 cpuinfo_log_debug(
2672 "matched Leadcore LC signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2673 return chipset;
2674 }
2675
2676 /* Compare to tabulated ro.board.platform values for Huawei devices which don't report chipset elsewhere */
2677 if (match_and_parse_huawei(platform, platform_end, &chipset)) {
2678 cpuinfo_log_debug(
2679 "found ro.board.platform string \"%.*s\" in Huawei chipset table",
2680 (int) platform_length, platform);
2681 return chipset;
2682 }
2683
2684 /*
2685 * Compare to known ro.board.platform values for Broadcom devices and
2686 * detect chipset from frequency and number of cores
2687 */
2688 if (match_and_parse_broadcom(platform, platform_end, cores, max_cpu_freq_max, &chipset)) {
2689 cpuinfo_log_debug(
2690 "found ro.board.platform string \"%.*s\" in Broadcom chipset table",
2691 (int) platform_length, platform);
2692 return chipset;
2693 }
2694
2695 /*
2696 * Compare to ro.board.platform value ("omap4") for OMAP4xxx chipsets.
2697 * Upon successful match, detect OMAP4430 from frequency and number of cores.
2698 */
2699 if (platform_length == 5 && cores == 2 && max_cpu_freq_max == 1008000 && memcmp(platform, "omap4", 5) == 0) {
2700 cpuinfo_log_debug(
2701 "matched Texas Instruments OMAP4 signature in ro.board.platform string \"%.*s\"",
2702 (int) platform_length, platform);
2703
2704 return (struct cpuinfo_arm_chipset) {
2705 .vendor = cpuinfo_arm_chipset_vendor_texas_instruments,
2706 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
2707 .model = 4430,
2708 };
2709 }
2710
2711 /*
2712 * Compare to tabulated ro.board.platform values for Amlogic chipsets/devices which can't be otherwise detected.
2713 * The tabulated Amlogic ro.board.platform values have not more than 6 characters.
2714 */
2715 if (platform_length <= 6) {
2716 for (size_t i = 0; i < CPUINFO_COUNT_OF(amlogic_map_entries); i++) {
2717 if (strncmp(amlogic_map_entries[i].ro_board_platform, platform, 6) == 0) {
2718 cpuinfo_log_debug(
2719 "found ro.board.platform string \"%.*s\" in Amlogic chipset table",
2720 (int) platform_length, platform);
2721 /* Create chipset name from entry */
2722 return (struct cpuinfo_arm_chipset) {
2723 .vendor = cpuinfo_arm_chipset_vendor_amlogic,
2724 .series = (enum cpuinfo_arm_chipset_series) amlogic_map_entries[i].series,
2725 .model = amlogic_map_entries[i].model,
2726 .suffix = {
2727 [0] = amlogic_map_entries[i].suffix[0],
2728 [1] = amlogic_map_entries[i].suffix[1],
2729 [2] = amlogic_map_entries[i].suffix[2],
2730 },
2731 };
2732 }
2733 }
2734 }
2735
2736 /* Compare to tabulated ro.board.platform values for popular chipsets/devices which can't be otherwise detected */
2737 for (size_t i = 0; i < CPUINFO_COUNT_OF(special_platform_map_entries); i++) {
2738 if (strncmp(special_platform_map_entries[i].platform, platform, platform_length) == 0 &&
2739 special_platform_map_entries[i].platform[platform_length] == 0)
2740 {
2741 /* Create chipset name from entry */
2742 cpuinfo_log_debug(
2743 "found ro.board.platform string \"%.*s\" in special chipset table", (int) platform_length, platform);
2744 return (struct cpuinfo_arm_chipset) {
2745 .vendor = chipset_series_vendor[special_platform_map_entries[i].series],
2746 .series = (enum cpuinfo_arm_chipset_series) special_platform_map_entries[i].series,
2747 .model = special_platform_map_entries[i].model,
2748 .suffix = {
2749 [0] = special_platform_map_entries[i].suffix,
2750 },
2751 };
2752 }
2753 }
2754
2755 /* None of the ro.board.platform signatures matched, indicate unknown chipset */
2756 return (struct cpuinfo_arm_chipset) {
2757 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2758 .series = cpuinfo_arm_chipset_series_unknown,
2759 };
2760 }
2761
2762 /*
2763 * Decodes chipset name from ro.mediatek.platform Android system property.
2764 *
2765 * @param[in] platform - ro.mediatek.platform value.
2766 *
2767 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2768 * and series identifiers.
2769 */
2770 struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_mediatek_platform(
2771 const char platform[restrict static CPUINFO_BUILD_PROP_VALUE_MAX])
2772 {
2773 struct cpuinfo_arm_chipset chipset;
2774 const char* platform_end = platform + strnlen(platform, CPUINFO_BUILD_PROP_VALUE_MAX);;
2775
2776 /* Check MediaTek MT signature */
2777 if (match_mt(platform, platform_end, false, &chipset)) {
2778 return chipset;
2779 }
2780
2781 return (struct cpuinfo_arm_chipset) {
2782 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2783 .series = cpuinfo_arm_chipset_series_unknown,
2784 };
2785 }
2786
2787 /*
2788 * Decodes chipset name from ro.chipname Android system property.
2789 *
2790 * @param[in] chipname - ro.chipname value.
2791 *
2792 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2793 * and series identifiers.
2794 */
2795 struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_chipname(
2796 const char chipname[restrict static CPUINFO_BUILD_PROP_VALUE_MAX])
2797 {
2798 struct cpuinfo_arm_chipset chipset;
2799 const size_t chipname_length = strnlen(chipname, CPUINFO_BUILD_PROP_VALUE_MAX);
2800 const char* chipname_end = chipname + chipname_length;
2801
2802 /* Check Qualcomm MSM/APQ signatures */
2803 if (match_msm_apq(chipname, chipname_end, &chipset)) {
2804 cpuinfo_log_debug(
2805 "matched Qualcomm MSM/APQ signature in ro.chipname string \"%.*s\"",
2806 (int) chipname_length, chipname);
2807 return chipset;
2808 }
2809
2810 /* Check exynosXXXX (Samsung Exynos) signature */
2811 if (match_exynos(chipname, chipname_end, &chipset)) {
2812 cpuinfo_log_debug(
2813 "matched exynosXXXX (Samsung Exynos) signature in ro.chipname string \"%.*s\"",
2814 (int) chipname_length, chipname);
2815 return chipset;
2816 }
2817
2818 /* Check universalXXXX (Samsung Exynos) signature */
2819 if (match_universal(chipname, chipname_end, &chipset)) {
2820 cpuinfo_log_debug(
2821 "matched UNIVERSAL (Samsung Exynos) signature in ro.chipname Hardware string \"%.*s\"",
2822 (int) chipname_length, chipname);
2823 return chipset;
2824 }
2825
2826 /* Check MediaTek MT signature */
2827 if (match_mt(chipname, chipname_end, true, &chipset)) {
2828 cpuinfo_log_debug(
2829 "matched MediaTek MT signature in ro.chipname string \"%.*s\"",
2830 (int) chipname_length, chipname);
2831 return chipset;
2832 }
2833
2834 /* Check Spreadtrum SC signature */
2835 if (match_sc(chipname, chipname_end, &chipset)) {
2836 cpuinfo_log_debug(
2837 "matched Spreadtrum SC signature in ro.chipname string \"%.*s\"",
2838 (int) chipname_length, chipname);
2839 return chipset;
2840 }
2841
2842 /* Check Marvell PXA signature */
2843 if (match_pxa(chipname, chipname_end, &chipset)) {
2844 cpuinfo_log_debug(
2845 "matched Marvell PXA signature in ro.chipname string \"%.*s\"",
2846 (int) chipname_length, chipname);
2847 return chipset;
2848 }
2849
2850 /* Compare to ro.chipname value ("mp523x") for Renesas MP5232 which can't be otherwise detected */
2851 if (chipname_length == 6 && memcmp(chipname, "mp523x", 6) == 0) {
2852 cpuinfo_log_debug(
2853 "matched Renesas MP5232 signature in ro.chipname string \"%.*s\"",
2854 (int) chipname_length, chipname);
2855
2856 return (struct cpuinfo_arm_chipset) {
2857 .vendor = cpuinfo_arm_chipset_vendor_renesas,
2858 .series = cpuinfo_arm_chipset_series_renesas_mp,
2859 .model = 5232,
2860 };
2861 }
2862
2863 return (struct cpuinfo_arm_chipset) {
2864 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2865 .series = cpuinfo_arm_chipset_series_unknown,
2866 };
2867 }
2868#endif /* __ANDROID__ */
Marat Dukhan006461a2017-08-24 16:10:46 -07002869
2870/*
2871 * Fix common bugs, typos, and renames in chipset name.
2872 *
2873 * @param[in,out] chipset - chipset name to fix.
2874 * @param cores - number of cores in the chipset.
2875 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2876 */
2877void cpuinfo_arm_fixup_chipset(
2878 struct cpuinfo_arm_chipset chipset[restrict static 1], uint32_t cores, uint32_t max_cpu_freq_max)
2879{
2880 switch (chipset->series) {
2881 case cpuinfo_arm_chipset_series_qualcomm_msm:
2882 /* Check if there is suffix */
2883 if (chipset->suffix[0] == 0) {
2884 /* No suffix, but the model may be misreported */
2885 switch (chipset->model) {
2886 case 8216:
2887 /* MSM8216 was renamed to MSM8916 */
2888 cpuinfo_log_info("reinterpreted MSM8216 chipset as MSM8916");
2889 chipset->model = 8916;
2890 break;
2891 case 8916:
2892 /* Common bug: MSM8939 (Octa-core) reported as MSM8916 (Quad-core) */
2893 switch (cores) {
2894 case 4:
2895 break;
2896 case 8:
2897 cpuinfo_log_info("reinterpreted MSM8916 chipset with 8 cores as MSM8939");
2898 chipset->model = 8939;
2899 break;
2900 default:
2901 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2902 cores, chipset->model);
2903 chipset->model = 0;
2904 }
2905 break;
2906 case 8937:
2907 /* Common bug: MSM8917 (Quad-core) reported as MSM8937 (Octa-core) */
2908 switch (cores) {
2909 case 4:
2910 cpuinfo_log_info("reinterpreted MSM8937 chipset with 4 cores as MSM8917");
2911 chipset->model = 8917;
2912 break;
2913 case 8:
2914 break;
2915 default:
2916 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2917 cores, chipset->model);
2918 chipset->model = 0;
2919 }
2920 break;
2921 case 8960:
2922 /* Common bug: APQ8064 (Quad-core) reported as MSM8960 (Dual-core) */
2923 switch (cores) {
2924 case 2:
2925 break;
2926 case 4:
2927 cpuinfo_log_info("reinterpreted MSM8960 chipset with 4 cores as APQ8064");
2928 chipset->series = cpuinfo_arm_chipset_series_qualcomm_apq;
2929 chipset->model = 8064;
2930 break;
2931 default:
2932 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2933 cores, chipset->model);
2934 chipset->model = 0;
2935 }
2936 break;
2937 case 8996:
2938 /* Common bug: MSM8994 (Octa-core) reported as MSM8996 (Quad-core) */
2939 switch (cores) {
2940 case 4:
2941 break;
2942 case 8:
2943 cpuinfo_log_info("reinterpreted MSM8996 chipset with 8 cores as MSM8994");
2944 chipset->model = 8994;
2945 break;
2946 default:
2947 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2948 cores, chipset->model);
2949 chipset->model = 0;
2950 }
2951 break;
2952 case 8610:
2953 /* Common bug: MSM8212 (Quad-core) reported as MSM8610 (Dual-core) */
2954 switch (cores) {
2955 case 2:
2956 break;
2957 case 4:
2958 cpuinfo_log_info("reinterpreted MSM8610 chipset with 4 cores as MSM8212");
2959 chipset->model = 8212;
2960 break;
2961 default:
2962 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2963 cores, chipset->model);
2964 chipset->model = 0;
2965 }
2966 break;
2967 }
2968 } else {
2969 /* Suffix may need correction */
2970 const uint32_t suffix_word = load_u32le(chipset->suffix);
2971 if (suffix_word == UINT32_C(0x004D534D) /* "\0MSM" = reverse("MSM\0") */) {
2972 /*
2973 * Common bug: model name repeated twice, e.g. "MSM8916MSM8916"
2974 * In this case, model matching code parses the second "MSM" as a suffix
2975 */
2976 chipset->suffix[0] = 0;
2977 chipset->suffix[1] = 0;
2978 chipset->suffix[2] = 0;
2979 } else {
2980 switch (chipset->model) {
2981 case 8976:
2982 /* MSM8976SG -> MSM8976PRO */
2983 if (suffix_word == UINT32_C(0x00004753) /* "\0\0GS" = reverse("SG\0\0") */ ) {
2984 chipset->suffix[0] = 'P';
2985 chipset->suffix[1] = 'R';
2986 chipset->suffix[2] = 'O';
2987 }
2988 break;
2989 case 8996:
2990 /* MSM8996PRO -> MSM8996PRO-AB or MSM8996PRO-AC */
2991 if (suffix_word == UINT32_C(0x004F5250) /* "\0ORP" = reverse("PRO\0") */ ) {
2992 chipset->suffix[3] = '-';
2993 chipset->suffix[4] = 'A';
2994 chipset->suffix[5] = 'B' + (char) (max_cpu_freq_max >= 2188800);
2995 }
2996 break;
2997 }
2998 }
2999 }
3000 break;
3001 case cpuinfo_arm_chipset_series_qualcomm_apq:
3002 {
3003 /* Suffix may need correction */
3004 const uint32_t expected_apq = load_u32le(chipset->suffix);
3005 if (expected_apq == UINT32_C(0x00515041) /* "\0QPA" = reverse("APQ\0") */) {
3006 /*
3007 * Common bug: model name repeated twice, e.g. "APQ8016APQ8016"
3008 * In this case, model matching code parses the second "APQ" as a suffix
3009 */
3010 chipset->suffix[0] = 0;
3011 chipset->suffix[1] = 0;
3012 chipset->suffix[2] = 0;
3013 }
3014 break;
3015 }
3016 case cpuinfo_arm_chipset_series_samsung_exynos:
3017 if (chipset->model == 7580) {
3018 /* Common bug: Exynos 7578 (Quad-core) reported as Exynos 7580 (Octa-core) */
3019 switch (cores) {
3020 case 4:
3021 cpuinfo_log_info("reinterpreted Exynos 7580 chipset with 4 cores as Exynos 7578");
3022 chipset->model = 7578;
3023 break;
3024 case 8:
3025 break;
3026 default:
3027 cpuinfo_log_warning("system reported invalid %"PRIu32"-core Exynos 7580 chipset", cores);
3028 chipset->model = 0;
3029 }
3030 }
3031 break;
3032 case cpuinfo_arm_chipset_series_mediatek_mt:
3033 if (chipset->model == 6752) {
3034 /* Common bug: MT6732 (Quad-core) reported as MT6752 (Octa-core) */
3035 switch (cores) {
3036 case 4:
3037 cpuinfo_log_info("reinterpreted MT6752 chipset with 4 cores as MT6732");
3038 chipset->model = 6732;
3039 break;
3040 case 8:
3041 break;
3042 default:
3043 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MT6752 chipset", cores);
3044 chipset->model = 0;
3045 }
3046 }
3047 if (chipset->suffix[0] == 'T') {
3048 /* Normalization: "TURBO" and "TRUBO" (apparently a typo) -> "T" */
3049 const uint32_t suffix_word = load_u32le(chipset->suffix + 1);
3050 switch (suffix_word) {
3051 case UINT32_C(0x4F425255): /* "OBRU" = reverse("URBO") */
3052 case UINT32_C(0x4F425552): /* "OBUR" = reverse("RUBO") */
3053 if (chipset->suffix[5] == 0) {
3054 chipset->suffix[1] = 0;
3055 chipset->suffix[2] = 0;
3056 chipset->suffix[3] = 0;
3057 chipset->suffix[4] = 0;
3058 }
3059 break;
3060 }
3061 }
3062 break;
3063 default:
3064 break;
3065 }
3066}
3067
3068/* Map from ARM chipset vendor ID to its string representation */
3069static const char* chipset_vendor_string[cpuinfo_arm_chipset_vendor_max] = {
3070 [cpuinfo_arm_chipset_vendor_unknown] = "Unknown",
3071 [cpuinfo_arm_chipset_vendor_qualcomm] = "Qualcomm",
3072 [cpuinfo_arm_chipset_vendor_mediatek] = "MediaTek",
3073 [cpuinfo_arm_chipset_vendor_samsung] = "Samsung",
3074 [cpuinfo_arm_chipset_vendor_hisilicon] = "HiSilicon",
3075 [cpuinfo_arm_chipset_vendor_actions] = "Actions",
3076 [cpuinfo_arm_chipset_vendor_allwinner] = "Allwinner",
3077 [cpuinfo_arm_chipset_vendor_amlogic] = "Amlogic",
3078 [cpuinfo_arm_chipset_vendor_broadcom] = "Broadcom",
3079 [cpuinfo_arm_chipset_vendor_lg] = "LG",
3080 [cpuinfo_arm_chipset_vendor_leadcore] = "Leadcore",
3081 [cpuinfo_arm_chipset_vendor_marvell] = "Marvell",
3082 [cpuinfo_arm_chipset_vendor_mstar] = "MStar",
3083 [cpuinfo_arm_chipset_vendor_novathor] = "NovaThor",
Marat Dukhan93982f22017-10-20 13:10:23 -07003084 [cpuinfo_arm_chipset_vendor_nvidia] = "Nvidia",
Marat Dukhan006461a2017-08-24 16:10:46 -07003085 [cpuinfo_arm_chipset_vendor_pinecone] = "Pinecone",
3086 [cpuinfo_arm_chipset_vendor_renesas] = "Renesas",
3087 [cpuinfo_arm_chipset_vendor_rockchip] = "Rockchip",
3088 [cpuinfo_arm_chipset_vendor_spreadtrum] = "Spreadtrum",
3089 [cpuinfo_arm_chipset_vendor_telechips] = "Telechips",
3090 [cpuinfo_arm_chipset_vendor_texas_instruments] = "Texas Instruments",
3091 [cpuinfo_arm_chipset_vendor_wondermedia] = "WonderMedia",
3092};
3093
3094/* Map from ARM chipset series ID to its string representation */
3095static const char* chipset_series_string[cpuinfo_arm_chipset_series_max] = {
3096 [cpuinfo_arm_chipset_series_unknown] = NULL,
3097 [cpuinfo_arm_chipset_series_qualcomm_qsd] = "QSD",
3098 [cpuinfo_arm_chipset_series_qualcomm_msm] = "MSM",
3099 [cpuinfo_arm_chipset_series_qualcomm_apq] = "APQ",
3100 [cpuinfo_arm_chipset_series_qualcomm_snapdragon] = "Snapdragon ",
3101 [cpuinfo_arm_chipset_series_mediatek_mt] = "MT",
3102 [cpuinfo_arm_chipset_series_samsung_exynos] = "Exynos ",
3103 [cpuinfo_arm_chipset_series_hisilicon_k3v] = "K3V",
3104 [cpuinfo_arm_chipset_series_hisilicon_hi] = "Hi",
3105 [cpuinfo_arm_chipset_series_hisilicon_kirin] = "Kirin ",
3106 [cpuinfo_arm_chipset_series_actions_atm] = "ATM",
3107 [cpuinfo_arm_chipset_series_allwinner_a] = "A",
3108 [cpuinfo_arm_chipset_series_amlogic_aml] = "AML",
3109 [cpuinfo_arm_chipset_series_amlogic_s] = "S",
3110 [cpuinfo_arm_chipset_series_broadcom_bcm] = "BCM",
3111 [cpuinfo_arm_chipset_series_lg_nuclun] = "Nuclun ",
3112 [cpuinfo_arm_chipset_series_leadcore_lc] = "LC",
3113 [cpuinfo_arm_chipset_series_marvell_pxa] = "PXA",
3114 [cpuinfo_arm_chipset_series_mstar_6a] = "6A",
3115 [cpuinfo_arm_chipset_series_novathor_u] = "U",
3116 [cpuinfo_arm_chipset_series_nvidia_tegra_t] = "Tegra T",
3117 [cpuinfo_arm_chipset_series_nvidia_tegra_ap] = "Tegra AP",
3118 [cpuinfo_arm_chipset_series_nvidia_tegra_sl] = "Tegra SL",
3119 [cpuinfo_arm_chipset_series_pinecone_surge_s] = "Surge S",
3120 [cpuinfo_arm_chipset_series_renesas_mp] = "MP",
3121 [cpuinfo_arm_chipset_series_rockchip_rk] = "RK",
3122 [cpuinfo_arm_chipset_series_spreadtrum_sc] = "SC",
3123 [cpuinfo_arm_chipset_series_telechips_tcc] = "TCC",
3124 [cpuinfo_arm_chipset_series_texas_instruments_omap] = "OMAP",
3125 [cpuinfo_arm_chipset_series_wondermedia_wm] = "WM",
3126};
3127
3128/* Convert chipset name represented by cpuinfo_arm_chipset structure to a string representation */
3129void cpuinfo_arm_chipset_to_string(
3130 const struct cpuinfo_arm_chipset chipset[restrict static 1],
3131 char name[restrict static CPUINFO_ARM_CHIPSET_NAME_MAX])
3132{
3133 enum cpuinfo_arm_chipset_vendor vendor = chipset->vendor;
3134 if (vendor >= cpuinfo_arm_chipset_vendor_max) {
3135 vendor = cpuinfo_arm_chipset_vendor_unknown;
3136 }
3137 enum cpuinfo_arm_chipset_series series = chipset->series;
3138 if (series >= cpuinfo_arm_chipset_series_max) {
3139 series = cpuinfo_arm_chipset_series_unknown;
3140 }
3141 const char* vendor_string = chipset_vendor_string[vendor];
3142 const char* series_string = chipset_series_string[series];
3143 const uint32_t model = chipset->model;
3144 if (model == 0) {
3145 if (series == cpuinfo_arm_chipset_series_unknown) {
3146 strncpy(name, vendor_string, CPUINFO_ARM_CHIPSET_NAME_MAX);
3147 } else {
3148 snprintf(name, CPUINFO_ARM_CHIPSET_NAME_MAX,
3149 "%s %s", vendor_string, series_string);
3150 }
3151 } else {
3152 const size_t suffix_length = strnlen(chipset->suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX);
3153 snprintf(name, CPUINFO_ARM_CHIPSET_NAME_MAX,
3154 "%s %s%"PRIu32"%.*s", vendor_string, series_string, model, (int) suffix_length, chipset->suffix);
3155 }
3156}
3157
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003158#ifdef __ANDROID__
3159 static inline struct cpuinfo_arm_chipset disambiguate_qualcomm_chipset(
3160 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3161 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3162 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1],
3163 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3164 {
3165 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3166 return *ro_chipname_chipset;
3167 }
3168 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3169 return *proc_cpuinfo_hardware_chipset;
3170 }
3171 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3172 return *ro_product_board_chipset;
3173 }
Marat Dukhan006461a2017-08-24 16:10:46 -07003174 return *ro_board_platform_chipset;
3175 }
Marat Dukhan006461a2017-08-24 16:10:46 -07003176
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003177 static inline struct cpuinfo_arm_chipset disambiguate_mediatek_chipset(
3178 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3179 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3180 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1],
3181 const struct cpuinfo_arm_chipset ro_mediatek_platform_chipset[restrict static 1],
3182 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3183 {
3184 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3185 return *ro_chipname_chipset;
Marat Dukhan006461a2017-08-24 16:10:46 -07003186 }
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003187 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3188 return *proc_cpuinfo_hardware_chipset;
3189 }
3190 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3191 return *ro_product_board_chipset;
3192 }
3193 if (ro_board_platform_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3194 return *ro_board_platform_chipset;
3195 }
3196 return *ro_mediatek_platform_chipset;
Marat Dukhan006461a2017-08-24 16:10:46 -07003197 }
3198
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003199 static inline struct cpuinfo_arm_chipset disambiguate_hisilicon_chipset(
3200 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3201 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3202 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1])
3203 {
3204 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3205 return *proc_cpuinfo_hardware_chipset;
3206 }
3207 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3208 return *ro_product_board_chipset;
3209 }
3210 return *ro_board_platform_chipset;
3211 }
3212
3213 static inline struct cpuinfo_arm_chipset disambiguate_amlogic_chipset(
3214 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3215 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1])
3216 {
3217 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3218 return *proc_cpuinfo_hardware_chipset;
3219 }
3220 return *ro_board_platform_chipset;
3221 }
3222
3223 static inline struct cpuinfo_arm_chipset disambiguate_marvell_chipset(
3224 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3225 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3226 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3227 {
3228 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3229 return *ro_chipname_chipset;
3230 }
3231 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3232 return *ro_product_board_chipset;
3233 }
3234 return *proc_cpuinfo_hardware_chipset;
3235 }
3236
3237 static inline struct cpuinfo_arm_chipset disambiguate_rockchip_chipset(
3238 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3239 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3240 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1])
3241 {
3242 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3243 return *ro_product_board_chipset;
3244 }
3245 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3246 return *proc_cpuinfo_hardware_chipset;
3247 }
3248 return *ro_board_platform_chipset;
3249 }
3250
3251 static inline struct cpuinfo_arm_chipset disambiguate_spreadtrum_chipset(
3252 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3253 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3254 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1],
3255 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3256 {
3257 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3258 return *ro_chipname_chipset;
3259 }
3260 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3261 return *ro_product_board_chipset;
3262 }
3263 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3264 return *proc_cpuinfo_hardware_chipset;
3265 }
3266 return *ro_board_platform_chipset;
Marat Dukhan006461a2017-08-24 16:10:46 -07003267 }
3268
3269 /*
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003270 * Decodes chipset name from Android system properties:
3271 * - /proc/cpuinfo Hardware string
3272 * - ro.product.board
3273 * - ro.board.platform
3274 * - ro.mediatek.platform
3275 * - ro.chipname
3276 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
3277 *
3278 * @param[in] properties - structure with the Android system properties described above.
3279 * @param cores - number of cores in the chipset.
3280 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
3281 *
3282 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
3283 * and series identifiers.
Marat Dukhan006461a2017-08-24 16:10:46 -07003284 */
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003285 struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset(
3286 const struct cpuinfo_android_properties properties[restrict static 1],
3287 uint32_t cores,
3288 uint32_t max_cpu_freq_max)
3289 {
3290 struct cpuinfo_arm_chipset chipset = {
3291 .vendor = cpuinfo_arm_chipset_vendor_unknown,
3292 .series = cpuinfo_arm_chipset_series_unknown,
3293 };
3294
3295 const bool tegra_platform = is_tegra(
3296 properties->ro_board_platform,
3297 properties->ro_board_platform + strnlen(properties->ro_board_platform, CPUINFO_BUILD_PROP_VALUE_MAX));
3298
3299 struct cpuinfo_arm_chipset chipsets[cpuinfo_android_chipset_property_max] = {
3300 [cpuinfo_android_chipset_property_proc_cpuinfo_hardware] =
Marat Dukhan1415d7d2017-10-16 09:40:15 -07003301 cpuinfo_arm_linux_decode_chipset_from_proc_cpuinfo_hardware(
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003302 properties->proc_cpuinfo_hardware, cores, max_cpu_freq_max, tegra_platform),
3303 [cpuinfo_android_chipset_property_ro_product_board] =
3304 cpuinfo_arm_android_decode_chipset_from_ro_product_board(
3305 properties->ro_product_board, cores, max_cpu_freq_max),
3306 [cpuinfo_android_chipset_property_ro_board_platform] =
3307 cpuinfo_arm_android_decode_chipset_from_ro_board_platform(
3308 properties->ro_board_platform, cores, max_cpu_freq_max),
3309 [cpuinfo_android_chipset_property_ro_mediatek_platform] =
3310 cpuinfo_arm_android_decode_chipset_from_ro_mediatek_platform(properties->ro_mediatek_platform),
3311 [cpuinfo_android_chipset_property_ro_chipname] =
3312 cpuinfo_arm_android_decode_chipset_from_ro_chipname(properties->ro_chipname),
3313 };
3314 enum cpuinfo_arm_chipset_vendor vendor = cpuinfo_arm_chipset_vendor_unknown;
3315 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3316 const enum cpuinfo_arm_chipset_vendor decoded_vendor = chipsets[i].vendor;
3317 if (decoded_vendor != cpuinfo_arm_chipset_vendor_unknown) {
3318 if (vendor == cpuinfo_arm_chipset_vendor_unknown) {
3319 vendor = decoded_vendor;
3320 } else if (vendor != decoded_vendor) {
3321 /* Parsing different system properties produces different chipset vendors. This situation is rare. */
3322 cpuinfo_log_error(
3323 "chipset detection failed: different chipset vendors reported in different system properties");
3324 goto finish;
3325 }
3326 }
3327 }
3328 if (vendor == cpuinfo_arm_chipset_vendor_unknown) {
3329 cpuinfo_log_warning(
3330 "chipset detection failed: none of the system properties matched known signatures");
3331 goto finish;
3332 }
3333
3334 /* Fix common bugs in reported chipsets */
3335 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3336 cpuinfo_arm_fixup_chipset(&chipsets[i], cores, max_cpu_freq_max);
3337 }
3338
3339 /*
3340 * Propagate suffixes: consider all pairs of chipsets, if both chipsets in the pair are from the same series,
3341 * and one's suffix is a prefix of another's chipset suffix, use the longest suffix.
3342 */
3343 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3344 const size_t chipset_i_suffix_length = strnlen(chipsets[i].suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX);
3345 for (size_t j = 0; j < i; j++) {
3346 if (chipsets[i].series == chipsets[j].series) {
3347 const size_t chipset_j_suffix_length = strnlen(chipsets[j].suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX);
3348 if (chipset_i_suffix_length != chipset_j_suffix_length) {
3349 const size_t common_prefix_length = (chipset_i_suffix_length < chipset_j_suffix_length) ?
3350 chipset_i_suffix_length : chipset_j_suffix_length;
3351 if (common_prefix_length == 0 ||
3352 memcmp(chipsets[i].suffix, chipsets[j].suffix, common_prefix_length) == 0)
3353 {
3354 if (chipset_i_suffix_length > chipset_j_suffix_length) {
3355 memcpy(chipsets[j].suffix, chipsets[i].suffix, chipset_i_suffix_length);
3356 } else {
3357 memcpy(chipsets[i].suffix, chipsets[j].suffix, chipset_j_suffix_length);
3358 }
Marat Dukhan006461a2017-08-24 16:10:46 -07003359 }
3360 }
3361 }
3362 }
3363 }
Marat Dukhan006461a2017-08-24 16:10:46 -07003364
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003365 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3366 if (chipsets[i].series != cpuinfo_arm_chipset_series_unknown) {
3367 if (chipset.series == cpuinfo_arm_chipset_series_unknown) {
3368 chipset = chipsets[i];
3369 } else if (chipsets[i].series != chipset.series || chipsets[i].model != chipset.model ||
3370 strncmp(chipsets[i].suffix, chipset.suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX) != 0)
3371 {
3372 cpuinfo_log_info(
3373 "different chipsets reported in different system properties; "
3374 "vendor-specific disambiguation heuristic would be used");
3375 switch (vendor) {
3376 case cpuinfo_arm_chipset_vendor_qualcomm:
3377 return disambiguate_qualcomm_chipset(
3378 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3379 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3380 &chipsets[cpuinfo_android_chipset_property_ro_board_platform],
3381 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3382 case cpuinfo_arm_chipset_vendor_mediatek:
3383 return disambiguate_mediatek_chipset(
3384 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3385 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3386 &chipsets[cpuinfo_android_chipset_property_ro_board_platform],
3387 &chipsets[cpuinfo_android_chipset_property_ro_mediatek_platform],
3388 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3389 case cpuinfo_arm_chipset_vendor_hisilicon:
3390 return disambiguate_hisilicon_chipset(
3391 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3392 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3393 &chipsets[cpuinfo_android_chipset_property_ro_board_platform]);
3394 case cpuinfo_arm_chipset_vendor_amlogic:
3395 return disambiguate_amlogic_chipset(
3396 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3397 &chipsets[cpuinfo_android_chipset_property_ro_board_platform]);
3398 case cpuinfo_arm_chipset_vendor_marvell:
3399 return disambiguate_marvell_chipset(
3400 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3401 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3402 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3403 case cpuinfo_arm_chipset_vendor_rockchip:
3404 return disambiguate_rockchip_chipset(
3405 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3406 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3407 &chipsets[cpuinfo_android_chipset_property_ro_board_platform]);
3408 case cpuinfo_arm_chipset_vendor_spreadtrum:
3409 return disambiguate_spreadtrum_chipset(
3410 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3411 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3412 &chipsets[cpuinfo_android_chipset_property_ro_board_platform],
3413 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3414 default:
3415 cpuinfo_log_error(
3416 "chipset detection failed: "
3417 "could not disambiguate different chipsets reported in different system properties");
3418 /* chipset variable contains valid, but inconsistent chipset information, overwrite it */
3419 chipset = (struct cpuinfo_arm_chipset) {
3420 .vendor = cpuinfo_arm_chipset_vendor_unknown,
3421 .series = cpuinfo_arm_chipset_series_unknown,
3422 };
3423 goto finish;
3424 }
Marat Dukhan006461a2017-08-24 16:10:46 -07003425 }
3426 }
3427 }
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003428
3429 finish:
3430 return chipset;
3431 }
3432#else /* !defined(__ANDROID__) */
3433
3434 /*
3435 * Decodes chipset name from /proc/cpuinfo Hardware string.
3436 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
3437 *
3438 * @param[in] hardware - /proc/cpuinfo Hardware string.
3439 * @param cores - number of cores in the chipset.
3440 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
3441 *
3442 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
3443 * and series identifiers.
3444 */
3445 struct cpuinfo_arm_chipset cpuinfo_arm_linux_decode_chipset(
3446 const char hardware[restrict static CPUINFO_HARDWARE_VALUE_MAX],
3447 uint32_t cores,
3448 uint32_t max_cpu_freq_max)
3449 {
3450 struct cpuinfo_arm_chipset chipset =
Marat Dukhan1415d7d2017-10-16 09:40:15 -07003451 cpuinfo_arm_linux_decode_chipset_from_proc_cpuinfo_hardware(
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003452 hardware, cores, max_cpu_freq_max, false);
3453 if (chipset.vendor == cpuinfo_arm_chipset_vendor_unknown) {
3454 cpuinfo_log_warning(
3455 "chipset detection failed: /proc/cpuinfo Hardware string did not match known signatures");
3456 } else {
3457 cpuinfo_arm_fixup_chipset(&chipset, cores, max_cpu_freq_max);
3458 }
3459 return chipset;
Marat Dukhan006461a2017-08-24 16:10:46 -07003460 }
3461
Marat Dukhan7c775ab2017-10-15 21:50:11 +00003462#endif