1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2020 Arm Limited 4 */ 5 6 #define pr_fmt(fmt) "SMCCC: SOC_ID: " fmt 7 8 #include <linux/arm-smccc.h> 9 #include <linux/bitfield.h> 10 #include <linux/device.h> 11 #include <linux/module.h> 12 #include <linux/kernel.h> 13 #include <linux/slab.h> 14 #include <linux/sys_soc.h> 15 16 #define SMCCC_SOC_ID_JEP106_BANK_IDX_MASK GENMASK(30, 24) 17 /* 18 * As per the SMC Calling Convention specification v1.2 (ARM DEN 0028C) 19 * Section 7.4 SMCCC_ARCH_SOC_ID bits[23:16] are JEP-106 identification 20 * code with parity bit for the SiP. We can drop the parity bit. 21 */ 22 #define SMCCC_SOC_ID_JEP106_ID_CODE_MASK GENMASK(22, 16) 23 #define SMCCC_SOC_ID_IMP_DEF_SOC_ID_MASK GENMASK(15, 0) 24 25 #define JEP106_BANK_CONT_CODE(x) \ 26 (u8)(FIELD_GET(SMCCC_SOC_ID_JEP106_BANK_IDX_MASK, (x))) 27 #define JEP106_ID_CODE(x) \ 28 (u8)(FIELD_GET(SMCCC_SOC_ID_JEP106_ID_CODE_MASK, (x))) 29 #define IMP_DEF_SOC_ID(x) \ 30 (u16)(FIELD_GET(SMCCC_SOC_ID_IMP_DEF_SOC_ID_MASK, (x))) 31 32 static struct soc_device *soc_dev; 33 static struct soc_device_attribute *soc_dev_attr; 34 35 #ifdef CONFIG_ARM64 36 37 static char __ro_after_init smccc_soc_id_name[136] = ""; 38 39 static inline void str_fragment_from_reg(char *dst, unsigned long reg) 40 { 41 dst[0] = (reg >> 0) & 0xff; 42 dst[1] = (reg >> 8) & 0xff; 43 dst[2] = (reg >> 16) & 0xff; 44 dst[3] = (reg >> 24) & 0xff; 45 dst[4] = (reg >> 32) & 0xff; 46 dst[5] = (reg >> 40) & 0xff; 47 dst[6] = (reg >> 48) & 0xff; 48 dst[7] = (reg >> 56) & 0xff; 49 } 50 51 static char __init *smccc_soc_name_init(void) 52 { 53 struct arm_smccc_1_2_regs args; 54 struct arm_smccc_1_2_regs res; 55 size_t len; 56 57 /* 58 * Issue Number 1.6 of the Arm SMC Calling Convention 59 * specification introduces an optional "name" string 60 * to the ARM_SMCCC_ARCH_SOC_ID function. Fetch it if 61 * available. 62 */ 63 args.a0 = ARM_SMCCC_ARCH_SOC_ID; 64 args.a1 = 2; /* SOC_ID name */ 65 arm_smccc_1_2_invoke(&args, &res); 66 67 if ((u32)res.a0 == 0) { 68 /* 69 * Copy res.a1..res.a17 to the smccc_soc_id_name string 70 * 8 bytes at a time. As per Issue 1.6 of the Arm SMC 71 * Calling Convention, the string will be NUL terminated 72 * and padded, from the end of the string to the end of the 73 * 136 byte buffer, with NULs. 74 */ 75 str_fragment_from_reg(smccc_soc_id_name + 8 * 0, res.a1); 76 str_fragment_from_reg(smccc_soc_id_name + 8 * 1, res.a2); 77 str_fragment_from_reg(smccc_soc_id_name + 8 * 2, res.a3); 78 str_fragment_from_reg(smccc_soc_id_name + 8 * 3, res.a4); 79 str_fragment_from_reg(smccc_soc_id_name + 8 * 4, res.a5); 80 str_fragment_from_reg(smccc_soc_id_name + 8 * 5, res.a6); 81 str_fragment_from_reg(smccc_soc_id_name + 8 * 6, res.a7); 82 str_fragment_from_reg(smccc_soc_id_name + 8 * 7, res.a8); 83 str_fragment_from_reg(smccc_soc_id_name + 8 * 8, res.a9); 84 str_fragment_from_reg(smccc_soc_id_name + 8 * 9, res.a10); 85 str_fragment_from_reg(smccc_soc_id_name + 8 * 10, res.a11); 86 str_fragment_from_reg(smccc_soc_id_name + 8 * 11, res.a12); 87 str_fragment_from_reg(smccc_soc_id_name + 8 * 12, res.a13); 88 str_fragment_from_reg(smccc_soc_id_name + 8 * 13, res.a14); 89 str_fragment_from_reg(smccc_soc_id_name + 8 * 14, res.a15); 90 str_fragment_from_reg(smccc_soc_id_name + 8 * 15, res.a16); 91 str_fragment_from_reg(smccc_soc_id_name + 8 * 16, res.a17); 92 93 len = strnlen(smccc_soc_id_name, sizeof(smccc_soc_id_name)); 94 if (len) { 95 if (len == sizeof(smccc_soc_id_name)) 96 pr_warn(FW_BUG "Ignoring improperly formatted name\n"); 97 else 98 return smccc_soc_id_name; 99 } 100 } 101 102 return NULL; 103 } 104 105 #else 106 107 static char __init *smccc_soc_name_init(void) 108 { 109 return NULL; 110 } 111 112 #endif 113 114 static int __init smccc_soc_init(void) 115 { 116 int soc_id_rev, soc_id_version; 117 static char soc_id_str[20], soc_id_rev_str[12]; 118 static char soc_id_jep106_id_str[12]; 119 120 if (arm_smccc_get_version() < ARM_SMCCC_VERSION_1_2) 121 return 0; 122 123 soc_id_version = arm_smccc_get_soc_id_version(); 124 if (soc_id_version == SMCCC_RET_NOT_SUPPORTED) { 125 pr_info("ARCH_SOC_ID not implemented, skipping ....\n"); 126 return 0; 127 } 128 129 if (soc_id_version < 0) { 130 pr_err("Invalid SoC Version: %x\n", soc_id_version); 131 return -EINVAL; 132 } 133 134 soc_id_rev = arm_smccc_get_soc_id_revision(); 135 if (soc_id_rev < 0) { 136 pr_err("Invalid SoC Revision: %x\n", soc_id_rev); 137 return -EINVAL; 138 } 139 140 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL); 141 if (!soc_dev_attr) 142 return -ENOMEM; 143 144 sprintf(soc_id_rev_str, "0x%08x", soc_id_rev); 145 sprintf(soc_id_jep106_id_str, "jep106:%02x%02x", 146 JEP106_BANK_CONT_CODE(soc_id_version), 147 JEP106_ID_CODE(soc_id_version)); 148 sprintf(soc_id_str, "%s:%04x", soc_id_jep106_id_str, 149 IMP_DEF_SOC_ID(soc_id_version)); 150 151 soc_dev_attr->soc_id = soc_id_str; 152 soc_dev_attr->revision = soc_id_rev_str; 153 soc_dev_attr->family = soc_id_jep106_id_str; 154 soc_dev_attr->machine = smccc_soc_name_init(); 155 156 soc_dev = soc_device_register(soc_dev_attr); 157 if (IS_ERR(soc_dev)) { 158 kfree(soc_dev_attr); 159 return PTR_ERR(soc_dev); 160 } 161 162 pr_info("ID = %s Revision = %s\n", soc_dev_attr->soc_id, 163 soc_dev_attr->revision); 164 165 return 0; 166 } 167 module_init(smccc_soc_init); 168 169 static void __exit smccc_soc_exit(void) 170 { 171 if (soc_dev) 172 soc_device_unregister(soc_dev); 173 kfree(soc_dev_attr); 174 } 175 module_exit(smccc_soc_exit); 176