From patchwork Wed Nov 29 16:03:25 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: 13473060 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 09B53C4167B for ; Wed, 29 Nov 2023 16:03:52 +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=IzY3NIr1/mCzkQzQLmg+jAOCgXQfvYNrlCh6Pqwx7W4=; b=BIje7rEcV4m1z7 jRdl8P1N99Rz6k8WZdrnmarGQsexrrTPEcRMFsgIZ8w0uR953X0DGm9QoN4D7SH0DdD7+JEeXkPtW /qCMwgSR8tDH0K+T8Pk24OfMKpPC8PnOFVbOJ7FjKfnlIwdXuWUczFegjwY3zWWuYKp8cyuMOVycS I59K/LwHiEDYXCipilKnQnEqtKHlWtBwgV3lMsLhC2zGYSfG9PMoxClNtVAs2QLht8LsvcgUSs1Q9 XpLJDSCH9d9I+BbjSd8MlzkBp/zN/Ty8iom83xK7hee/OYLz0w9w6Zy+JW50MCk7wVkm+cWkcVZuf djfPaDwPD4ZnQjkTSTHg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r8N2P-008o6D-2Z; Wed, 29 Nov 2023 16:03:45 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r8N2M-008o52-0D for linux-riscv@lists.infradead.org; Wed, 29 Nov 2023 16:03:44 +0000 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-3316d855828so1190989f8f.0 for ; Wed, 29 Nov 2023 08:03:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1701273820; x=1701878620; 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=7IGDwssStSDpyhXdV1nfus/AGOvw09dSHX5DmZ6h29s=; b=vHOgmrh9/VTNKXnINkUxYhZW4aNSpN1oz93fWT4NLIn7cVHGImdAnuKMFUBMC/fofz G9Uwtl2aUBg7j6OFQ2vv6BQF+0l/M/10Ne3KwfFcEF31cY2joI+RhF5mC2cfI4Z/GedH Vc3K/yPBSEabw4vqjuxWvqahmKs0p3GNUMZt04WvoFJePwl5VAG6CxmtwkTKFGTbM7vl 0C+ePIEXNI9xik7QAoiP/Lv6CKxQTGZoKgPxA5GqGMCV6/8pRvGEAagi3dE6SuffDwsb is0CyCehtihFzqrDJpe76pQZn9gsBHCZvJbyqrPvzCtdYJMTXKou7UeyOLkFXDDIG56X iwfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701273820; x=1701878620; 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=7IGDwssStSDpyhXdV1nfus/AGOvw09dSHX5DmZ6h29s=; b=b4ieLXlE8/1MoDHM5BN1oCW1krjwinEuuT8tWGtzjZlGzK0DJBqGMHmT6oY4ZsdkNe ny43NTbA/IvJHreRzKsBZ8L5rBAuKGRHENS7izsT/Tkd4VkCOyDVi9FwhWBs6kspUPBS J9uv19GvEaI41EC3xxQBnlvHA4ZwdOOW3SlBqhIxBjXu+qaP+On9Uf5i/4jxpEQ6qR4b V2peYCcbqkOK63pc8G0tyLWtyoNa9h2ONw9gJ1Oj+vcB0/UTxTuh7KVf71ZE4+nhJ6EL nsUUKy/CgNF5RKU/T8ylyJmBNYkb+gCl8GL6wB12YJqSGMl/RO8ZF+QwodjTK5pHUrZB eO0Q== X-Gm-Message-State: AOJu0YxA9xUqNzoJEnlJaIpIEXiQ8hj/uwbaPx++94Ogrik+alDo0aIq Ow0DI3fcTZF2uCrP69cvCs2e/g== X-Google-Smtp-Source: AGHT+IHiupA6lbwePPPMup095kGgR6Wzqg1hkQkrzFoWGaSz5k1vlW/5w08YDqh5N/Ye6RNFE45lOw== X-Received: by 2002:a05:6000:4011:b0:332:e749:adaa with SMTP id cp17-20020a056000401100b00332e749adaamr12719911wrb.3.1701273819573; Wed, 29 Nov 2023 08:03:39 -0800 (PST) Received: from carbon-x1.. ([2a01:e0a:999:a3a0:11a9:3d89:528b:b336]) by smtp.gmail.com with ESMTPSA id q1-20020a5d6581000000b00331a55d3875sm18230283wru.38.2023.11.29.08.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 08:03:39 -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 , Conor Dooley , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH] RISC-V: Implement archrandom when Zkr is available Date: Wed, 29 Nov 2023 17:03:25 +0100 Message-ID: <20231129160325.1119803-1-cleger@rivosinc.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231129_080342_408449_B4893B71 X-CRM114-Status: GOOD ( 18.17 ) 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 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] --- arch/riscv/include/asm/archrandom.h | 69 +++++++++++++++++++++++++++++ arch/riscv/include/asm/csr.h | 9 ++++ 2 files changed, 78 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..795837ccb583 --- /dev/null +++ b/arch/riscv/include/asm/archrandom.h @@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Kernel interface for the RISCV arch_random_* functions + * + * Copyright (c) 2023 by Rivos Inc. + * + */ + +#ifndef ASM_RISCV_ARCHRANDOM_H +#define ASM_RISCV_ARCHRANDOM_H + +#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); + + switch (csr_seed & SEED_OPST_MASK) { + 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: + 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