| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (C) 2020 Arm Limited |
| 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) "smccc: " fmt |
| 7 | |
| 8 | #include <linux/cache.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/arm-smccc.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <asm/archrandom.h> |
| 14 | |
| 15 | static u32 smccc_version = ARM_SMCCC_VERSION_1_0; |
| 16 | static enum arm_smccc_conduit smccc_conduit = SMCCC_CONDUIT_NONE; |
| 17 | |
| 18 | bool __ro_after_init smccc_trng_available = false; |
| 19 | s32 __ro_after_init smccc_soc_id_version = SMCCC_RET_NOT_SUPPORTED; |
| 20 | s32 __ro_after_init smccc_soc_id_revision = SMCCC_RET_NOT_SUPPORTED; |
| 21 | |
| 22 | void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit) |
| 23 | { |
| 24 | struct arm_smccc_res res; |
| 25 | |
| 26 | smccc_version = version; |
| 27 | smccc_conduit = conduit; |
| 28 | |
| 29 | smccc_trng_available = smccc_probe_trng(); |
| 30 | |
| 31 | if ((smccc_version >= ARM_SMCCC_VERSION_1_2) && |
| 32 | (smccc_conduit != SMCCC_CONDUIT_NONE)) { |
| 33 | arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, |
| 34 | ARM_SMCCC_ARCH_SOC_ID, &res); |
| 35 | if ((s32)res.a0 >= 0) { |
| 36 | arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_SOC_ID, 0, &res); |
| 37 | smccc_soc_id_version = (s32)res.a0; |
| 38 | arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_SOC_ID, 1, &res); |
| 39 | smccc_soc_id_revision = (s32)res.a0; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void) |
| 45 | { |
| 46 | if (smccc_version < ARM_SMCCC_VERSION_1_1) |
| 47 | return SMCCC_CONDUIT_NONE; |
| 48 | |
| 49 | return smccc_conduit; |
| 50 | } |
| 51 | EXPORT_SYMBOL_GPL(arm_smccc_1_1_get_conduit); |
| 52 | |
| 53 | u32 arm_smccc_get_version(void) |
| 54 | { |
| 55 | return smccc_version; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(arm_smccc_get_version); |
| 58 | |
| 59 | s32 arm_smccc_get_soc_id_version(void) |
| 60 | { |
| 61 | return smccc_soc_id_version; |
| 62 | } |
| 63 | |
| 64 | s32 arm_smccc_get_soc_id_revision(void) |
| 65 | { |
| 66 | return smccc_soc_id_revision; |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(arm_smccc_get_soc_id_revision); |
| 69 | |
| 70 | bool arm_smccc_hypervisor_has_uuid(const uuid_t *hyp_uuid) |
| 71 | { |
| 72 | struct arm_smccc_res res = {}; |
| 73 | uuid_t uuid; |
| 74 | |
| 75 | if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_HVC) |
| 76 | return false; |
| 77 | |
| 78 | arm_smccc_1_1_hvc(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, &res); |
| 79 | if (res.a0 == SMCCC_RET_NOT_SUPPORTED) |
| 80 | return false; |
| 81 | |
| 82 | uuid = smccc_res_to_uuid(r0: res.a0, r1: res.a1, r2: res.a2, r3: res.a3); |
| 83 | return uuid_equal(u1: &uuid, u2: hyp_uuid); |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(arm_smccc_hypervisor_has_uuid); |
| 86 | |
| 87 | static int __init smccc_devices_init(void) |
| 88 | { |
| 89 | struct platform_device *pdev; |
| 90 | |
| 91 | if (smccc_trng_available) { |
| 92 | pdev = platform_device_register_simple(name: "smccc_trng" , id: -1, |
| 93 | NULL, num: 0); |
| 94 | if (IS_ERR(ptr: pdev)) |
| 95 | pr_err("smccc_trng: could not register device: %ld\n" , |
| 96 | PTR_ERR(pdev)); |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | device_initcall(smccc_devices_init); |
| 102 | |