Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp570896pxu; Tue, 6 Oct 2020 13:22:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyTVZvHPCb0BDgldjocdJv3i1+d/BU3xqgna1qkRWSr/JXy6RMo5maJHv+JOnrDVuksJLC X-Received: by 2002:a17:906:275b:: with SMTP id a27mr1454453ejd.190.1602015723794; Tue, 06 Oct 2020 13:22:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602015723; cv=none; d=google.com; s=arc-20160816; b=CG5pa3ztHcEEFV3b4lvKt+jwdmsbFlp0WgM4kyh+rc29XQoJ4asLhWX2LXwOZAcyEL zeN8aYygmkzjMKKf+v6B3h+rDCFfOXu3FUdbabWj85fHO6xwN4aXWrEturVyhdiZktU3 BZ2bHG61kv1ffrH2E6b/fdD+AZQdWAhnIKAd4/Gz7NvW6qlcSVQvLZGXWnkXFH05KVG/ qoH3/7IAsI/OfrYN8danBL+3h2CJ70jjUwKJhoNvS5NHECIdDdpJpOZMYTyI8fcHz2Jz 9F6saNx4y/CsdCVPLWEGAUJCzWu5EWuN+N9gtQtC8umD2UmMMgrqzRzr3b372s4E9Eyc SQgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=XLIuE7hEHPMM1k94sQtZuD3NSojVz9JRjxpZCfQkDKo=; b=yRVdmvVG+6dz1opBpd0fyDSSQfAei0uo2+eRAHkyq0RGENyWClKo/XOfASZe3vLv8n etkxvxxKUmRPF6q7RgkCFX0veGCvLDnkgzYbng980otdKKSiKQ1ALqsM+Uo+zLTud8pF hNw2hiGtokZ8x1jgiOEkkIWmSWYxIPTX7NUu9kboHwfeFgivvx3XZhDyMRdYtq4CAOIX jZ2un5FBG7xHIUluFWgI34wa0pf5IjjnIpvJ6hhtqvm7wbo3lGZVXcEwbE5mwvQmE4SX lSsNzbGKBl7FL2KHsZXacQOt6mdwYaHL3F89Mz5IFo4PApAqKZtF6Wa5KZoO/HNCcxGo sGCQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t18si2823204eje.652.2020.10.06.13.21.40; Tue, 06 Oct 2020 13:22:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727239AbgJFUSy (ORCPT + 99 others); Tue, 6 Oct 2020 16:18:54 -0400 Received: from foss.arm.com ([217.140.110.172]:56228 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727214AbgJFUSw (ORCPT ); Tue, 6 Oct 2020 16:18:52 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3D47B113E; Tue, 6 Oct 2020 13:18:51 -0700 (PDT) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.195.35]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C6A993F71F; Tue, 6 Oct 2020 13:18:49 -0700 (PDT) From: Andre Przywara To: Catalin Marinas , Will Deacon Cc: Mark Rutland , Mark Brown , Ard Biesheuvel , Richard Henderson , Lorenzo Pieralisi , Sudeep Holla , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] arm64: Add support for SMCCC TRNG firmware interface Date: Tue, 6 Oct 2020 21:18:08 +0100 Message-Id: <20201006201808.37665-3-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201006201808.37665-1-andre.przywara@arm.com> References: <20201006201808.37665-1-andre.przywara@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The ARM architected TRNG firmware interface, described in ARM spec DEN0098[1], defines an ARM SMCCC based interface to a true random number generator, provided by firmware. This can be discovered via the SMCCC >=v1.1 interface, and provides up to 192 bits of entropy per call. Hook this SMC call into arm64's arch_get_random_*() implementation, coming to the rescue when the CPU does not implement the ARM v8.5 RNG system registers. For the detection, we piggy back on the PSCI/SMCCC discovery (which gives us the conduit to use: hvc or smc), then try to call the ARM_SMCCC_TRNG_VERSION function, which returns -1 if this interface is not implemented. [1] https://developer.arm.com/documentation/den0098/latest/ Signed-off-by: Andre Przywara --- arch/arm64/include/asm/archrandom.h | 83 +++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/archrandom.h b/arch/arm64/include/asm/archrandom.h index ffb1a40d5475..b6c291c42a48 100644 --- a/arch/arm64/include/asm/archrandom.h +++ b/arch/arm64/include/asm/archrandom.h @@ -7,6 +7,13 @@ #include #include #include +#include + +static enum smc_trng_status { + SMC_TRNG_UNKNOWN, + SMC_TRNG_NOT_SUPPORTED, + SMC_TRNG_SUPPORTED +} smc_trng_status = SMC_TRNG_UNKNOWN; static inline bool __arm64_rndr(unsigned long *v) { @@ -26,6 +33,36 @@ static inline bool __arm64_rndr(unsigned long *v) return ok; } +static inline bool __check_smc_trng(void) +{ + struct arm_smccc_res res; + + if (smc_trng_status == SMC_TRNG_UNKNOWN) { + /* + * The variable behind the get_version() call is initialised + * as ARM_SMCCC_VERSION_1_0, so getting this could mean: + * a) not checked yet (early at boot, before PSCI init), or + * b) not implemented by firmware. + * Since we don't know which one it is, we return false, but + * don't fix the answer yet. + */ + if (arm_smccc_get_version() <= ARM_SMCCC_VERSION_1_0) + return false; + + /* + * With the knowledge at having at least SMCCC v1.1, we + * can now test the existence of the interface. + */ + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_VERSION, &res); + if ((int)res.a0 < 0) + smc_trng_status = SMC_TRNG_NOT_SUPPORTED; + else + smc_trng_status = SMC_TRNG_SUPPORTED; + } + + return smc_trng_status == SMC_TRNG_SUPPORTED; +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; @@ -38,26 +75,52 @@ static inline bool __must_check arch_get_random_int(unsigned int *v) static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { + struct arm_smccc_res res; + /* - * Only support the generic interface after we have detected - * the system wide capability, avoiding complexity with the - * cpufeature code and with potential scheduling between CPUs + * Try the ARMv8.5-A RNDR instruction first, but only after + * we have detected the system wide capability, avoiding complexity + * with the cpufeature code and with potential scheduling between CPUs * with and without the feature. */ - if (!cpus_have_const_cap(ARM64_HAS_RNG)) - return false; + if (cpus_have_const_cap(ARM64_HAS_RNG)) + return __arm64_rndr(v); - return __arm64_rndr(v); -} + if (__check_smc_trng()) { + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res); + if ((int)res.a0 < 0) + return false; + *v = res.a3; + return true; + } + + return false; +} static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { + struct arm_smccc_res res; unsigned long val; - bool ok = arch_get_random_seed_long(&val); - *v = val; - return ok; + if (cpus_have_const_cap(ARM64_HAS_RNG)) { + if (arch_get_random_seed_long(&val)) { + *v = val; + return true; + } + return false; + } + + if (__check_smc_trng()) { + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 32, &res); + if ((int)res.a0 < 0) + return false; + + *v = res.a3 & GENMASK(31, 0); + return true; + } + + return false; } static inline bool __init __early_cpu_has_rndr(void) -- 2.17.1