1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2017-2019, Linaro Ltd.
7 #include <linux/debugfs.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/random.h>
12 #include <linux/slab.h>
13 #include <linux/soc/qcom/smem.h>
14 #include <linux/string.h>
15 #include <linux/sys_soc.h>
16 #include <linux/types.h>
18 #include <asm/unaligned.h>
21 * SoC version type with major number in the upper 16 bits and minor
22 * number in the lower 16 bits.
24 #define SOCINFO_MAJOR(ver) (((ver) >> 16) & 0xffff)
25 #define SOCINFO_MINOR(ver) ((ver) & 0xffff)
26 #define SOCINFO_VERSION(maj, min) ((((maj) & 0xffff) << 16)|((min) & 0xffff))
28 #define SMEM_SOCINFO_BUILD_ID_LENGTH 32
29 #define SMEM_SOCINFO_CHIP_ID_LENGTH 32
32 * SMEM item id, used to acquire handles to respective
35 #define SMEM_HW_SW_BUILD_ID 137
37 #ifdef CONFIG_DEBUG_FS
38 #define SMEM_IMAGE_VERSION_BLOCKS_COUNT 32
39 #define SMEM_IMAGE_VERSION_SIZE 4096
40 #define SMEM_IMAGE_VERSION_NAME_SIZE 75
41 #define SMEM_IMAGE_VERSION_VARIANT_SIZE 20
42 #define SMEM_IMAGE_VERSION_OEM_SIZE 32
45 * SMEM Image table indices
47 #define SMEM_IMAGE_TABLE_BOOT_INDEX 0
48 #define SMEM_IMAGE_TABLE_TZ_INDEX 1
49 #define SMEM_IMAGE_TABLE_RPM_INDEX 3
50 #define SMEM_IMAGE_TABLE_APPS_INDEX 10
51 #define SMEM_IMAGE_TABLE_MPSS_INDEX 11
52 #define SMEM_IMAGE_TABLE_ADSP_INDEX 12
53 #define SMEM_IMAGE_TABLE_CNSS_INDEX 13
54 #define SMEM_IMAGE_TABLE_VIDEO_INDEX 14
55 #define SMEM_IMAGE_VERSION_TABLE 469
58 * SMEM Image table names
60 static const char *const socinfo_image_names[] = {
61 [SMEM_IMAGE_TABLE_ADSP_INDEX] = "adsp",
62 [SMEM_IMAGE_TABLE_APPS_INDEX] = "apps",
63 [SMEM_IMAGE_TABLE_BOOT_INDEX] = "boot",
64 [SMEM_IMAGE_TABLE_CNSS_INDEX] = "cnss",
65 [SMEM_IMAGE_TABLE_MPSS_INDEX] = "mpss",
66 [SMEM_IMAGE_TABLE_RPM_INDEX] = "rpm",
67 [SMEM_IMAGE_TABLE_TZ_INDEX] = "tz",
68 [SMEM_IMAGE_TABLE_VIDEO_INDEX] = "video",
71 static const char *const pmic_models[] = {
72 [0] = "Unknown PMIC model",
85 [13] = "PM8909/PM8058",
88 [16] = "PM8950/PM8027",
89 [17] = "PMI8950/ISL9519",
90 [18] = "PMK8001/PM8921",
91 [19] = "PMI8996/PM8018",
92 [20] = "PM8998/PM8015",
93 [21] = "PMI8998/PM8014",
96 [24] = "PM8005/PM8922",
116 #endif /* CONFIG_DEBUG_FS */
118 /* Socinfo SMEM item structure */
123 char build_id[SMEM_SOCINFO_BUILD_ID_LENGTH];
132 __le32 accessory_chip;
134 __le32 hw_plat_subtype;
140 __le32 pmic_die_rev_1;
142 __le32 pmic_die_rev_2;
149 __le32 pmic_array_offset;
152 __le32 raw_device_family;
153 __le32 raw_device_num;
156 char chip_id[SMEM_SOCINFO_CHIP_ID_LENGTH];
159 __le32 ncluster_array_offset;
160 __le32 num_defective_parts;
161 __le32 ndefective_parts_array_offset;
163 __le32 nmodem_supported;
166 #ifdef CONFIG_DEBUG_FS
167 struct socinfo_params {
168 u32 raw_device_family;
180 u32 ncluster_array_offset;
181 u32 num_defective_parts;
182 u32 ndefective_parts_array_offset;
183 u32 nmodem_supported;
186 struct smem_image_version {
187 char name[SMEM_IMAGE_VERSION_NAME_SIZE];
188 char variant[SMEM_IMAGE_VERSION_VARIANT_SIZE];
190 char oem[SMEM_IMAGE_VERSION_OEM_SIZE];
192 #endif /* CONFIG_DEBUG_FS */
194 struct qcom_socinfo {
195 struct soc_device *soc_dev;
196 struct soc_device_attribute attr;
197 #ifdef CONFIG_DEBUG_FS
198 struct dentry *dbg_root;
199 struct socinfo_params info;
200 #endif /* CONFIG_DEBUG_FS */
208 static const struct soc_id soc_id[] = {
216 { 138, "MSM8960AB" },
217 { 139, "APQ8060AB" },
218 { 140, "MSM8260AB" },
219 { 141, "MSM8660AB" },
222 { 153, "APQ8064AB" },
234 { 172, "APQ8064AA" },
239 { 194, "MSM8974PRO-AC" },
246 { 208, "APQ8074PRO-AA" },
247 { 209, "APQ8074PRO-AB" },
248 { 210, "APQ8074PRO-AC" },
249 { 211, "MSM8274PRO-AA" },
250 { 212, "MSM8274PRO-AB" },
251 { 213, "MSM8274PRO-AC" },
252 { 214, "MSM8674PRO-AA" },
253 { 215, "MSM8674PRO-AB" },
254 { 216, "MSM8674PRO-AC" },
255 { 217, "MSM8974PRO-AA" },
256 { 218, "MSM8974PRO-AB" },
285 { 305, "MSM8996SG" },
286 { 310, "MSM8996AU" },
287 { 311, "APQ8096AU" },
288 { 312, "APQ8096SG" },
339 static const char *socinfo_machine(struct device *dev, unsigned int id)
343 for (idx = 0; idx < ARRAY_SIZE(soc_id); idx++) {
344 if (soc_id[idx].id == id)
345 return soc_id[idx].name;
351 #ifdef CONFIG_DEBUG_FS
353 #define QCOM_OPEN(name, _func) \
354 static int qcom_open_##name(struct inode *inode, struct file *file) \
356 return single_open(file, _func, inode->i_private); \
359 static const struct file_operations qcom_ ##name## _ops = { \
360 .open = qcom_open_##name, \
362 .llseek = seq_lseek, \
363 .release = single_release, \
366 #define DEBUGFS_ADD(info, name) \
367 debugfs_create_file(__stringify(name), 0444, \
368 qcom_socinfo->dbg_root, \
369 info, &qcom_ ##name## _ops)
372 static int qcom_show_build_id(struct seq_file *seq, void *p)
374 struct socinfo *socinfo = seq->private;
376 seq_printf(seq, "%s\n", socinfo->build_id);
381 static int qcom_show_pmic_model(struct seq_file *seq, void *p)
383 struct socinfo *socinfo = seq->private;
384 int model = SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_model));
389 if (model < ARRAY_SIZE(pmic_models) && pmic_models[model])
390 seq_printf(seq, "%s\n", pmic_models[model]);
392 seq_printf(seq, "unknown (%d)\n", model);
397 static int qcom_show_pmic_model_array(struct seq_file *seq, void *p)
399 struct socinfo *socinfo = seq->private;
400 unsigned int num_pmics = le32_to_cpu(socinfo->num_pmics);
401 unsigned int pmic_array_offset = le32_to_cpu(socinfo->pmic_array_offset);
405 ptr += pmic_array_offset;
407 /* No need for bounds checking, it happened at socinfo_debugfs_init */
408 for (i = 0; i < num_pmics; i++) {
409 unsigned int model = SOCINFO_MINOR(get_unaligned_le32(ptr + 2 * i * sizeof(u32)));
410 unsigned int die_rev = get_unaligned_le32(ptr + (2 * i + 1) * sizeof(u32));
412 if (model < ARRAY_SIZE(pmic_models) && pmic_models[model])
413 seq_printf(seq, "%s %u.%u\n", pmic_models[model],
414 SOCINFO_MAJOR(die_rev),
415 SOCINFO_MINOR(die_rev));
417 seq_printf(seq, "unknown (%d)\n", model);
423 static int qcom_show_pmic_die_revision(struct seq_file *seq, void *p)
425 struct socinfo *socinfo = seq->private;
427 seq_printf(seq, "%u.%u\n",
428 SOCINFO_MAJOR(le32_to_cpu(socinfo->pmic_die_rev)),
429 SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_die_rev)));
434 static int qcom_show_chip_id(struct seq_file *seq, void *p)
436 struct socinfo *socinfo = seq->private;
438 seq_printf(seq, "%s\n", socinfo->chip_id);
443 QCOM_OPEN(build_id, qcom_show_build_id);
444 QCOM_OPEN(pmic_model, qcom_show_pmic_model);
445 QCOM_OPEN(pmic_model_array, qcom_show_pmic_model_array);
446 QCOM_OPEN(pmic_die_rev, qcom_show_pmic_die_revision);
447 QCOM_OPEN(chip_id, qcom_show_chip_id);
449 #define DEFINE_IMAGE_OPS(type) \
450 static int show_image_##type(struct seq_file *seq, void *p) \
452 struct smem_image_version *image_version = seq->private; \
453 if (image_version->type[0] != '\0') \
454 seq_printf(seq, "%s\n", image_version->type); \
457 static int open_image_##type(struct inode *inode, struct file *file) \
459 return single_open(file, show_image_##type, inode->i_private); \
462 static const struct file_operations qcom_image_##type##_ops = { \
463 .open = open_image_##type, \
465 .llseek = seq_lseek, \
466 .release = single_release, \
469 DEFINE_IMAGE_OPS(name);
470 DEFINE_IMAGE_OPS(variant);
471 DEFINE_IMAGE_OPS(oem);
473 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
474 struct socinfo *info, size_t info_size)
476 struct smem_image_version *versions;
477 struct dentry *dentry;
480 unsigned int num_pmics;
481 unsigned int pmic_array_offset;
483 qcom_socinfo->dbg_root = debugfs_create_dir("qcom_socinfo", NULL);
485 qcom_socinfo->info.fmt = __le32_to_cpu(info->fmt);
487 debugfs_create_x32("info_fmt", 0444, qcom_socinfo->dbg_root,
488 &qcom_socinfo->info.fmt);
490 switch (qcom_socinfo->info.fmt) {
491 case SOCINFO_VERSION(0, 15):
492 qcom_socinfo->info.nmodem_supported = __le32_to_cpu(info->nmodem_supported);
494 debugfs_create_u32("nmodem_supported", 0444, qcom_socinfo->dbg_root,
495 &qcom_socinfo->info.nmodem_supported);
497 case SOCINFO_VERSION(0, 14):
498 qcom_socinfo->info.num_clusters = __le32_to_cpu(info->num_clusters);
499 qcom_socinfo->info.ncluster_array_offset = __le32_to_cpu(info->ncluster_array_offset);
500 qcom_socinfo->info.num_defective_parts = __le32_to_cpu(info->num_defective_parts);
501 qcom_socinfo->info.ndefective_parts_array_offset = __le32_to_cpu(info->ndefective_parts_array_offset);
503 debugfs_create_u32("num_clusters", 0444, qcom_socinfo->dbg_root,
504 &qcom_socinfo->info.num_clusters);
505 debugfs_create_u32("ncluster_array_offset", 0444, qcom_socinfo->dbg_root,
506 &qcom_socinfo->info.ncluster_array_offset);
507 debugfs_create_u32("num_defective_parts", 0444, qcom_socinfo->dbg_root,
508 &qcom_socinfo->info.num_defective_parts);
509 debugfs_create_u32("ndefective_parts_array_offset", 0444, qcom_socinfo->dbg_root,
510 &qcom_socinfo->info.ndefective_parts_array_offset);
512 case SOCINFO_VERSION(0, 13):
513 qcom_socinfo->info.nproduct_id = __le32_to_cpu(info->nproduct_id);
515 debugfs_create_u32("nproduct_id", 0444, qcom_socinfo->dbg_root,
516 &qcom_socinfo->info.nproduct_id);
517 DEBUGFS_ADD(info, chip_id);
519 case SOCINFO_VERSION(0, 12):
520 qcom_socinfo->info.chip_family =
521 __le32_to_cpu(info->chip_family);
522 qcom_socinfo->info.raw_device_family =
523 __le32_to_cpu(info->raw_device_family);
524 qcom_socinfo->info.raw_device_num =
525 __le32_to_cpu(info->raw_device_num);
527 debugfs_create_x32("chip_family", 0444, qcom_socinfo->dbg_root,
528 &qcom_socinfo->info.chip_family);
529 debugfs_create_x32("raw_device_family", 0444,
530 qcom_socinfo->dbg_root,
531 &qcom_socinfo->info.raw_device_family);
532 debugfs_create_x32("raw_device_number", 0444,
533 qcom_socinfo->dbg_root,
534 &qcom_socinfo->info.raw_device_num);
536 case SOCINFO_VERSION(0, 11):
537 num_pmics = le32_to_cpu(info->num_pmics);
538 pmic_array_offset = le32_to_cpu(info->pmic_array_offset);
539 if (pmic_array_offset + 2 * num_pmics * sizeof(u32) <= info_size)
540 DEBUGFS_ADD(info, pmic_model_array);
542 case SOCINFO_VERSION(0, 10):
543 case SOCINFO_VERSION(0, 9):
544 qcom_socinfo->info.foundry_id = __le32_to_cpu(info->foundry_id);
546 debugfs_create_u32("foundry_id", 0444, qcom_socinfo->dbg_root,
547 &qcom_socinfo->info.foundry_id);
549 case SOCINFO_VERSION(0, 8):
550 case SOCINFO_VERSION(0, 7):
551 DEBUGFS_ADD(info, pmic_model);
552 DEBUGFS_ADD(info, pmic_die_rev);
554 case SOCINFO_VERSION(0, 6):
555 qcom_socinfo->info.hw_plat_subtype =
556 __le32_to_cpu(info->hw_plat_subtype);
558 debugfs_create_u32("hardware_platform_subtype", 0444,
559 qcom_socinfo->dbg_root,
560 &qcom_socinfo->info.hw_plat_subtype);
562 case SOCINFO_VERSION(0, 5):
563 qcom_socinfo->info.accessory_chip =
564 __le32_to_cpu(info->accessory_chip);
566 debugfs_create_u32("accessory_chip", 0444,
567 qcom_socinfo->dbg_root,
568 &qcom_socinfo->info.accessory_chip);
570 case SOCINFO_VERSION(0, 4):
571 qcom_socinfo->info.plat_ver = __le32_to_cpu(info->plat_ver);
573 debugfs_create_u32("platform_version", 0444,
574 qcom_socinfo->dbg_root,
575 &qcom_socinfo->info.plat_ver);
577 case SOCINFO_VERSION(0, 3):
578 qcom_socinfo->info.hw_plat = __le32_to_cpu(info->hw_plat);
580 debugfs_create_u32("hardware_platform", 0444,
581 qcom_socinfo->dbg_root,
582 &qcom_socinfo->info.hw_plat);
584 case SOCINFO_VERSION(0, 2):
585 qcom_socinfo->info.raw_ver = __le32_to_cpu(info->raw_ver);
587 debugfs_create_u32("raw_version", 0444, qcom_socinfo->dbg_root,
588 &qcom_socinfo->info.raw_ver);
590 case SOCINFO_VERSION(0, 1):
591 DEBUGFS_ADD(info, build_id);
595 versions = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_IMAGE_VERSION_TABLE,
598 for (i = 0; i < ARRAY_SIZE(socinfo_image_names); i++) {
599 if (!socinfo_image_names[i])
602 dentry = debugfs_create_dir(socinfo_image_names[i],
603 qcom_socinfo->dbg_root);
604 debugfs_create_file("name", 0444, dentry, &versions[i],
605 &qcom_image_name_ops);
606 debugfs_create_file("variant", 0444, dentry, &versions[i],
607 &qcom_image_variant_ops);
608 debugfs_create_file("oem", 0444, dentry, &versions[i],
609 &qcom_image_oem_ops);
613 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo)
615 debugfs_remove_recursive(qcom_socinfo->dbg_root);
618 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
619 struct socinfo *info, size_t info_size)
622 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo) { }
623 #endif /* CONFIG_DEBUG_FS */
625 static int qcom_socinfo_probe(struct platform_device *pdev)
627 struct qcom_socinfo *qs;
628 struct socinfo *info;
631 info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HW_SW_BUILD_ID,
634 dev_err(&pdev->dev, "Couldn't find socinfo\n");
635 return PTR_ERR(info);
638 qs = devm_kzalloc(&pdev->dev, sizeof(*qs), GFP_KERNEL);
642 qs->attr.family = "Snapdragon";
643 qs->attr.machine = socinfo_machine(&pdev->dev,
644 le32_to_cpu(info->id));
645 qs->attr.soc_id = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u",
646 le32_to_cpu(info->id));
647 qs->attr.revision = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u.%u",
648 SOCINFO_MAJOR(le32_to_cpu(info->ver)),
649 SOCINFO_MINOR(le32_to_cpu(info->ver)));
650 if (offsetof(struct socinfo, serial_num) <= item_size)
651 qs->attr.serial_number = devm_kasprintf(&pdev->dev, GFP_KERNEL,
653 le32_to_cpu(info->serial_num));
655 qs->soc_dev = soc_device_register(&qs->attr);
656 if (IS_ERR(qs->soc_dev))
657 return PTR_ERR(qs->soc_dev);
659 socinfo_debugfs_init(qs, info, item_size);
661 /* Feed the soc specific unique data into entropy pool */
662 add_device_randomness(info, item_size);
664 platform_set_drvdata(pdev, qs);
669 static int qcom_socinfo_remove(struct platform_device *pdev)
671 struct qcom_socinfo *qs = platform_get_drvdata(pdev);
673 soc_device_unregister(qs->soc_dev);
675 socinfo_debugfs_exit(qs);
680 static struct platform_driver qcom_socinfo_driver = {
681 .probe = qcom_socinfo_probe,
682 .remove = qcom_socinfo_remove,
684 .name = "qcom-socinfo",
688 module_platform_driver(qcom_socinfo_driver);
690 MODULE_DESCRIPTION("Qualcomm SoCinfo driver");
691 MODULE_LICENSE("GPL v2");
692 MODULE_ALIAS("platform:qcom-socinfo");