From patchwork Thu Nov 30 11:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13474244 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B5885C4167B for ; Thu, 30 Nov 2023 11:20:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=UbOhJ+WcqLcQ3l1/C51dL5u941I/cDbNsVcyKks3DUY=; b=C+YGOnz4Q2oakg gdxm4feLjlPfklz+zJ1Hp7V+EDqHxQnBizIDvVW3R2SHlmn7WNGa4t5oOcYHarBrYdQVMwAFRsWWu KZ41bYY4t3PIHpDtRgZk22u2CunM1UKEo5YNBh4SXwOffomZUor1oqvwSSTN/HsHfsf+fVZ4gHhYO DR/L+xJH2JioA6PdZ0qhZ2ANQeRkiAK8OagG0eRo0w/d6gANTCcW/8ZnJSZEXtsXoAjkYXC5yvVVa RHEajMlNepp8MFd//wNjfw/WmXVYWUnirogiPvZRKGiWYHDSx4GOPGUFCYQ/rnFIdKhN0U/T9qOgA 3gExmaf7wtiOi+w8Cxcw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r8f5P-00AYGe-24; Thu, 30 Nov 2023 11:20:03 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r8f5L-00AYFl-1n for linux-riscv@lists.infradead.org; Thu, 30 Nov 2023 11:20:01 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-40b4ff0f254so847765e9.1 for ; Thu, 30 Nov 2023 03:19:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701343195; x=1701947995; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2xkg3D7dfeo+mWJRReRlSNv0FZynZ0tPFmMr3Kpg4FI=; b=nPtSpg1WMZCbX0MjUfo+D2laWr7InXd9DrH+W2GaIZDjd/Z8z2YHdNjfroa6bfCiAZ N8DPz8x+EImuALDLFi81OIJbyLxYNmHDTe+lNyU8XwwYj/nIVi6KwwvEGZ8kuDDV+AKh +ZiV+N5mGMd5SiJRtVkSz2XoE6Dwg5h/rvDme4N82x260K1qStq6Gl76nEIyb9B8H+Yh ViqHJfltsdavOnY72/CuCm1RiD2rHSkqVMVCTmEgKGVHMkp0n87m6GUHx7TG3F6PQoDv fl5MCsKRkkvtUildIGJae8wrgDdj+G0+s8fKqdNbJLqrXdbn2fwa6/e58xNdVGDZRAIO WcRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701343195; x=1701947995; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2xkg3D7dfeo+mWJRReRlSNv0FZynZ0tPFmMr3Kpg4FI=; b=rB35eIevivtpDixY/Izkktfoh4Qq31gX/zycBdrMWLtV+dbiPVP23L+Gt5QnF12K0L rZff347yzL1YIDiq+Grf7LfseaBcVejEu30qplfpfyQPl9C7XoNRjBBZN1VYloVkdXub 4d2rM31LVP37ipv6CXApfy/DkeQIR2O+32q0W9X9MSgOkQvzwFnNKCbb9DD4ZRCFRqL1 7NryGvruDb6crQq1fhJqoH4fDP/eBLyvsOta0Fl97P+4oDVNtPaa6l6WM/j07VSQm9FS pgu7qXiffV27C4GaByyUec2fm7FW8sc08KIbc+Jgw5RiheYRFJJKSrrN0QmDPw5vXkLV O3lQ== X-Gm-Message-State: AOJu0Yz8tkTXpTTtnyHTkhb+yrU7IFOlsQNRwpm3O2CUGepKlUEf/aDf G1nlqNI3zNbpEIEsCnXgh+moXw== X-Google-Smtp-Source: AGHT+IFfRRNQEO2XT8TyLZKIAq0sDnYFn0kJ8Z/ibitwXQYGOwIrgK+qTbFcYzc1pO4WH0/im53AAw== X-Received: by 2002:a05:600c:4f43:b0:40b:3d6d:e46b with SMTP id m3-20020a05600c4f4300b0040b3d6de46bmr1021907wmq.4.1701343195571; Thu, 30 Nov 2023 03:19:55 -0800 (PST) Received: from carbon-x1.. ([2a01:e0a:999:a3a0:1e3e:ecf4:f8d8:c45e]) by smtp.gmail.com with ESMTPSA id fs20-20020a05600c3f9400b0040b5517ae31sm4537310wmb.6.2023.11.30.03.19.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 03:19:55 -0800 (PST) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Samuel Ortiz , Ben Dooks , Conor Dooley , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH v2] RISC-V: Implement archrandom when Zkr is available Date: Thu, 30 Nov 2023 12:17:02 +0100 Message-ID: <20231130111704.1319081-1-cleger@rivosinc.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231130_031959_815589_BC913D31 X-CRM114-Status: GOOD ( 19.30 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Samuel Ortiz The Zkr extension is ratified and provides 16 bits of entropy seed when reading the SEED CSR. We can implement arch_get_random_seed_longs() by doing multiple csrrw to that CSR and filling an unsigned long with valid entropy bits. Acked-by: Conor Dooley Signed-off-by: Samuel Ortiz Signed-off-by: Clément Léger --- This series depends on "riscv: report more ISA extensions through hwprobe" series [1]. Link: https://lore.kernel.org/lkml/20231114141256.126749-1-cleger@rivosinc.com/ [1] Changes in V2: - Add cpu_relax() in BIST/WAIT/default cases. - Remove From: from commit description - Remove "by" from copyright mention --- arch/riscv/include/asm/archrandom.h | 72 +++++++++++++++++++++++++++++ arch/riscv/include/asm/csr.h | 9 ++++ 2 files changed, 81 insertions(+) create mode 100644 arch/riscv/include/asm/archrandom.h diff --git a/arch/riscv/include/asm/archrandom.h b/arch/riscv/include/asm/archrandom.h new file mode 100644 index 000000000000..5345360adfb9 --- /dev/null +++ b/arch/riscv/include/asm/archrandom.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Kernel interface for the RISCV arch_random_* functions + * + * Copyright (c) 2023 Rivos Inc. + * + */ + +#ifndef ASM_RISCV_ARCHRANDOM_H +#define ASM_RISCV_ARCHRANDOM_H + +#include +#include + +#define SEED_RETRY_LOOPS 100 + +static inline bool __must_check csr_seed_long(unsigned long *v) +{ + unsigned int retry = SEED_RETRY_LOOPS, valid_seeds = 0; + const int needed_seeds = sizeof(long) / sizeof(u16); + u16 *entropy = (u16 *)v; + + do { + /* + * The SEED CSR must be accessed with a read-write instruction. + */ + unsigned long csr_seed = csr_swap(CSR_SEED, 0); + unsigned long opst = csr_seed & SEED_OPST_MASK; + + switch (opst) { + case SEED_OPST_ES16: + entropy[valid_seeds++] = csr_seed & SEED_ENTROPY_MASK; + if (valid_seeds == needed_seeds) + return true; + break; + + case SEED_OPST_DEAD: + pr_err_once("archrandom: Unrecoverable error\n"); + return false; + + case SEED_OPST_BIST: + case SEED_OPST_WAIT: + default: + cpu_relax(); + continue; + } + } while (--retry); + + return false; +} + +static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs) +{ + return 0; +} + +static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs) +{ + if (!max_longs) + return 0; + + /* + * If Zkr is supported and csr_seed_long succeeds, we return one long + * worth of entropy. + */ + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZKR) && csr_seed_long(v)) + return 1; + + return 0; +} + +#endif /* ASM_RISCV_ARCHRANDOM_H */ diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 306a19a5509c..510014051f5d 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -411,6 +411,15 @@ #define CSR_VTYPE 0xc21 #define CSR_VLENB 0xc22 +/* Scalar Crypto Extension - Entropy */ +#define CSR_SEED 0x015 +#define SEED_OPST_MASK _AC(0xC0000000, UL) +#define SEED_OPST_BIST _AC(0x00000000, UL) +#define SEED_OPST_WAIT _AC(0x40000000, UL) +#define SEED_OPST_ES16 _AC(0x80000000, UL) +#define SEED_OPST_DEAD _AC(0xC0000000, UL) +#define SEED_ENTROPY_MASK _AC(0xFFFF, UL) + #ifdef CONFIG_RISCV_M_MODE # define CSR_STATUS CSR_MSTATUS # define CSR_IE CSR_MIE