From nobody Sat May 18 08:35:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1695365499; cv=pass; d=zohomail.com; s=zohoarc; b=IlxxOkNtgl+wJs4E6aAqgu7FzgwRsBlHLIyX4j6r9+Wk2E94bQ8ti5X7ImktCqL2E+Ezpj9eh2KjntGRFsPgZskqSFJ5qa7jRVe3brv4fAPxDUVE8K+IA4FwUfN87iUWyWTaQGTR3iBpGPgykR3kgABFjLknx9fnACdBwCbEm90= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695365499; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=uee0P6N3zOFSy84Mg967+zwc0sH7/3W81SNTfXQs9Fk=; b=M3PBMm7FHXjqfZEEkXXiyTgWHY0IiaGircjv5Lo+pDM83YvHMGTeIZc2Hk+l109Jegqj9Ly+qiwpTfVKeMA3S4yI1aYgN5EpBaSAcBnqGGgwzuflD3oMC0wcD2/GcojMD3B6SoXObjXIR/IOpxR+J/EUzW6GEHmde+1qrp/Hto8= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169536549933019.530287805184685; Thu, 21 Sep 2023 23:51:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qja00-00020c-Ia; Fri, 22 Sep 2023 02:50:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjZzv-0001zd-GY; Fri, 22 Sep 2023 02:50:43 -0400 Received: from mail-bn8nam04on2060f.outbound.protection.outlook.com ([2a01:111:f400:7e8d::60f] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjZzo-00046P-MV; Fri, 22 Sep 2023 02:50:41 -0400 Received: from BL1PR13CA0313.namprd13.prod.outlook.com (2603:10b6:208:2c1::18) by BL0PR12MB4883.namprd12.prod.outlook.com (2603:10b6:208:1c6::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.21; Fri, 22 Sep 2023 06:50:29 +0000 Received: from MN1PEPF0000ECDA.namprd02.prod.outlook.com (2603:10b6:208:2c1:cafe::e5) by BL1PR13CA0313.outlook.office365.com (2603:10b6:208:2c1::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.11 via Frontend Transport; Fri, 22 Sep 2023 06:50:29 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by MN1PEPF0000ECDA.mail.protection.outlook.com (10.167.242.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6792.20 via Frontend Transport; Fri, 22 Sep 2023 06:50:29 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 22 Sep 2023 01:50:24 -0500 Received: from SATLEXMB03.amd.com (10.181.40.144) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 22 Sep 2023 01:50:11 -0500 Received: from xsjtongh40.xilinx.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server id 15.1.2507.27 via Frontend Transport; Fri, 22 Sep 2023 01:50:11 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V8Uyl41FLfCaj89iUKyCbGPH1hrX3bWUNKcA/71cLWjPhBzO2SfssZ4mzCmX3fBa9jqreS7HLRDUPN4wuiTEQixRGHPaqwDj1N08bwLql64GW9DbyCk6YZaZ8X6vPkj5W3SDFfJdUybw9L4cjehsbn3ccsmrvv5UWeYBa4rYfDaAzJerMyt3aW0ySCCAk3kiEF0Iq+s3yMmdcIcQsM16hHe4yNtkOYOIFJRcjY6dvzy2jBoVly47XtffQBxCjUMReIVySe741TG7+zAMBGFbJZ/qU84Y8s9pq9RMV0nYOaCDJc3aFdS14ey4fJzazHVhpOQUgJbOMCq97oD3apK1+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uee0P6N3zOFSy84Mg967+zwc0sH7/3W81SNTfXQs9Fk=; b=JeZajvtnLYPNqu1PI809nU2fNnKszrjbNPYuS3Xz1e0K3d3Ey57k+iPKDuah9VFIXCVr+NIyvXZGyYY5QKNo3LzoYCYWaHdy4pEDkJA4/3za4lwl7JuC6HqQaM9LwSF4FKT+d1BP9XohDT6AKwulKWw/4Pt9Dwz5/tu7RxWubfd6lfureSHu7v+K29vPL77fC0AZ8ACpWlkCag0Blw1Iafrk43mHH+fZZG2K91eSPQWOx1uoUSIM2yxQj1QyMMy3Txkj1FFa4uNGllAwDSHU8jsK380pU4Gyq9NAvgzZrSP0b25e/uhlQy1IeKDHa0roqLlKpH6+MNHLWGdW+7kRdw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nongnu.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uee0P6N3zOFSy84Mg967+zwc0sH7/3W81SNTfXQs9Fk=; b=jeA497bEP1nwICoRbe3Ihp9jBdTwmWeJeHAW/PBu2Ji4+AypghejsxRmGxj0qFX3Id5/LbhmNFEvsCOQY00mGJWaR5lRO/xVonojhcdUEBENj9riRk9ch1slm+dthYsbrAIOM4OBU3260Udz1kz+jZeupwCKivewFBe0DdeEFi0= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Tong Ho To: CC: , , , , , Subject: [PATCH v2 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device Date: Thu, 21 Sep 2023 23:50:08 -0700 Message-ID: <20230922065010.4071693-2-tong.ho@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230922065010.4071693-1-tong.ho@amd.com> References: <20230922065010.4071693-1-tong.ho@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000ECDA:EE_|BL0PR12MB4883:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d1eff2b-d5dc-4d69-3faa-08dbbb383575 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PlxOBCztvQq5IFb7H8aTybivrT4g4R6qV0D7T8wogJ/NOfByvuC4s0rCN4jXbL58EMu93g5wvHUUFR+wGGOUDrf1U4d6HF4GYDI/xdEJa1j6STlaZTrabGah/88rNN6kTzOOl59XEgHwT4qthqo5hq5UvaW3w+cYegKa0eW/V5RcZfeeHgRJG/wwhPxGfuFsMbj0W243TejCT3s9UKPhT6Ymb3cbTVbjW/V8vlnFptbhTMXvG/6wrNwkzkUsP96olcAOoInY+z2zigSK3Bmytzq9aM5vtN1Ub4KbDFiI5NokA/eCsOVzQ1zqLBn/3JWAVd1ynUZg6c6eX1iPMW+31X/MUE/OivcARaWa8RTQP+2WMnfQ3612ReUHRYcml9pL8yLjYwDdaBsb0NZEM5iZEyYyMNzy73bodXQicJfzFCInPAQ47Wlp2nKej/jy+RNAhoFmqC4VxsVhjTqfZ4h2cyiX7VwyLKLkIFdnkOS/H5qMoCwekSB51VECejko6uwDVh5uzw7uKp7m8lUqjSbzjz0sRpk9ZE0OvYQoJClRUdm/kdQOlW/QhtBOrN3H/dIUzyHf5M2h6z3W0AegNRT9m6pWfpQGLYTeR8In8plTfTHDhL9ShlMjGTknRxcku8aE9KwqfqALaEFffNuibwi2lsMdNLsMssmj/U236zn4TZXkD0/i64Czc9C1bBPS/px4jgPTejpa1JnJRbWvMztaGk5V1ILqZOMDQYv1nNjvSAjYlecoSbhtdE/75xJipXKs4K75PSDTxVsO5q2WcvSUkT5oUhS4dcbusMSKQFBQ1Hh/Pg4apsMYuLJh6uDxB2Lh X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB03.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230031)(4636009)(39860400002)(136003)(346002)(396003)(376002)(1800799009)(230921699003)(451199024)(82310400011)(186009)(36840700001)(46966006)(40470700004)(36860700001)(36756003)(426003)(336012)(356005)(66899024)(26005)(81166007)(40480700001)(83380400001)(40460700003)(86362001)(82740400003)(47076005)(44832011)(5660300002)(478600001)(2906002)(30864003)(54906003)(8676002)(8936002)(4326008)(41300700001)(316002)(6916009)(70206006)(70586007)(1076003)(2616005)(6666004)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2023 06:50:29.2183 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9d1eff2b-d5dc-4d69-3faa-08dbbb383575 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000ECDA.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4883 Received-SPF: softfail client-ip=2a01:111:f400:7e8d::60f; envelope-from=tong.ho@amd.com; helo=NAM04-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1695365500398100001 Content-Type: text/plain; charset="utf-8" This adds a non-cryptographic grade implementation of the model for the True Random Number Generator (TRNG) component in AMD/Xilinx Versal device family. This implements all 3 modes defined by the actual hardware specs, all of which selectable by guest software at will at anytime: 1) PRNG mode, in which the generated sequence is required to be reproducible after reseeded by the same 384-bit value as supplied by guest software. 2) Test mode, in which the generated sequence is required to be reproducible ater reseeded by the same 128-bit test seed supplied by guest software. 3) TRNG mode, in which non-reproducible sequence is generated based on periodic reseed by a suitable entropy source. This model is only intended for non-real world testing of guest software, where cryptographically strong PRNG and TRNG is not needed. This model supports versions 1 & 2 of the device, with default to be version 2; the 'hw-version' uint32 property can be set to 0x0100 to override the default. Other implemented properties: - 'forced-prng', uint64 When set to non-zero, mode 3's entropy source is implemented as a deterministic sequence based on the given value and other deterministic parameters. This option allows the emulation to test guest software using mode 3 and to reproduce data-dependent defects. - 'fips-fault-events', uint32, bit-mask bit 3: Triggers the SP800-90B entropy health test fault irq bit 1: Triggers the FIPS 140-2 continuous test fault irq Signed-off-by: Tong Ho --- hw/misc/Kconfig | 3 + hw/misc/meson.build | 3 + hw/misc/xlnx-versal-trng.c | 734 +++++++++++++++++++++++++++++ include/hw/misc/xlnx-versal-trng.h | 57 +++ 4 files changed, 797 insertions(+) create mode 100644 hw/misc/xlnx-versal-trng.c create mode 100644 include/hw/misc/xlnx-versal-trng.h diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index 6996d265e4..6b6105dcbf 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -186,4 +186,7 @@ config AXP2XX_PMU bool depends on I2C =20 +config XLNX_VERSAL_TRNG + bool + source macio/Kconfig diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 88ecab8392..8507ec9e86 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -102,6 +102,9 @@ system_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: file= s( 'xlnx-cfi-if.c', 'xlnx-versal-cframe-reg.c', )) +system_ss.add(when: 'CONFIG_XLNX_VERSAL_TRNG', if_true: files( + 'xlnx-versal-trng.c', +)) system_ss.add(when: 'CONFIG_STM32F2XX_SYSCFG', if_true: files('stm32f2xx_s= yscfg.c')) system_ss.add(when: 'CONFIG_STM32F4XX_SYSCFG', if_true: files('stm32f4xx_s= yscfg.c')) system_ss.add(when: 'CONFIG_STM32F4XX_EXTI', if_true: files('stm32f4xx_ext= i.c')) diff --git a/hw/misc/xlnx-versal-trng.c b/hw/misc/xlnx-versal-trng.c new file mode 100644 index 0000000000..6f52b0d636 --- /dev/null +++ b/hw/misc/xlnx-versal-trng.c @@ -0,0 +1,734 @@ +/* + * Non-crypto strength model of the True Random Number Generator + * in the AMD/Xilinx Versal device family. + * + * Copyright (c) 2017-2020 Xilinx Inc. + * Copyright (c) 2023 Advanced Micro Devices, Inc. + * + * Written by Edgar E. Iglesias + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#include "qemu/osdep.h" +#include "hw/misc/xlnx-versal-trng.h" + +#include "qemu/bitops.h" +#include "qemu/log.h" +#include "qemu/error-report.h" +#include "qemu/timer.h" +#include "qapi/visitor.h" +#include "migration/vmstate.h" +#include "hw/qdev-properties.h" + +#ifndef XLNX_VERSAL_TRNG_ERR_DEBUG +#define XLNX_VERSAL_TRNG_ERR_DEBUG 0 +#endif + +REG32(INT_CTRL, 0x0) + FIELD(INT_CTRL, CERTF_RST, 5, 1) + FIELD(INT_CTRL, DTF_RST, 4, 1) + FIELD(INT_CTRL, DONE_RST, 3, 1) + FIELD(INT_CTRL, CERTF_EN, 2, 1) + FIELD(INT_CTRL, DTF_EN, 1, 1) + FIELD(INT_CTRL, DONE_EN, 0, 1) +REG32(STATUS, 0x4) + FIELD(STATUS, QCNT, 9, 3) + FIELD(STATUS, EAT, 4, 5) + FIELD(STATUS, CERTF, 3, 1) + FIELD(STATUS, DTF, 1, 1) + FIELD(STATUS, DONE, 0, 1) +REG32(CTRL, 0x8) + FIELD(CTRL, PERSODISABLE, 10, 1) + FIELD(CTRL, SINGLEGENMODE, 9, 1) + FIELD(CTRL, EUMODE, 8, 1) + FIELD(CTRL, PRNGMODE, 7, 1) + FIELD(CTRL, TSTMODE, 6, 1) + FIELD(CTRL, PRNGSTART, 5, 1) + FIELD(CTRL, EATAU, 4, 1) + FIELD(CTRL, PRNGXS, 3, 1) + FIELD(CTRL, TRSSEN, 2, 1) + FIELD(CTRL, QERTUEN, 1, 1) + FIELD(CTRL, PRNGSRST, 0, 1) +REG32(CTRL_2, 0xc) + FIELD(CTRL_2, REPCOUNTTESTCUTOFF, 8, 9) + FIELD(CTRL_2, RESERVED_7_5, 5, 3) + FIELD(CTRL_2, DIT, 0, 5) +REG32(CTRL_3, 0x10) + FIELD(CTRL_3, ADAPTPROPTESTCUTOFF, 8, 10) + FIELD(CTRL_3, DLEN, 0, 8) +REG32(CTRL_4, 0x14) + FIELD(CTRL_4, SINGLEBITRAW, 0, 1) +REG32(EXT_SEED_0, 0x40) +REG32(EXT_SEED_1, 0x44) +REG32(EXT_SEED_2, 0x48) +REG32(EXT_SEED_3, 0x4c) +REG32(EXT_SEED_4, 0x50) +REG32(EXT_SEED_5, 0x54) +REG32(EXT_SEED_6, 0x58) +REG32(EXT_SEED_7, 0x5c) +REG32(EXT_SEED_8, 0x60) +REG32(EXT_SEED_9, 0x64) +REG32(EXT_SEED_10, 0x68) +REG32(EXT_SEED_11, 0x6c) +REG32(PER_STRNG_0, 0x80) +REG32(PER_STRNG_1, 0x84) +REG32(PER_STRNG_2, 0x88) +REG32(PER_STRNG_3, 0x8c) +REG32(PER_STRNG_4, 0x90) +REG32(PER_STRNG_5, 0x94) +REG32(PER_STRNG_6, 0x98) +REG32(PER_STRNG_7, 0x9c) +REG32(PER_STRNG_8, 0xa0) +REG32(PER_STRNG_9, 0xa4) +REG32(PER_STRNG_10, 0xa8) +REG32(PER_STRNG_11, 0xac) +REG32(CORE_OUTPUT, 0xc0) +REG32(RESET, 0xd0) + FIELD(RESET, VAL, 0, 1) +REG32(OSC_EN, 0xd4) + FIELD(OSC_EN, VAL, 0, 1) +REG32(TRNG_ISR, 0xe0) + FIELD(TRNG_ISR, SLVERR, 1, 1) + FIELD(TRNG_ISR, CORE_INT, 0, 1) +REG32(TRNG_IMR, 0xe4) + FIELD(TRNG_IMR, SLVERR, 1, 1) + FIELD(TRNG_IMR, CORE_INT, 0, 1) +REG32(TRNG_IER, 0xe8) + FIELD(TRNG_IER, SLVERR, 1, 1) + FIELD(TRNG_IER, CORE_INT, 0, 1) +REG32(TRNG_IDR, 0xec) + FIELD(TRNG_IDR, SLVERR, 1, 1) + FIELD(TRNG_IDR, CORE_INT, 0, 1) +REG32(SLV_ERR_CTRL, 0xf0) + FIELD(SLV_ERR_CTRL, ENABLE, 0, 1) + +#define R_MAX (R_SLV_ERR_CTRL + 1) + +QEMU_BUILD_BUG_ON(R_MAX * 4 !=3D sizeof_field(XlnxVersalTRng, regs)); + +#define TRNG_GUEST_ERROR(D, FMT, ...) \ + do { \ + g_autofree char *p =3D object_get_canonical_path(OBJECT(D)); \ + qemu_log_mask(LOG_GUEST_ERROR, "%s: " FMT, p, ## __VA_ARGS__); \ + } while (0) + +#define TRNG_WARN(D, FMT, ...) \ + do { \ + g_autofree char *p =3D object_get_canonical_path(OBJECT(D)); \ + warn_report("%s: " FMT, p, ## __VA_ARGS__); \ + } while (0) + +static bool trng_older_than_v2(XlnxVersalTRng *s) +{ + return s->hw_version < 0x0200; +} + +static bool trng_in_reset(XlnxVersalTRng *s) +{ + if (ARRAY_FIELD_EX32(s->regs, RESET, VAL)) { + return true; + } + if (ARRAY_FIELD_EX32(s->regs, CTRL, PRNGSRST)) { + return true; + } + + return false; +} + +static bool trng_test_enabled(XlnxVersalTRng *s) +{ + return ARRAY_FIELD_EX32(s->regs, CTRL, TSTMODE); +} + +static bool trng_trss_enabled(XlnxVersalTRng *s) +{ + if (trng_in_reset(s)) { + return false; + } + if (!ARRAY_FIELD_EX32(s->regs, CTRL, TRSSEN)) { + return false; + } + if (!ARRAY_FIELD_EX32(s->regs, OSC_EN, VAL)) { + return false; + } + + return true; +} + +static void trng_le384(uint32_t *le384, const uint32_t *h384) +{ + size_t i; + + for (i =3D 0; i < (384 / 32); i++) { + le384[i] =3D cpu_to_le32(h384[i]); + } +} + +static void trng_reseed(XlnxVersalTRng *s) +{ + bool ext_seed =3D ARRAY_FIELD_EX32(s->regs, CTRL, PRNGXS); + bool pers_disabled =3D ARRAY_FIELD_EX32(s->regs, CTRL, PERSODISABLE); + + enum { + U384_U32 =3D 384 / 32, + }; + + /* + * Maximum seed length is len(personalized string) + len(ext seed). + * + * Use little-endian to ensure guest sequence being indepedent of + * host endian. + */ + struct { + guint32 ps[U384_U32]; + union { + uint64_t int_seed[2]; + guint32 ext_seed[U384_U32]; + }; + } gs; + + /* + * A disabled personalized string is the same as + * a string with all zeros. + * + * The device's hardware spec defines 3 modes (all selectable + * by guest at will and at anytime): + * 1) External seeding + * This is a PRNG mode, in which the produced sequence shall + * be reproducible if reseeded by the same 384-bit seed, as + * supplied by guest software. + * 2) Test seeding + * This is a PRNG mode, in which the produced sequence shall + * be reproducible if reseeded by a 128-bit test seed, as + * supplied by guest software. + * 3) Truly-random seeding + * This is the TRNG mode, in which the produced sequence is + * periodically reseeded by a crypto-strength entropy source. + * + * To assist debugging of certain classes of software defects, + * this QEMU model implements a 4th mode, + * 4) Forced PRNG + * When in this mode, a reproducible sequence is generated + * if software has selected the TRNG mode (mode 2). + * + * This emulation-only mode can only be selected by setting + * the uint64 property 'forced-prng' to a non-zero value. + * Guest software cannot select this mode. + */ + memset(&gs, 0, sizeof(gs)); + gs.ext_seed[ARRAY_SIZE(gs.ext_seed) - 1] =3D cpu_to_be32(1); + + if (!pers_disabled) { + trng_le384(gs.ps, &s->regs[R_PER_STRNG_0]); + } + + if (ext_seed) { + trng_le384(gs.ext_seed, &s->regs[R_EXT_SEED_0]); + } else if (trng_test_enabled(s)) { + gs.int_seed[0] =3D cpu_to_le64(s->tst_seed[0]); + gs.int_seed[1] =3D cpu_to_le64(s->tst_seed[1]); + } else if (s->forced_prng_seed) { + s->forced_prng_count++; + gs.int_seed[0] =3D cpu_to_le64(s->forced_prng_count); + gs.int_seed[1] =3D cpu_to_le64(s->forced_prng_seed); + } else { + gs.int_seed[0] =3D cpu_to_le64(qemu_clock_get_ns(QEMU_CLOCK_HOST)); + gs.int_seed[1] =3D cpu_to_le64(getpid()); + } + + g_rand_set_seed_array(s->prng, gs.ps, + sizeof(gs) / sizeof(guint32)); + + s->rand_count =3D 0; + s->rand_reseed =3D 1ULL << 48; +} + +static void trng_regen(XlnxVersalTRng *s) +{ + if (s->rand_reseed =3D=3D 0) { + TRNG_GUEST_ERROR(s, "Too many generations without a reseed"); + trng_reseed(s); + } + s->rand_reseed--; + + /* + * In real hardware, each regen creates 256 bits, but QCNT + * reports a max of 4. + */ + ARRAY_FIELD_DP32(s->regs, STATUS, QCNT, 4); + s->rand_count =3D 256 / 32; +} + +static uint32_t trng_rdout(XlnxVersalTRng *s) +{ + assert(s->rand_count); + + s->rand_count--; + if (s->rand_count < 4) { + ARRAY_FIELD_DP32(s->regs, STATUS, QCNT, s->rand_count); + } + + /* Reject all 0's and all 1's */ + while (true) { + /* g_rand_int_range() returns gint32, not guint32 */ + guint32 nr =3D g_rand_int(s->prng); + + if (nr && (nr !=3D ~0)) { + return nr; + } + } +} + +static void trng_irq_update(XlnxVersalTRng *s) +{ + bool pending =3D s->regs[R_TRNG_ISR] & ~s->regs[R_TRNG_IMR]; + qemu_set_irq(s->irq, pending); +} + +static void trng_isr_postw(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + trng_irq_update(s); +} + +static uint64_t trng_ier_prew(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + uint32_t val =3D val64; + + s->regs[R_TRNG_IMR] &=3D ~val; + trng_irq_update(s); + return 0; +} + +static uint64_t trng_idr_prew(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + uint32_t val =3D val64; + + s->regs[R_TRNG_IMR] |=3D val; + trng_irq_update(s); + return 0; +} + +static void trng_core_int_update(XlnxVersalTRng *s) +{ + bool pending =3D false; + uint32_t st =3D s->regs[R_STATUS]; + uint32_t en =3D s->regs[R_INT_CTRL]; + + if (FIELD_EX32(st, STATUS, CERTF) && FIELD_EX32(en, INT_CTRL, CERTF_EN= )) { + pending =3D true; + } + + if (FIELD_EX32(st, STATUS, DTF) && FIELD_EX32(en, INT_CTRL, DTF_EN)) { + pending =3D true; + } + + if (FIELD_EX32(st, STATUS, DONE) && FIELD_EX32(en, INT_CTRL, DONE_EN))= { + pending =3D true; + } + + ARRAY_FIELD_DP32(s->regs, TRNG_ISR, CORE_INT, pending); + trng_irq_update(s); +} + +static void trng_int_ctrl_postw(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + uint32_t v32 =3D val64; + uint32_t clr_mask =3D 0; + + if (FIELD_EX32(v32, INT_CTRL, CERTF_RST)) { + clr_mask |=3D R_STATUS_CERTF_MASK; + } + if (FIELD_EX32(v32, INT_CTRL, DTF_RST)) { + clr_mask |=3D R_STATUS_DTF_MASK; + } + if (FIELD_EX32(v32, INT_CTRL, DONE_RST)) { + clr_mask |=3D R_STATUS_DONE_MASK; + } + + s->regs[R_STATUS] &=3D ~clr_mask; + trng_core_int_update(s); +} + +static void trng_done(XlnxVersalTRng *s) +{ + ARRAY_FIELD_DP32(s->regs, STATUS, DONE, true); + trng_core_int_update(s); +} + +static void trng_fault_event_set(XlnxVersalTRng *s, uint32_t events) +{ + bool pending =3D false; + + /* Disabled TRSS cannot generate any fault event */ + if (!trng_trss_enabled(s)) { + return; + } + + if (FIELD_EX32(events, STATUS, CERTF)) { + /* In older version, ERTU must be enabled explicitly to get CERTF = */ + if (trng_older_than_v2(s) && + !ARRAY_FIELD_EX32(s->regs, CTRL, QERTUEN)) { + TRNG_WARN(s, "CERTF injection ignored: ERTU disabled"); + } else { + ARRAY_FIELD_DP32(s->regs, STATUS, CERTF, true); + pending =3D true; + } + } + + if (FIELD_EX32(events, STATUS, DTF)) { + ARRAY_FIELD_DP32(s->regs, STATUS, DTF, true); + pending =3D true; + } + + if (pending) { + trng_core_int_update(s); + } +} + +static void trng_soft_reset(XlnxVersalTRng *s) +{ + s->rand_count =3D 0; + s->regs[R_STATUS] =3D 0; + + ARRAY_FIELD_DP32(s->regs, TRNG_ISR, CORE_INT, 0); +} + +static void trng_ctrl_postw(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + + if (trng_in_reset(s)) { + return; + } + + if (FIELD_EX32(val64, CTRL, PRNGSRST)) { + trng_soft_reset(s); + trng_irq_update(s); + return; + } + + if (!FIELD_EX32(val64, CTRL, PRNGSTART)) { + return; + } + + if (FIELD_EX32(val64, CTRL, PRNGMODE)) { + trng_regen(s); + } else { + trng_reseed(s); + } + + trng_done(s); +} + +static void trng_ctrl4_postw(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + + if (trng_older_than_v2(s)) { + return; + } + + /* Only applies to test mode with TRSS enabled */ + if (!trng_test_enabled(s) || !trng_trss_enabled(s)) { + return; + } + + /* Shift in a single bit. */ + s->tst_seed[1] <<=3D 1; + s->tst_seed[1] |=3D s->tst_seed[0] >> 63; + s->tst_seed[0] <<=3D 1; + s->tst_seed[0] |=3D val64 & 1; + + trng_reseed(s); + trng_regen(s); +} + +static uint64_t trng_core_out_postr(RegisterInfo *reg, uint64_t val) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + bool oneshot =3D ARRAY_FIELD_EX32(s->regs, CTRL, SINGLEGENMODE); + bool start =3D ARRAY_FIELD_EX32(s->regs, CTRL, PRNGSTART); + uint32_t r =3D 0xbad; + + if (trng_in_reset(s)) { + TRNG_GUEST_ERROR(s, "Reading random number while in reset!"); + return r; + } + + if (s->rand_count =3D=3D 0) { + TRNG_GUEST_ERROR(s, "Reading random number when unavailable!"); + return r; + } + + r =3D trng_rdout(s); + + /* Automatic mode regenerates when half the output reg is empty. */ + if (!oneshot && start && s->rand_count <=3D 3) { + trng_regen(s); + } + + return r; +} + +static void trng_reset(XlnxVersalTRng *s) +{ + unsigned int i; + + s->forced_prng_count =3D 0; + + for (i =3D 0; i < ARRAY_SIZE(s->regs_info); ++i) { + register_reset(&s->regs_info[i]); + } + trng_soft_reset(s); + trng_irq_update(s); +} + +static uint64_t trng_reset_prew(RegisterInfo *reg, uint64_t val64) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg->opaque); + + if (!ARRAY_FIELD_EX32(s->regs, RESET, VAL) && + FIELD_EX32(val64, RESET, VAL)) { + trng_reset(s); + } + + return val64; +} + +static uint64_t trng_register_read(void *opaque, hwaddr addr, unsigned siz= e) +{ + /* + * Guest provided seed and personalized strings cannot be + * read back, and read attempts return value of A_STATUS. + */ + switch (addr) { + case A_EXT_SEED_0 ... A_PER_STRNG_11: + addr =3D A_STATUS; + break; + } + + return register_read_memory(opaque, addr, size); +} + +static void trng_register_write(void *opaque, hwaddr addr, + uint64_t value, unsigned size) +{ + RegisterInfoArray *reg_array =3D opaque; + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(reg_array->r[0]->opaque); + + if (trng_older_than_v2(s)) { + switch (addr) { + case A_CTRL: + value =3D FIELD_DP64(value, CTRL, PERSODISABLE, 0); + value =3D FIELD_DP64(value, CTRL, SINGLEGENMODE, 0); + break; + case A_CTRL_2: + case A_CTRL_3: + case A_CTRL_4: + return; + } + } else { + switch (addr) { + case A_CTRL: + value =3D FIELD_DP64(value, CTRL, EATAU, 0); + value =3D FIELD_DP64(value, CTRL, QERTUEN, 0); + break; + } + } + + register_write_memory(opaque, addr, value, size); +} + +static RegisterAccessInfo trng_regs_info[] =3D { + { .name =3D "INT_CTRL", .addr =3D A_INT_CTRL, + .post_write =3D trng_int_ctrl_postw, + },{ .name =3D "STATUS", .addr =3D A_STATUS, + .ro =3D 0xfff, + },{ .name =3D "CTRL", .addr =3D A_CTRL, + .post_write =3D trng_ctrl_postw, + },{ .name =3D "CTRL_2", .addr =3D A_CTRL_2, + .reset =3D 0x210c, + },{ .name =3D "CTRL_3", .addr =3D A_CTRL_3, + .reset =3D 0x26f09, + },{ .name =3D "CTRL_4", .addr =3D A_CTRL_4, + .post_write =3D trng_ctrl4_postw, + },{ .name =3D "EXT_SEED_0", .addr =3D A_EXT_SEED_0, + },{ .name =3D "EXT_SEED_1", .addr =3D A_EXT_SEED_1, + },{ .name =3D "EXT_SEED_2", .addr =3D A_EXT_SEED_2, + },{ .name =3D "EXT_SEED_3", .addr =3D A_EXT_SEED_3, + },{ .name =3D "EXT_SEED_4", .addr =3D A_EXT_SEED_4, + },{ .name =3D "EXT_SEED_5", .addr =3D A_EXT_SEED_5, + },{ .name =3D "EXT_SEED_6", .addr =3D A_EXT_SEED_6, + },{ .name =3D "EXT_SEED_7", .addr =3D A_EXT_SEED_7, + },{ .name =3D "EXT_SEED_8", .addr =3D A_EXT_SEED_8, + },{ .name =3D "EXT_SEED_9", .addr =3D A_EXT_SEED_9, + },{ .name =3D "EXT_SEED_10", .addr =3D A_EXT_SEED_10, + },{ .name =3D "EXT_SEED_11", .addr =3D A_EXT_SEED_11, + },{ .name =3D "PER_STRNG_0", .addr =3D A_PER_STRNG_0, + },{ .name =3D "PER_STRNG_1", .addr =3D A_PER_STRNG_1, + },{ .name =3D "PER_STRNG_2", .addr =3D A_PER_STRNG_2, + },{ .name =3D "PER_STRNG_3", .addr =3D A_PER_STRNG_3, + },{ .name =3D "PER_STRNG_4", .addr =3D A_PER_STRNG_4, + },{ .name =3D "PER_STRNG_5", .addr =3D A_PER_STRNG_5, + },{ .name =3D "PER_STRNG_6", .addr =3D A_PER_STRNG_6, + },{ .name =3D "PER_STRNG_7", .addr =3D A_PER_STRNG_7, + },{ .name =3D "PER_STRNG_8", .addr =3D A_PER_STRNG_8, + },{ .name =3D "PER_STRNG_9", .addr =3D A_PER_STRNG_9, + },{ .name =3D "PER_STRNG_10", .addr =3D A_PER_STRNG_10, + },{ .name =3D "PER_STRNG_11", .addr =3D A_PER_STRNG_11, + },{ .name =3D "CORE_OUTPUT", .addr =3D A_CORE_OUTPUT, + .ro =3D 0xffffffff, + .post_read =3D trng_core_out_postr, + },{ .name =3D "RESET", .addr =3D A_RESET, + .reset =3D 0x1, + .pre_write =3D trng_reset_prew, + },{ .name =3D "OSC_EN", .addr =3D A_OSC_EN, + },{ .name =3D "TRNG_ISR", .addr =3D A_TRNG_ISR, + .w1c =3D 0x3, + .post_write =3D trng_isr_postw, + },{ .name =3D "TRNG_IMR", .addr =3D A_TRNG_IMR, + .reset =3D 0x3, + .ro =3D 0x3, + },{ .name =3D "TRNG_IER", .addr =3D A_TRNG_IER, + .pre_write =3D trng_ier_prew, + },{ .name =3D "TRNG_IDR", .addr =3D A_TRNG_IDR, + .pre_write =3D trng_idr_prew, + },{ .name =3D "SLV_ERR_CTRL", .addr =3D A_SLV_ERR_CTRL, + } +}; + +static const MemoryRegionOps trng_ops =3D { + .read =3D trng_register_read, + .write =3D trng_register_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + }, +}; + +static void trng_init(Object *obj) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(obj); + SysBusDevice *sbd =3D SYS_BUS_DEVICE(obj); + RegisterInfoArray *reg_array; + + reg_array =3D + register_init_block32(DEVICE(obj), trng_regs_info, + ARRAY_SIZE(trng_regs_info), + s->regs_info, s->regs, + &trng_ops, + XLNX_VERSAL_TRNG_ERR_DEBUG, + R_MAX * 4); + + sysbus_init_mmio(sbd, ®_array->mem); + sysbus_init_irq(sbd, &s->irq); + + s->prng =3D g_rand_new(); +} + +static void trng_unrealize(DeviceState *dev) +{ + XlnxVersalTRng *s =3D XLNX_VERSAL_TRNG(dev); + + g_rand_free(s->prng); + s->prng =3D NULL; +} + +static void trng_reset_hold(Object *obj) +{ + trng_reset(XLNX_VERSAL_TRNG(obj)); +} + +static void trng_prop_fault_event_set(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + uint32_t events =3D 0; + + visit_type_uint32(v, name, &events, errp); + if (*errp) { + return; + } + + trng_fault_event_set(XLNX_VERSAL_TRNG(obj), events); +} + +static const PropertyInfo trng_prop_fault_events =3D { + .name =3D "uint32:bits", + .description =3D "Set to trigger TRNG fault events", + .set =3D trng_prop_fault_event_set, + .realized_set_allowed =3D true, +}; + +static PropertyInfo trng_prop_uint64; /* to extend qdev_prop_uint64 */ + +static Property trng_props[] =3D { + DEFINE_PROP_UINT64("forced-prng", XlnxVersalTRng, forced_prng_seed, 0), + DEFINE_PROP_UINT32("hw-version", XlnxVersalTRng, hw_version, 0x0200), + { .name =3D "fips-fault-events", .info =3D &trng_prop_fault_events, }, + + DEFINE_PROP_END_OF_LIST(), +}; + +static const VMStateDescription vmstate_trng =3D { + .name =3D TYPE_XLNX_VERSAL_TRNG, + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32_ARRAY(regs, XlnxVersalTRng, R_MAX), + VMSTATE_END_OF_LIST(), + } +}; + +static void trng_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + + dc->vmsd =3D &vmstate_trng; + dc->unrealize =3D trng_unrealize; + rc->phases.hold =3D trng_reset_hold; + + /* Clone uint64 property with set allowed after realized */ + trng_prop_uint64 =3D qdev_prop_uint64; + trng_prop_uint64.realized_set_allowed =3D true; + trng_props[0].info =3D &trng_prop_uint64; + + device_class_set_props(dc, trng_props); +} + +static const TypeInfo trng_info =3D { + .name =3D TYPE_XLNX_VERSAL_TRNG, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(XlnxVersalTRng), + .class_init =3D trng_class_init, + .instance_init =3D trng_init, +}; + +static void trng_register_types(void) +{ + type_register_static(&trng_info); +} + +type_init(trng_register_types) diff --git a/include/hw/misc/xlnx-versal-trng.h b/include/hw/misc/xlnx-vers= al-trng.h new file mode 100644 index 0000000000..d25508c424 --- /dev/null +++ b/include/hw/misc/xlnx-versal-trng.h @@ -0,0 +1,57 @@ +/* + * Non-crypto strength model of the True Random Number Generator + * in the AMD/Xilinx Versal device family. + * + * Copyright (c) 2017-2020 Xilinx Inc. + * Copyright (c) 2023 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#ifndef XLNX_VERSAL_TRNG_H +#define XLNX_VERSAL_TRNG_H + +#include "hw/irq.h" +#include "hw/sysbus.h" +#include "hw/register.h" + +#define TYPE_XLNX_VERSAL_TRNG "xlnx.versal-trng" +OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalTRng, XLNX_VERSAL_TRNG); + +#define RMAX_XLNX_VERSAL_TRNG ((0xf0 / 4) + 1) + +typedef struct XlnxVersalTRng { + SysBusDevice parent_obj; + qemu_irq irq; + GRand *prng; + + uint32_t hw_version; + + uint32_t rand_count; + uint64_t rand_reseed; + + uint64_t forced_prng_seed; + uint64_t forced_prng_count; + uint64_t tst_seed[2]; + + uint32_t regs[RMAX_XLNX_VERSAL_TRNG]; + RegisterInfo regs_info[RMAX_XLNX_VERSAL_TRNG]; +} XlnxVersalTRng; + +#undef RMAX_XLNX_VERSAL_TRNG +#endif --=20 2.25.1 From nobody Sat May 18 08:35:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1695365491; cv=pass; d=zohomail.com; s=zohoarc; b=immnIhj0Ko+m8iwuVJfz43z2L0mwb3mzbzLpy/Qseiy3oesyLjI9rQPKqn1gYfJXmulgIhiMCmFINZhbxUKIBwYZEcI3KLlNMG91oCXw8Ycufu1/0LhHicbyGVNN6jXZ69WgIXeEUHliEq4mo1gHjWzw5TTfLkzmtbmkcNJPzeM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695365491; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FJeH2A5KXqVydfk0l7rw+MP01ExufkDrC9mmwP2Pet0=; b=XkfCizmYNmsUNkQ0cSh7XxIHHAsSmiOmCjiNNsBgcanR6JfdhMVjzkdtqglE5XVjBNYGq26JXhvIjr7YSCELPjWKN8hwOl+MpFfoUvWaqXyy8c9syg620rzLbl7iny0E1rnOnuoaYJjcXD9jjg/QJnFKEXgUHgp+cGxn/4uIrFM= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1695365491572949.6202545342829; Thu, 21 Sep 2023 23:51:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qjZzy-00020U-DG; Fri, 22 Sep 2023 02:50:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjZzv-0001ze-Gc; Fri, 22 Sep 2023 02:50:43 -0400 Received: from mail-sn1nam02on2062c.outbound.protection.outlook.com ([2a01:111:f400:7ea9::62c] helo=NAM02-SN1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjZzo-00046g-Nh; Fri, 22 Sep 2023 02:50:38 -0400 Received: from BL1PR13CA0323.namprd13.prod.outlook.com (2603:10b6:208:2c1::28) by PH7PR12MB7018.namprd12.prod.outlook.com (2603:10b6:510:1b8::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.20; Fri, 22 Sep 2023 06:50:31 +0000 Received: from MN1PEPF0000ECDA.namprd02.prod.outlook.com (2603:10b6:208:2c1:cafe::5d) by BL1PR13CA0323.outlook.office365.com (2603:10b6:208:2c1::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.12 via Frontend Transport; Fri, 22 Sep 2023 06:50:31 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by MN1PEPF0000ECDA.mail.protection.outlook.com (10.167.242.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6792.20 via Frontend Transport; Fri, 22 Sep 2023 06:50:31 +0000 Received: from SATLEXMB03.amd.com (10.181.40.144) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 22 Sep 2023 01:50:24 -0500 Received: from xsjtongh40.xilinx.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server id 15.1.2507.27 via Frontend Transport; Fri, 22 Sep 2023 01:50:24 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N8KNyXKDWA5YWckaYXOBFm9qVjBqiqKvJyxRGzBuCrMD6Gric+SEi0w7/mg6bxlnVGvwAD2mbvXS2sO5FbDF71bUHLXVZcawCT3JY50s0G0J+DviFERs7L1HO4GBcV4rqLyWwiNFJEQHGsyvqw2rlMSiIKUffFUXu7Bqw8sURZIiuwcH1PZIqdrqI0+kHWNVShMW9Hqfi3t2f0fo5lSSxzfjjAa43v5EWTqsucg3eMypvW/6XzlelYVJ1qRDe+Ck2a8HRmHYbkXq+iATR1LbR4BcaMLnMBv9H1kae1mkMHG0PaRBQSoAxnuTZ0FmhN83+2Ku/GLXJYGdl4XrQmgfMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=FJeH2A5KXqVydfk0l7rw+MP01ExufkDrC9mmwP2Pet0=; b=VqklFI53SbX4qC9nv+eELZwN/akysP07XoWzwUwOTrVAbC+fQaIrvOm+3SBqYL3STJD8BhltFGuMsjAXTjM52KuzPcqZ3wbNmRzmP1Hesgve0iHb75T6Oj0j2MH50wb3u18wVicgZb0rI0/Rp8aBZPqZEmz8lRY3u04NzQua2giPVx8EHPCIPvCkB0HUACiJUqY11sYf0KymtAUSDA6lvGY3mX6sOOFrwgZxfOUDd7UEyo6X6oDajhXZQtoWwIVmihgS72JcCMIRuaG1UMhYHtLmy6P9R+ZCselL9etV2qSuT6SJDxIuKD6f+J6i1JSDCfG29gJPIzdfvq86NVi2bg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nongnu.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FJeH2A5KXqVydfk0l7rw+MP01ExufkDrC9mmwP2Pet0=; b=WNvG2QbImdtk0ORZvvsSJyDwyUG2SBUWJbYpB00sJcp5oA3pBxFvEhNJDVOBxO4FfAa9ED14ilwizmS/eMFnytsgOA+XjyPkg/1hLIPbIrM+EQ229/aCU57daGw1elylaOerTUE1f348x9q8myBoFuir5V4b9pvhaJFos3L3c04= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Tong Ho To: CC: , , , , , Subject: [PATCH v2 2/3] hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device Date: Thu, 21 Sep 2023 23:50:09 -0700 Message-ID: <20230922065010.4071693-3-tong.ho@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230922065010.4071693-1-tong.ho@amd.com> References: <20230922065010.4071693-1-tong.ho@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000ECDA:EE_|PH7PR12MB7018:EE_ X-MS-Office365-Filtering-Correlation-Id: b90c16fc-851c-4f64-43f7-08dbbb3836be X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rU4jYawrqn7OQFu22bw/Le1XWXW6kNLf3DN4VGVz2Mgo06uiTspMcloMW+RpCPj2X5bdZVDFaaBn1xeJzSwCZ8uqukXUMutADPITSHeI29clkoot6i9DUNu270FEWHm7g1TovwO4YKA73ZOeNLDdPJKCdjOeH5IAdbNhTwODrqbfyJheapOb97+qSbIP4JrxESHTiNJGhAbTd5fCFFJ+47xhmYZ7W/LmgvqezbKdzwg7QLnRa+9qGW7OFmLh9+P3omAy6DE8qiMk25AreEYTFIYI9QUDfthpxEbV8lHRoMO/zsWICIHuAQ6aVex46FeaN9tXir1sOQnp1Tp4Cr/MuR5YK9RqIkEce/8zqUWnW1tfeENKScLMEjZYypNhCWS52KuqR70uPOlX5TtAIjXX9oORvRfEteNAzt3kn0y4RVewsmpLxcth1CFOpjSDcQDYTLvhRylDP1oMWDDISwiZIfmTWMaor6hhBs03wNi7PHwircJoYR1n9RNxljetYmQX2AU8uITN9RLbKDcTBgtDHD3BxRm8kAailzpCTGwtQvxHhfJvgSG3nzesVhxOrr4G96Al35lMzmyLPWhXSp4bz3SBsWjKR7JlsIGBEmI66ORZNDrr0kRcxgU0GPt4YDvwH1jbBvDT3dqvH0Qfbrd7lLrP1uaIXH4o9tFdI7STGsPY/rUYkA0PiAH2UvsI3ciOt8t4cv2yHSBwa+AzkMzd/BfW1mfUhQqosORJAB/FCslSFfyzaxR6FW8vK5sNTxtyz+q/La0n/6inQC6ME338D9cGDCVEZLzlny9FVKV+7I5karTLJa/YCMNBG/mQpLKQ X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB03.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230031)(4636009)(376002)(136003)(39860400002)(396003)(346002)(82310400011)(230921699003)(1800799009)(186009)(451199024)(40470700004)(46966006)(36840700001)(81166007)(83380400001)(316002)(70206006)(70586007)(6916009)(6666004)(36756003)(26005)(1076003)(336012)(356005)(2616005)(426003)(478600001)(47076005)(40460700003)(5660300002)(44832011)(2906002)(82740400003)(40480700001)(54906003)(4326008)(8676002)(8936002)(86362001)(41300700001)(36860700001)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2023 06:50:31.3590 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b90c16fc-851c-4f64-43f7-08dbbb3836be X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000ECDA.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7018 Received-SPF: softfail client-ip=2a01:111:f400:7ea9::62c; envelope-from=tong.ho@amd.com; helo=NAM02-SN1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1695365492249100001 Content-Type: text/plain; charset="utf-8" Connect the support for Versal True Random Number Generator (TRNG) device. Warning: unlike the TRNG component in a real device from the Versal device familiy, the connected TRNG model is not of cryptographic grade and is not intended for use cases when cryptograpically strong TRNG is needed. Signed-off-by: Tong Ho --- hw/arm/Kconfig | 1 + hw/arm/xlnx-versal-virt.c | 20 ++++++++++++++++++++ hw/arm/xlnx-versal.c | 16 ++++++++++++++++ include/hw/arm/xlnx-versal.h | 5 +++++ 4 files changed, 42 insertions(+) diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig index 7e68348440..0a3ff6748d 100644 --- a/hw/arm/Kconfig +++ b/hw/arm/Kconfig @@ -482,6 +482,7 @@ config XLNX_VERSAL select XLNX_BBRAM select XLNX_EFUSE_VERSAL select XLNX_USB_SUBSYS + select XLNX_VERSAL_TRNG =20 config NPCM7XX bool diff --git a/hw/arm/xlnx-versal-virt.c b/hw/arm/xlnx-versal-virt.c index 88c561ff63..d99255ee89 100644 --- a/hw/arm/xlnx-versal-virt.c +++ b/hw/arm/xlnx-versal-virt.c @@ -391,6 +391,25 @@ static void fdt_add_rtc_node(VersalVirt *s) g_free(name); } =20 +static void fdt_add_trng_node(VersalVirt *s) +{ + const char compat[] =3D TYPE_XLNX_VERSAL_TRNG; + const char interrupt_names[] =3D "trng"; + g_autofree char *name =3D g_strdup_printf("/trng@%x", MM_PMC_TRNG); + + qemu_fdt_add_subnode(s->fdt, name); + + qemu_fdt_setprop_cells(s->fdt, name, "interrupts", + GIC_FDT_IRQ_TYPE_SPI, VERSAL_TRNG_IRQ, + GIC_FDT_IRQ_FLAGS_LEVEL_HI); + qemu_fdt_setprop(s->fdt, name, "interrupt-names", + interrupt_names, sizeof(interrupt_names)); + qemu_fdt_setprop_sized_cells(s->fdt, name, "reg", + 2, MM_PMC_TRNG, + 2, MM_PMC_TRNG_SIZE); + qemu_fdt_setprop(s->fdt, name, "compatible", compat, sizeof(compat)); +} + static void fdt_add_bbram_node(VersalVirt *s) { const char compat[] =3D TYPE_XLNX_BBRAM; @@ -690,6 +709,7 @@ static void versal_virt_init(MachineState *machine) fdt_add_usb_xhci_nodes(s); fdt_add_sd_nodes(s); fdt_add_rtc_node(s); + fdt_add_trng_node(s); fdt_add_bbram_node(s); fdt_add_efuse_ctrl_node(s); fdt_add_efuse_cache_node(s); diff --git a/hw/arm/xlnx-versal.c b/hw/arm/xlnx-versal.c index fa556d8764..4f74a64a0d 100644 --- a/hw/arm/xlnx-versal.c +++ b/hw/arm/xlnx-versal.c @@ -373,6 +373,21 @@ static void versal_create_rtc(Versal *s, qemu_irq *pic) qdev_get_gpio_in(DEVICE(&s->pmc.apb_irq_orgate), 0)= ); } =20 +static void versal_create_trng(Versal *s, qemu_irq *pic) +{ + SysBusDevice *sbd; + MemoryRegion *mr; + + object_initialize_child(OBJECT(s), "trng", &s->pmc.trng, + TYPE_XLNX_VERSAL_TRNG); + sbd =3D SYS_BUS_DEVICE(&s->pmc.trng); + sysbus_realize(sbd, &error_fatal); + + mr =3D sysbus_mmio_get_region(sbd, 0); + memory_region_add_subregion(&s->mr_ps, MM_PMC_TRNG, mr); + sysbus_connect_irq(sbd, 0, pic[VERSAL_TRNG_IRQ]); +} + static void versal_create_xrams(Versal *s, qemu_irq *pic) { int nr_xrams =3D ARRAY_SIZE(s->lpd.xram.ctrl); @@ -909,6 +924,7 @@ static void versal_realize(DeviceState *dev, Error **er= rp) versal_create_sds(s, pic); versal_create_pmc_apb_irq_orgate(s, pic); versal_create_rtc(s, pic); + versal_create_trng(s, pic); versal_create_xrams(s, pic); versal_create_bbram(s, pic); versal_create_efuse(s, pic); diff --git a/include/hw/arm/xlnx-versal.h b/include/hw/arm/xlnx-versal.h index 7b419f88c2..54f4b98d9d 100644 --- a/include/hw/arm/xlnx-versal.h +++ b/include/hw/arm/xlnx-versal.h @@ -31,6 +31,7 @@ #include "hw/dma/xlnx_csu_dma.h" #include "hw/misc/xlnx-versal-crl.h" #include "hw/misc/xlnx-versal-pmc-iou-slcr.h" +#include "hw/misc/xlnx-versal-trng.h" #include "hw/net/xlnx-versal-canfd.h" #include "hw/misc/xlnx-versal-cfu.h" #include "hw/misc/xlnx-versal-cframe-reg.h" @@ -116,6 +117,7 @@ struct Versal { } iou; =20 XlnxZynqMPRTC rtc; + XlnxVersalTRng trng; XlnxBBRam bbram; XlnxEFuse efuse; XlnxVersalEFuseCtrl efuse_ctrl; @@ -160,6 +162,7 @@ struct Versal { #define VERSAL_OSPI_IRQ 124 #define VERSAL_SD0_IRQ_0 126 #define VERSAL_EFUSE_IRQ 139 +#define VERSAL_TRNG_IRQ 141 #define VERSAL_RTC_ALARM_IRQ 142 #define VERSAL_RTC_SECONDS_IRQ 143 =20 @@ -329,4 +332,6 @@ struct Versal { #define MM_PMC_CRP_SIZE 0x10000 #define MM_PMC_RTC 0xf12a0000 #define MM_PMC_RTC_SIZE 0x10000 +#define MM_PMC_TRNG 0xf1230000 +#define MM_PMC_TRNG_SIZE 0x10000 #endif --=20 2.25.1 From nobody Sat May 18 08:35:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1695365491; cv=pass; d=zohomail.com; s=zohoarc; b=mTK0H0E3mE96i7psp5/KUXpdO1xj1PfY6c5baIJfvnep4yw0SJC/Ii2gzn29lzcnyeVIfciWa9eGqp9IjbHuei7jhUm+HMe+hs0XalwYixYtOFJZTjLzDCHqtxhVzILMzdPb/w6rOfMvNGdKBJ5wG8qCNVU+w46DP8IpTPYgB5k= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695365491; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=j9xEi5hWWY2HEoKdU8D2tPKgridiE+9m4l2BvdSJR3M=; b=U7eSoEVY5areNSZ1QZZFTvczCtj0PsH6qLO6He7/hFz+BqIGSeyfxZSAt89yJVACYuWpe5C4Tw6em8llUrZBTvNYbEEGu98DZVx0yMwx7qc0/WoUpK7/Vn+/3HrYAJoDuBNI19Qo36ZQuSPea1J3ZaINdyD3xAkuReDzGA8cvBc= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1695365491486629.0322514773484; Thu, 21 Sep 2023 23:51:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qja09-00023B-UW; Fri, 22 Sep 2023 02:50:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjZzx-00020K-NB; Fri, 22 Sep 2023 02:50:45 -0400 Received: from mail-bn8nam04on2061e.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61e] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjZzu-00047H-PG; Fri, 22 Sep 2023 02:50:45 -0400 Received: from BL1PR13CA0325.namprd13.prod.outlook.com (2603:10b6:208:2c1::30) by SA0PR12MB4415.namprd12.prod.outlook.com (2603:10b6:806:70::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.23; Fri, 22 Sep 2023 06:50:35 +0000 Received: from MN1PEPF0000ECDA.namprd02.prod.outlook.com (2603:10b6:208:2c1:cafe::4a) by BL1PR13CA0325.outlook.office365.com (2603:10b6:208:2c1::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.12 via Frontend Transport; Fri, 22 Sep 2023 06:50:34 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by MN1PEPF0000ECDA.mail.protection.outlook.com (10.167.242.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6792.20 via Frontend Transport; Fri, 22 Sep 2023 06:50:34 +0000 Received: from SATLEXMB03.amd.com (10.181.40.144) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 22 Sep 2023 01:50:25 -0500 Received: from xsjtongh40.xilinx.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server id 15.1.2507.27 via Frontend Transport; Fri, 22 Sep 2023 01:50:25 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SO8nvdFaq/cM3JvI1dKXlLbc/UvsH3AyPX5SKCn7XZxGooFP+Q5PhWCfLM6/WN7m5OJwLH1ZwlX+esfPHUg1jqs2l8ShV1ToacFKnQJcSg5waq2DS+QLWvLl3mkzzAal8NpAXgv/iRleMnaoGDoPsRD1xU9EKAKns4LI52+pfLKdBWzUgvRByx8tL/nF1MTGa8FrWqQrq1rH5ji+JSGM1e8vqTO1Kf/IRKxJeBZiBTSDBI1rEHsosK73mLX/otvDnGdDWTk5MKlQiTJRz+aYSUAl3d3lDu9AlYNnra+j/xugnIHvcdRg1M7CuDZDKsZjJuMhsU+okAeYA7O9XrJ55g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=j9xEi5hWWY2HEoKdU8D2tPKgridiE+9m4l2BvdSJR3M=; b=fOxf4BSbUKda6aUifI3uspbKfNnqd0pII0yNKSO/aPoVWwZtinAFYCbjNSZSR3qZNLxwjMhKFvZUuVJWvKAPO/Si76T6ESNbzJQ2TMTM5IGFhKhnCfrqV7lQ82dHSwkIrDqWbJzCMRpA1YYUYiHclPdpV/H4MVc+uhCjrmjUHcSPKHwafPVjtku6cxCFx9M3JUbgs9/ezciZuAKr01JkLCdTBdMAw6pvOMxiiyD3mgTVP+C7qEQURDf3vOrRbD46JVeuC2ckR6ieQTVeSKLlOWhAjgWArAczD9dM5+cyR1EFwZUmgLxWoxytxzBmQ0LNECKSfjrmQ7DN2dL6uLc2ww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nongnu.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=j9xEi5hWWY2HEoKdU8D2tPKgridiE+9m4l2BvdSJR3M=; b=J+kGfvr00XJwoNHbhuHS1PIcixZ1tyEfnUau7KMhe8irb1qW8/O7VMWNOq7I7yxEtaCWUNGhwThggx0EOVdcjZDVZHYWiUtLoJe5Lu6HdPmaMqJ4yOfAAREAA3N8r8IKQZwvGPknuesxeyYgX8Tif89781J9P5P9kuQ4gCA0jYM= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Tong Ho To: CC: , , , , , Subject: [PATCH v2 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device Date: Thu, 21 Sep 2023 23:50:10 -0700 Message-ID: <20230922065010.4071693-4-tong.ho@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230922065010.4071693-1-tong.ho@amd.com> References: <20230922065010.4071693-1-tong.ho@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000ECDA:EE_|SA0PR12MB4415:EE_ X-MS-Office365-Filtering-Correlation-Id: d293ea72-317e-4f48-2b0c-08dbbb3838cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ENdcMxzH/fKZ80wt+v1XV8SEcENWAHu9PTas2wYw2EZ5NsPYlk/xY3jLm8lfbemQJ2zHP9tGhTIQ64KRhGqsDrFAiUGYb1D1xqW5nT8qgIkgRNxya6pU9+WcfxIkwzWTqHefb7VPCXmG6jHS50GhzsUnedf0xPGoWZD4bkYwiT6DtvesHazVCCU5o84NIyfFDMWPIgmDbOY0OFE1sDqAHoCek34WEY++l8a38M/3elU/N+IadkEMcYmJr3prl+cIRl8lrQMvx/S2h7PfWxpD/Xfh8VjRCCI1KhdCC04+EG2deOmgSbbUKn2HAHwQvOMsxHS18DEI4eWQE1+J2Gl4x1jXUlK79r4l5KIi0sWYzb+Xci0j0SWAIvlIAKM1G73UA8koPBBWq9GZKhn+E1QZExFQcBVALRT9mLxzB1cWJtHgBZGmwTZNvN/yWxONOGZ5BOAND2EE7m5YKuVDKsQ4QS2nNLbPEmkk7BGsYIihrCxNeOKt0IN+LsfV12BYAfY+ltnRE+6aG4cCmcXZUO1/OnJx9rt4dAGxwtqZPwfpouhdnaqnEohj4SpmGx9Rx8cFI1WpHp4n79bcwV4IvW4SMVNqn9IgHhnDipvWyL1BpBvdIzP9wBwhOYQpertJACWu/YZN6j4BRyDbZ1JgFSPj3U4oZAxD/VLldVNkvvIGTYYA3cx7ZSO47SG7aExua2kmXjZT8m7cYbP1PAltpH5scqUpE1KxhI7YjbuD+4JnDw2aCnAEu0GY7TWNHZa7pUhc6buS8kYeiXx2Af92P2APzWAscxU564PWw/MUtEpkpwvsC7AnJVur2BRMQWisCaa4 X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB03.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230031)(4636009)(396003)(346002)(376002)(136003)(39860400002)(82310400011)(1800799009)(186009)(230921699003)(451199024)(40470700004)(46966006)(36840700001)(6666004)(478600001)(2616005)(83380400001)(26005)(1076003)(426003)(30864003)(336012)(47076005)(2906002)(44832011)(70586007)(70206006)(6916009)(54906003)(316002)(4326008)(8676002)(8936002)(5660300002)(41300700001)(40460700003)(36860700001)(86362001)(36756003)(40480700001)(356005)(81166007)(82740400003)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2023 06:50:34.8277 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d293ea72-317e-4f48-2b0c-08dbbb3838cd X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000ECDA.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4415 Received-SPF: softfail client-ip=2a01:111:f400:7e8d::61e; envelope-from=tong.ho@amd.com; helo=NAM04-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1695365492891100006 Content-Type: text/plain; charset="utf-8" Signed-off-by: Tong Ho --- tests/qtest/meson.build | 2 +- tests/qtest/xlnx-versal-trng-test.c | 490 ++++++++++++++++++++++++++++ 2 files changed, 491 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/xlnx-versal-trng-test.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 1fba07f4ed..215d20e8cf 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -216,7 +216,7 @@ qtests_aarch64 =3D \ (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG= _TPM_TIS_SYSBUS') ? \ ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) + = \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test'= , 'fuzz-xlnx-dp-test'] : []) + \ - (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] = : []) + \ + (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', = 'xlnx-versal-trng-test'] : []) + \ (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : [])= + \ (config_all.has_key('CONFIG_TCG') and = \ config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test']= : []) + \ diff --git a/tests/qtest/xlnx-versal-trng-test.c b/tests/qtest/xlnx-versal-= trng-test.c new file mode 100644 index 0000000000..6aff00c7fc --- /dev/null +++ b/tests/qtest/xlnx-versal-trng-test.c @@ -0,0 +1,490 @@ +/* + * QTests for the Xilinx Versal True Random Number Generator device + * + * Copyright (c) 2023 Advanced Micro Devices, Inc. + * + * SPDX-License-Identifier: MIT + */ + +#include "qemu/osdep.h" +#include "libqtest-single.h" + +/* Base Address */ +#define TRNG_BASEADDR (0xf1230000) + +/* TRNG_INT_CTRL */ +#define R_TRNG_INT_CTRL (0x0000) +#define TRNG_INT_CTRL_CERTF_RST_MASK (1 << 5) +#define TRNG_INT_CTRL_DTF_RST_MASK (1 << 4) +#define TRNG_INT_CTRL_DONE_RST_MASK (1 << 3) +#define TRNG_INT_CTRL_CERTF_EN_MASK (1 << 2) +#define TRNG_INT_CTRL_DTF_EN_MASK (1 << 1) +#define TRNG_INT_CTRL_DONE_EN_MASK (1) + +/* TRNG_STATUS */ +#define R_TRNG_STATUS (0x0004) +#define TRNG_STATUS_QCNT_SHIFT (9) +#define TRNG_STATUS_QCNT_MASK (7 << TRNG_STATUS_QCNT_SHIFT) +#define TRNG_STATUS_CERTF_MASK (1 << 3) +#define TRNG_STATUS_DTF_MASK (1 << 1) +#define TRNG_STATUS_DONE_MASK (1) + +/* TRNG_CTRL */ +#define R_TRNG_CTRL (0x0008) +#define TRNG_CTRL_PERSODISABLE_MASK (1 << 10) +#define TRNG_CTRL_SINGLEGENMODE_MASK (1 << 9) +#define TRNG_CTRL_PRNGMODE_MASK (1 << 7) +#define TRNG_CTRL_TSTMODE_MASK (1 << 6) +#define TRNG_CTRL_PRNGSTART_MASK (1 << 5) +#define TRNG_CTRL_PRNGXS_MASK (1 << 3) +#define TRNG_CTRL_TRSSEN_MASK (1 << 2) +#define TRNG_CTRL_QERTUEN_MASK (1 << 1) +#define TRNG_CTRL_PRNGSRST_MASK (1) + +/* TRNG_EXT_SEED_0 ... _11 */ +#define R_TRNG_EXT_SEED_0 (0x0040) +#define R_TRNG_EXT_SEED_11 (R_TRNG_EXT_SEED_0 + 4 * 11) + +/* TRNG_PER_STRNG_0 ... 11 */ +#define R_TRNG_PER_STRNG_0 (0x0080) +#define R_TRNG_PER_STRNG_11 (R_TRNG_PER_STRNG_0 + 4 * 11) + +/* TRNG_CORE_OUTPUT */ +#define R_TRNG_CORE_OUTPUT (0x00c0) + +/* TRNG_RESET */ +#define R_TRNG_RESET (0x00d0) +#define TRNG_RESET_VAL_MASK (1) + +/* TRNG_OSC_EN */ +#define R_TRNG_OSC_EN (0x00d4) +#define TRNG_OSC_EN_VAL_MASK (1) + +/* TRNG_TRNG_ISR, _IMR, _IER, _IDR */ +#define R_TRNG_ISR (0x00e0) +#define R_TRNG_IMR (0x00e4) +#define R_TRNG_IER (0x00e8) +#define R_TRNG_IDR (0x00ec) +#define TRNG_IRQ_SLVERR_MASK (1 << 1) +#define TRNG_IRQ_CORE_INT_MASK (1) + +#define FAILED(FMT, ...) g_error("%s(): " FMT, __func__, ## __VA_ARGS__) + +static const uint32_t prng_seed[12] =3D { + 0x01234567, 0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc, + 0x76543210, 0x87654321, 0x98765432, 0xa9876543, 0xba987654, 0xfedcba98, +}; + +static const uint32_t pers_str[12] =3D { + 0x76543210, 0x87654321, 0x98765432, 0xa9876543, 0xba987654, 0xfedcba98, + 0x01234567, 0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc, +}; + +static void trng_test_start(void) +{ + qtest_start("-machine xlnx-versal-virt"); +} + +static void trng_test_stop(void) +{ + qtest_end(); +} + +static void trng_test_set_uint_prop(const char *name, uint64_t value) +{ + const char *path =3D "/machine/xlnx-versal/trng"; + QDict *response; + + response =3D qmp("{ 'execute': 'qom-set'," + " 'arguments': {" + " 'path': %s," + " 'property': %s," + " 'value': %llu" + "} }", path, + name, (unsigned long long)value); + g_assert(qdict_haskey(response, "return")); + qobject_unref(response); +} + +static void trng_write(unsigned ra, uint32_t val) +{ + writel(TRNG_BASEADDR + ra, val); +} + +static uint32_t trng_read(unsigned ra) +{ + return readl(TRNG_BASEADDR + ra); +} + +static void trng_bit_set(unsigned ra, uint32_t bits) +{ + trng_write(ra, (trng_read(ra) | bits)); +} + +static void trng_bit_clr(unsigned ra, uint32_t bits) +{ + trng_write(ra, (trng_read(ra) & ~bits)); +} + +static void trng_ctrl_set(uint32_t bits) +{ + trng_bit_set(R_TRNG_CTRL, bits); +} + +static void trng_ctrl_clr(uint32_t bits) +{ + trng_bit_clr(R_TRNG_CTRL, bits); +} + +static uint32_t trng_status(void) +{ + return trng_read(R_TRNG_STATUS); +} + +static unsigned trng_qcnt(void) +{ + uint32_t sta =3D trng_status(); + + return (sta & TRNG_STATUS_QCNT_MASK) >> TRNG_STATUS_QCNT_SHIFT; +} + +static const char *trng_info(void) +{ + uint32_t sta =3D trng_status(); + uint32_t ctl =3D trng_read(R_TRNG_CTRL); + + static char info[64]; + + snprintf(info, sizeof(info), "; status=3D0x%x, ctrl=3D0x%x", sta, ctl); + return info; +} + +static void trng_wait(uint32_t wait_mask, bool on, const char *act) +{ + time_t tmo =3D time(NULL) + 2; /* at most 2 seconds */ + uint32_t event_mask =3D 0; + uint32_t clear_mask =3D 0; + + /* + * Only selected bits are events in R_TRNG_STATUS, and + * clear them needs to go through R_INT_CTRL. + */ + if (wait_mask & TRNG_STATUS_CERTF_MASK) { + event_mask |=3D TRNG_STATUS_CERTF_MASK; + clear_mask |=3D TRNG_INT_CTRL_CERTF_RST_MASK; + } + if (wait_mask & TRNG_STATUS_DTF_MASK) { + event_mask |=3D TRNG_STATUS_DTF_MASK; + clear_mask |=3D TRNG_INT_CTRL_DTF_RST_MASK; + } + if (wait_mask & TRNG_STATUS_DONE_MASK) { + event_mask |=3D TRNG_STATUS_DONE_MASK; + clear_mask |=3D TRNG_INT_CTRL_DONE_RST_MASK; + } + + for (;;) { + bool sta =3D !!(trng_status() & event_mask); + + if ((on ^ sta) =3D=3D 0) { + break; + } + + if (time(NULL) >=3D tmo) { + FAILED("%s: Timed out waiting for event 0x%x to be %d%s", + act, event_mask, (int)on, trng_info()); + } + + g_usleep(10000); + } + + /* Remove event */ + trng_bit_set(R_TRNG_INT_CTRL, clear_mask); + + if (!!(trng_read(R_TRNG_STATUS) & event_mask)) { + FAILED("%s: Event 0x%0x stuck at 1 after clear: %s", + act, event_mask, trng_info()); + } +} + +static void trng_wait_done(const char *act) +{ + trng_wait(TRNG_STATUS_DONE_MASK, true, act); +} + +static void trng_wait_dtf(void) +{ + trng_wait(TRNG_STATUS_DTF_MASK, true, "DTF injection"); +} + +static void trng_wait_certf(void) +{ + trng_wait(TRNG_STATUS_CERTF_MASK, true, "CERTF injection"); +} + +static void trng_reset(void) +{ + trng_write(R_TRNG_RESET, TRNG_RESET_VAL_MASK); + trng_write(R_TRNG_RESET, 0); +} + +static void trng_load(unsigned r0, const uint32_t *b384) +{ + static const uint32_t zero[12] =3D { 0 }; + unsigned k; + + if (!b384) { + b384 =3D zero; + } + + for (k =3D 0; k < 12; k++) { + trng_write(r0 + 4 * k, b384[k]); + } +} + +static void trng_reseed(const uint32_t *seed) +{ + const char *act; + uint32_t ctl; + + ctl =3D TRNG_CTRL_PRNGSTART_MASK | + TRNG_CTRL_PRNGXS_MASK | + TRNG_CTRL_TRSSEN_MASK; + + trng_ctrl_clr(ctl | TRNG_CTRL_PRNGMODE_MASK); + + if (seed) { + trng_load(R_TRNG_EXT_SEED_0, seed); + act =3D "Reseed PRNG"; + ctl &=3D ~TRNG_CTRL_TRSSEN_MASK; + } else { + trng_write(R_TRNG_OSC_EN, TRNG_OSC_EN_VAL_MASK); + act =3D "Reseed TRNG"; + ctl &=3D ~TRNG_CTRL_PRNGXS_MASK; + } + + trng_ctrl_set(ctl); + trng_wait_done(act); + trng_ctrl_clr(TRNG_CTRL_PRNGSTART_MASK); +} + +static void trng_generate(bool auto_enb) +{ + uint32_t ctl; + + ctl =3D TRNG_CTRL_PRNGSTART_MASK | TRNG_CTRL_SINGLEGENMODE_MASK; + trng_ctrl_clr(ctl); + + if (auto_enb) { + ctl &=3D ~TRNG_CTRL_SINGLEGENMODE_MASK; + } + + trng_ctrl_set(ctl | TRNG_CTRL_PRNGMODE_MASK); + + trng_wait_done("Generate"); + g_assert(trng_qcnt() !=3D 7); +} + +static size_t trng_collect(uint32_t *rnd, size_t cnt) +{ + size_t i; + + for (i =3D 0; i < cnt; i++) { + if (trng_qcnt() =3D=3D 0) { + return i; + } + + rnd[i] =3D trng_read(R_TRNG_CORE_OUTPUT); + } + + return i; +} + +static void trng_test_autogen(void) +{ + const size_t cnt =3D 512 / 32; + uint32_t rng[cnt], prng[cnt]; + size_t n; + + trng_reset(); + + /* PRNG run #1 */ + trng_reseed(prng_seed); + trng_generate(true); + + n =3D trng_collect(prng, cnt); + if (n !=3D cnt) { + FAILED("PRNG_1 Auto-gen test failed: expected =3D %u, got =3D %u", + (unsigned)cnt, (unsigned)n); + } + + /* TRNG, should not match PRNG */ + trng_reseed(NULL); + trng_generate(true); + + n =3D trng_collect(rng, cnt); + if (n !=3D cnt) { + FAILED("TRNG Auto-gen test failed: expected =3D %u, got =3D %u", + (unsigned)cnt, (unsigned)n); + } + + if (!memcmp(rng, prng, sizeof(rng))) { + FAILED("TRNG test failed: matching PRNG"); + } + + /* PRNG #2: should matches run #1 */ + trng_reseed(prng_seed); + trng_generate(true); + + n =3D trng_collect(rng, cnt); + if (n !=3D cnt) { + FAILED("PRNG_2 Auto-gen test failed: expected =3D %u, got =3D %u", + (unsigned)cnt, (unsigned)n); + } + + if (memcmp(rng, prng, sizeof(rng))) { + FAILED("PRNG_2 Auto-gen test failed: does not match PRNG_1"); + } +} + +static void trng_test_oneshot(void) +{ + const size_t cnt =3D 512 / 32; + uint32_t rng[cnt]; + size_t n; + + trng_reset(); + + /* PRNG run #1 */ + trng_reseed(prng_seed); + trng_generate(false); + + n =3D trng_collect(rng, cnt); + if (n =3D=3D cnt) { + FAILED("PRNG_1 One-shot gen test failed"); + } + + /* TRNG, should not match PRNG */ + trng_reseed(NULL); + trng_generate(false); + + n =3D trng_collect(rng, cnt); + if (n =3D=3D cnt) { + FAILED("TRNG One-shot test failed"); + } +} + +static void trng_test_per_str(void) +{ + const size_t cnt =3D 512 / 32; + uint32_t rng[cnt], prng[cnt]; + size_t n; + + trng_reset(); + + /* #1: disabled */ + trng_ctrl_set(TRNG_CTRL_PERSODISABLE_MASK); + trng_reseed(prng_seed); + trng_ctrl_clr(TRNG_CTRL_PERSODISABLE_MASK); + + trng_generate(true); + n =3D trng_collect(prng, cnt); + g_assert_cmpuint(n, =3D=3D, cnt); + + /* #2: zero string should match personalization disabled */ + trng_load(R_TRNG_PER_STRNG_0, NULL); + trng_reseed(prng_seed); + + trng_generate(true); + n =3D trng_collect(rng, cnt); + g_assert_cmpuint(n, =3D=3D, cnt); + + if (memcmp(rng, prng, sizeof(rng))) { + FAILED("Failed: PER_DISABLE !=3D PER_STRNG_ALL_ZERO"); + } + + /* #3: non-zero string should not match personalization disabled */ + trng_load(R_TRNG_PER_STRNG_0, pers_str); + trng_reseed(prng_seed); + + trng_generate(true); + n =3D trng_collect(rng, cnt); + g_assert_cmpuint(n, =3D=3D, cnt); + + if (!memcmp(rng, prng, sizeof(rng))) { + FAILED("Failed: PER_DISABLE =3D=3D PER_STRNG_NON_ZERO"); + } +} + +static void trng_test_forced_prng(void) +{ + const char *prop =3D "forced-prng"; + const uint64_t seed =3D 0xdeadbeefbad1bad0ULL; + + trng_reset(); + const size_t cnt =3D 512 / 32; + uint32_t rng[cnt], prng[cnt]; + size_t n; + + trng_test_set_uint_prop(prop, seed); + + /* TRNG run #1 */ + trng_reset(); + trng_reseed(NULL); + trng_generate(true); + + n =3D trng_collect(prng, cnt); + g_assert_cmpuint(n, =3D=3D, cnt); + + /* TRNG run #2 should match run #1 */ + trng_reset(); + trng_reseed(NULL); + trng_generate(true); + + n =3D trng_collect(rng, cnt); + g_assert_cmpuint(n, =3D=3D, cnt); + + if (memcmp(rng, prng, sizeof(rng))) { + FAILED("Forced-prng test failed: results do not match"); + } +} + +static void trng_test_fault_events(void) +{ + const char *prop =3D "fips-fault-events"; + + trng_reset(); + + /* Fault events only when TRSS is enabled */ + trng_write(R_TRNG_OSC_EN, TRNG_OSC_EN_VAL_MASK); + trng_ctrl_set(TRNG_CTRL_TRSSEN_MASK); + + trng_test_set_uint_prop(prop, TRNG_STATUS_CERTF_MASK); + trng_wait_certf(); + + trng_test_set_uint_prop(prop, TRNG_STATUS_DTF_MASK); + trng_wait_dtf(); + + trng_reset(); +} + +int main(int argc, char **argv) +{ + int rc; + + g_test_init(&argc, &argv, NULL); + + #define TRNG_TEST_ADD(n) \ + qtest_add_func("/hw/misc/xlnx-versal-trng/" #n, trng_test_ ## = n); + TRNG_TEST_ADD(autogen); + TRNG_TEST_ADD(oneshot); + TRNG_TEST_ADD(per_str); + TRNG_TEST_ADD(forced_prng); + TRNG_TEST_ADD(fault_events); + #undef TRNG_TEST_ADD + + trng_test_start(); + rc =3D g_test_run(); + trng_test_stop(); + + return rc; +} --=20 2.25.1