Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp228135rdb; Thu, 30 Nov 2023 03:20:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IGPzw//+sopf5vOOfby/GnxwmkOnp+VOUqVWTVbSFs3icSiKN+VEadIIjSfAdPwohahg9MR X-Received: by 2002:a05:6830:1e68:b0:6b9:1473:7fa3 with SMTP id m8-20020a0568301e6800b006b914737fa3mr24642740otr.27.1701343212820; Thu, 30 Nov 2023 03:20:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701343212; cv=none; d=google.com; s=arc-20160816; b=KBks0FkAlAdqjnfsPv4zhG/aAZHCjVDR1sLJFZAtfxryFwE11H3KdrCJBcoj5OLj+5 JY9/Zr/++gQJ9Ok62iZHYg3z1Oyzw7y0FlBiFSzbSAMJVPgwDdkNYpHQx1TmEubp6VR2 /v1YWx8KtSqgjFwFwbBXats6gw5w1/ONrIxfmRtkpe1+1ya9/sSuhEO8fwyeA/Pw+Clt RbEXCjdf/GRUpC7UPEijSVT53TUYZDxlc6hFyc0IqSJEh6qT+q/ET+SumuQrHOx5WLvs W1H41KwONDwWE4B7BIAU1OQIByS4ARUI3qKW7h/pw1tJ+XcAURA/HLvZ1ccMRCxGv0V/ ItTg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=2xkg3D7dfeo+mWJRReRlSNv0FZynZ0tPFmMr3Kpg4FI=; fh=/qGZ6fL/kfpdweItwhXsubUnOOzWHFu5Hukr8rWvh7I=; b=unHh/tN7U8d1ZpA/keotYDLSyD1UUY+DDTj9NZ96VJLSoZWFcrJzs5bsyM7yejqsWJ iU8+TAcS1CgbaT7F1uaXr+ns3riovQeEh3ORvSYq4lxTg90IXZ85JWKEy5VhWIwJdrmh Vs8LPKda72qyBpG6VUJ9SR1ZZwu/Chcp0aLCJsCULdNhAykEnNkwxJHvg+xyhW9kz6MV 0+xHNhUjP8UXBesIXdOTd4txDGloqp0wK39AjEeEhnZSVuyYrWRhtatA1QrtQRkJRbYr +0TWcEZnLv/GmmtiK6fFLAgdrKpwnBZTlCgQdze3lxFRFUTM6nDgDHMA4SLONlN5hqjX +BYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GrnJWDOb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id d3-20020a63ed03000000b005bd04d27b69si1078242pgi.725.2023.11.30.03.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 03:20:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GrnJWDOb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id ECA6D8039EF8; Thu, 30 Nov 2023 03:20:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345038AbjK3LTx (ORCPT + 99 others); Thu, 30 Nov 2023 06:19:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345004AbjK3LTv (ORCPT ); Thu, 30 Nov 2023 06:19:51 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FF31D54 for ; Thu, 30 Nov 2023 03:19:57 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-40b4ff0f254so847755e9.1 for ; Thu, 30 Nov 2023 03:19:57 -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=vger.kernel.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=GrnJWDObHcUogjBOngRofFMH9Ig8ey1Jy9vZJniVZDXuegnlm21xjGY83VE51fb4pP TqbtIRvgsDVlg80K1/YOjZmUZYV6RvKWPekEKkm+FlbxwOStn9e6+wg+vOqNyHTj/pEQ OoyExbXMfmxaGIiWHEojtd3qgTqVo0uRby2a7gI82S6SAB1b12OMqlTyYgDicaXvcB4+ mzIAqLu0Wn4SuzttPmCyvwaFvCjJRW4+6uThU4oouHd4ZNiFMO/9QXZSvhgqKn0osule Yjc2ghkQFg3EFhVYvVpzGJgnxXQZtbwXWLZlhmom9cKLYjxxRju00mII5s3J6RmO5AW9 6c0Q== 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=suYFljXR1XtApdVmWib6gRJNA44VeAqq0gBRgivaCYjAOV9n0B8Xx2clfyHCRksiFp tnAxAYtnLTrI7jPDKA6mXkVJDtzeow7ZDXZDM+b/qSnpGdqAFfNi6haVD3lMWgpjcgZw YF7ab+VVZsKBFUdRdTMrA7FqdQ0w7fH3qgKc9cl0b4NKs7LTA9//n5zLyD46MZYu3czE LhHsuBAKQfOkVPHW/nJJZI/EucmJlUqDOKapwGZm88v1zGflkajZ8WAkbWQdSM123q7W nVudO8dpfCp6vR5Z+XNlnUp+V1g/NgOrlqiBE+0Me9rk+dJI2myhzJKmLVthJhtyWaNk ysKA== X-Gm-Message-State: AOJu0Yz1gGzwywAoy2FJDWMaM6RmDHXJmXWBAikP/eX3VcTp7lHpGD2x kMROmXC+kwojSjBmcaFhU1PFpg== 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?q?Cl=C3=A9ment=20L=C3=A9ger?= 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?q?Cl=C3=A9ment=20L=C3=A9ger?= 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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 30 Nov 2023 03:20:10 -0800 (PST) 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 -- 2.43.0