Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2020 Arm Limited
0004  */
0005 
0006 #define pr_fmt(fmt) "SMCCC: SOC_ID: " fmt
0007 
0008 #include <linux/arm-smccc.h>
0009 #include <linux/bitfield.h>
0010 #include <linux/device.h>
0011 #include <linux/module.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/sys_soc.h>
0015 
0016 #define SMCCC_SOC_ID_JEP106_BANK_IDX_MASK   GENMASK(30, 24)
0017 /*
0018  * As per the SMC Calling Convention specification v1.2 (ARM DEN 0028C)
0019  * Section 7.4 SMCCC_ARCH_SOC_ID bits[23:16] are JEP-106 identification
0020  * code with parity bit for the SiP. We can drop the parity bit.
0021  */
0022 #define SMCCC_SOC_ID_JEP106_ID_CODE_MASK    GENMASK(22, 16)
0023 #define SMCCC_SOC_ID_IMP_DEF_SOC_ID_MASK    GENMASK(15, 0)
0024 
0025 #define JEP106_BANK_CONT_CODE(x)    \
0026     (u8)(FIELD_GET(SMCCC_SOC_ID_JEP106_BANK_IDX_MASK, (x)))
0027 #define JEP106_ID_CODE(x)   \
0028     (u8)(FIELD_GET(SMCCC_SOC_ID_JEP106_ID_CODE_MASK, (x)))
0029 #define IMP_DEF_SOC_ID(x)   \
0030     (u16)(FIELD_GET(SMCCC_SOC_ID_IMP_DEF_SOC_ID_MASK, (x)))
0031 
0032 static struct soc_device *soc_dev;
0033 static struct soc_device_attribute *soc_dev_attr;
0034 
0035 static int __init smccc_soc_init(void)
0036 {
0037     struct arm_smccc_res res;
0038     int soc_id_rev, soc_id_version;
0039     static char soc_id_str[20], soc_id_rev_str[12];
0040     static char soc_id_jep106_id_str[12];
0041 
0042     if (arm_smccc_get_version() < ARM_SMCCC_VERSION_1_2)
0043         return 0;
0044 
0045     if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE) {
0046         pr_err("%s: invalid SMCCC conduit\n", __func__);
0047         return -EOPNOTSUPP;
0048     }
0049 
0050     arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
0051                  ARM_SMCCC_ARCH_SOC_ID, &res);
0052 
0053     if ((int)res.a0 == SMCCC_RET_NOT_SUPPORTED) {
0054         pr_info("ARCH_SOC_ID not implemented, skipping ....\n");
0055         return 0;
0056     }
0057 
0058     if ((int)res.a0 < 0) {
0059         pr_info("ARCH_FEATURES(ARCH_SOC_ID) returned error: %lx\n",
0060             res.a0);
0061         return -EINVAL;
0062     }
0063 
0064     arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_SOC_ID, 0, &res);
0065     if ((int)res.a0 < 0) {
0066         pr_err("ARCH_SOC_ID(0) returned error: %lx\n", res.a0);
0067         return -EINVAL;
0068     }
0069 
0070     soc_id_version = res.a0;
0071 
0072     arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_SOC_ID, 1, &res);
0073     if ((int)res.a0 < 0) {
0074         pr_err("ARCH_SOC_ID(1) returned error: %lx\n", res.a0);
0075         return -EINVAL;
0076     }
0077 
0078     soc_id_rev = res.a0;
0079 
0080     soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
0081     if (!soc_dev_attr)
0082         return -ENOMEM;
0083 
0084     sprintf(soc_id_rev_str, "0x%08x", soc_id_rev);
0085     sprintf(soc_id_jep106_id_str, "jep106:%02x%02x",
0086         JEP106_BANK_CONT_CODE(soc_id_version),
0087         JEP106_ID_CODE(soc_id_version));
0088     sprintf(soc_id_str, "%s:%04x", soc_id_jep106_id_str,
0089         IMP_DEF_SOC_ID(soc_id_version));
0090 
0091     soc_dev_attr->soc_id = soc_id_str;
0092     soc_dev_attr->revision = soc_id_rev_str;
0093     soc_dev_attr->family = soc_id_jep106_id_str;
0094 
0095     soc_dev = soc_device_register(soc_dev_attr);
0096     if (IS_ERR(soc_dev)) {
0097         kfree(soc_dev_attr);
0098         return PTR_ERR(soc_dev);
0099     }
0100 
0101     pr_info("ID = %s Revision = %s\n", soc_dev_attr->soc_id,
0102         soc_dev_attr->revision);
0103 
0104     return 0;
0105 }
0106 module_init(smccc_soc_init);
0107 
0108 static void __exit smccc_soc_exit(void)
0109 {
0110     if (soc_dev)
0111         soc_device_unregister(soc_dev);
0112     kfree(soc_dev_attr);
0113 }
0114 module_exit(smccc_soc_exit);