Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp38035695rwd; Wed, 12 Jul 2023 01:57:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlH6dZ+bhxBmgLUYy0wc14hd52DajXEMIx7Bjz6h5PaKabskri8C2wApGB9Hdaumb/8pEacY X-Received: by 2002:a05:6a20:54a7:b0:131:a21:9f96 with SMTP id i39-20020a056a2054a700b001310a219f96mr16892458pzk.6.1689152256257; Wed, 12 Jul 2023 01:57:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689152256; cv=none; d=google.com; s=arc-20160816; b=RDbtElIeMFxzriMXj49r5wyURyzIocOvt/6Fp3FCd98ldJBszByJkt5FkYMA3IWBYy TLS1xnLLiIpQCXgXnBtoYnv2VypNOE8awmupodKM6a4m/pXLf9s0lDmpLglctRDGQun5 4mPX0+E4Y7SWQxi9Sx0fyY3R6HcCekjoMb78HM/jY4YlAoFI3Vb3nBm1X6J6SDfrupxW X1DsxORMZDLSMNeDxAc/0cvwj5lhtEI0Id4uyjTramug7Y12f+D2Mzf+i38RRB42w8Q9 1C7g+a0vZ0vMyFSIygO+pdVlBE4+a0KYYb6FEhI6v2pWqLadX0/HQZZGaXLIVSrPIwwM MMXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2f9QGzip5+pACFq28gF27wBxnLW2cvXWZAANuTU4vJY=; fh=rMlIJuHI66XT2HYKCJobByoGfk2Rh5KElnqreKpDenk=; b=d5bA+y5zUxrbXdXROAdHAEi0g/NZBn92ztigQ/WOLZ64xW99mSYoibpZb5YY4LUDGJ cFKgaanuCg9jJ1OThbJ+MY5FzrkHd1bt98PUQn3ixpqSfev6dCqsSnEUc2a9KQbEtC/8 0KPGCHxvlIAz+wZ80/96v/VjZEqUR3RRKSFf+3PsyhQQJlTF3nHG6VDmL2wezB1vbGPT yxl+y+fc3+JTxKWhsvZQvKTpcCz1oVG3TUFKyuXWpw+NS7MjbxcDxqKNV9MQ6aQ3OTMu myYWgiJROqcMMI9YwW1PQeSbL/+yWh8OBU1SQVMXR994QmeayZjS3tf+dYYbOSMbASUU FX2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=Ryybzuaw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q7-20020a170902a3c700b001b8a8f359fcsi2987149plb.208.2023.07.12.01.57.24; Wed, 12 Jul 2023 01:57:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=Ryybzuaw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231891AbjGLIl6 (ORCPT + 99 others); Wed, 12 Jul 2023 04:41:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232810AbjGLIlu (ORCPT ); Wed, 12 Jul 2023 04:41:50 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15D2DBE for ; Wed, 12 Jul 2023 01:41:48 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fb4146e8fcso2845835e9.0 for ; Wed, 12 Jul 2023 01:41:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1689151306; x=1691743306; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2f9QGzip5+pACFq28gF27wBxnLW2cvXWZAANuTU4vJY=; b=RyybzuawSrIwX935H9lZcY7MHvabwPZqAhDMhz8FSxnVZ89twHobBelMctHy5lqgVT KH86wRZCxTlNAdMkFkAfjnJXtEHHqHYRWxEAtQql9nFgKiOpIdpYH72hKUh/QkYZtVQP pSIlWo3lCuXrO7GDDe7dHzGpVEd6+g0B/kOS+lK/v6N36XMxNOejef8ITqBB76Nj1Th8 m1Xka4EHzAcl/dHa+HPdqUChKntZq7JSB4wrq/PbwEF9dDsIhZi1vPUX6ipzI2SoeyM6 8pvlsD3BvhEQ7bgb8idT36+yz/EckqWKRP1A4GtK1+fwo1EDIPKw6P5F2hPBGHsfRcpM n9oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689151306; x=1691743306; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2f9QGzip5+pACFq28gF27wBxnLW2cvXWZAANuTU4vJY=; b=I9TuXROWXEM/m7egVinFcWbUZZ+swagXiG4qoF+ZrG549RjRY5sNKEEnlLamQRmVrc XphpXxxFksSGRBUoasJ6JiwJ0G7kQDKW4JxEeN7pnsi5iYE2OtDz0WkDDQjTlxezXUsX KULJqphvC9nW3r5IRkSnAWpkv8GfiKlXAMPK5pJCSGc2t2N7B+Lvl7PdnaBq1NJAt5Z7 Tn0vNEx3dPTeBVJ3QCZhusjib3j6PvrIJDsChuG+zd4N3YAvo7K6DGKpgSQTeCUyaYOI cLvQiIrzvFT9Sfai7pLVV0Y5OV6ktwJswOclae0ZJAoFiKXdvX5UdVERlESW3bipt3rq e2Og== X-Gm-Message-State: ABy/qLbJb7eyFg1aX/4jLPUUZxa6yrNPKCrryk4G4Et1TzDOpqcVbxTQ CIdq9YDodF+NMF90xZp3v03zAQ== X-Received: by 2002:a7b:cbc7:0:b0:3f7:a20a:561d with SMTP id n7-20020a7bcbc7000000b003f7a20a561dmr1121551wmi.8.1689151306617; Wed, 12 Jul 2023 01:41:46 -0700 (PDT) Received: from vermeer.tail79c99.ts.net ([2a01:cb1d:81a9:dd00:b570:b34c:ffd4:c805]) by smtp.gmail.com with ESMTPSA id z13-20020adfe54d000000b003143ac73fd0sm4496122wrm.1.2023.07.12.01.41.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 01:41:46 -0700 (PDT) From: Samuel Ortiz To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org Cc: Samuel Ortiz , linux@rivosinc.com, Conor Dooley , Andrew Jones , Heiko Stuebner , Anup Patel , linux-kernel@vger.kernel.org, "Hongren (Zenithal) Zheng" , Guo Ren , Atish Patra , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Evan Green , devicetree@vger.kernel.org, sorear@fastmail.com Subject: [PATCH v4 4/4] RISC-V: Implement archrandom when Zkr is available Date: Wed, 12 Jul 2023 10:41:20 +0200 Message-ID: <20230712084134.1648008-5-sameo@rivosinc.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230712084134.1648008-1-sameo@rivosinc.com> References: <20230712084134.1648008-1-sameo@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- arch/riscv/include/asm/archrandom.h | 70 +++++++++++++++++++++++++++++ arch/riscv/include/asm/csr.h | 9 ++++ 2 files changed, 79 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..38f3cced0fd0 --- /dev/null +++ b/arch/riscv/include/asm/archrandom.h @@ -0,0 +1,70 @@ +/* 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 (0x015) 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 7bac43a3176e..ff6f570487b9 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -391,6 +391,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 -- 2.41.0