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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F269AC433EF for ; Tue, 26 Oct 2021 06:58:51 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 55D8960EC0 for ; Tue, 26 Oct 2021 06:58:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 55D8960EC0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nxp.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A7221835CB; Tue, 26 Oct 2021 08:58:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=nxp.com header.i=@nxp.com header.b="PHbwp+Jk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C7E4283603; Tue, 26 Oct 2021 08:58:02 +0200 (CEST) Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on0614.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0e::614]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E5448835A3 for ; Tue, 26 Oct 2021 08:57:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=gaurav.jain@nxp.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mKjcWvs0Ba2S1jBtYVXyMwooR7v02MTd9jLaHqbNS8QQG4kEMpRuyaBUMvDcP5C2sXmSgeH+7fFRbWkpU8xrzzuKlP0DSAQ/1znaUaqXFFn2EEyI5umMHH0o6JXaxqRgVAXlCUmlXAlmahOPzTlLMbmBwt0HiDuNlxX+VnNv6WDgsuDOfgxAeeL25JYsSmtyn8qYl9g+AdmTEw73oOxcH8c/YZeuNYZ8s8ky5oltQNDMh7qI3IgSudaA6JxOry+TAYAkD7/GYKjwdM8b8deJ/sodYkeI+CzqkVmA/zsa3NF45TBQQ68H3xm4780oF+yeMWBfS+xtkHD71p5DVMuKeA== 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=+V8Hi/ppl5eA0ieQX1uAVWNL6E8rMmohtPzX/8KtvTc=; b=SIctb8okWbnsweFgp4A9d2ooQRlBnU/3qesydZHGTyi2UBu3MzRt8guc2CBsxaT24hHp/rzRAJX9pUzU31UFswDrbJyA6I6TiN04yADY3lpxSIIi5Fiuv9LqsF26ViM04Lc6CV2N9HL3g6+IfjNkVwsVnCMwj1SwXlJctAgwkbr+UsImr2XirMpFhhxtlVgQ7ji7Q29EyFIKiK3f6fKOBkAtA46ZP5RIcmJqb0LZ5xRencWxs/ne4bN07M3j/BmsjWGoOcFDM6V+2xpYm6wCmMZG8kZwk+CD3TlEOdEFZm8uRiWj0bXpGTDUmvUbIvr1q266oCKXZRYR9JVvpi4Vlw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+V8Hi/ppl5eA0ieQX1uAVWNL6E8rMmohtPzX/8KtvTc=; b=PHbwp+JkfINJQ/PrAWhhkNJc69blEXdguidLl+bWT1s21SdViq7PH87l2vrANLpU+WQMPiKqSmnnERNUpc1vkvJTLXNP1bmIcX51P4Iu65Ko+TQ1zebaGxwDSgk3BsIbagBgxE6KkZbtXZKohpJ5Q20GqkK898Ovr7Dr491sNIk= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from VI1PR04MB5342.eurprd04.prod.outlook.com (2603:10a6:803:46::16) by VI1PR04MB5135.eurprd04.prod.outlook.com (2603:10a6:803:62::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.18; Tue, 26 Oct 2021 06:57:52 +0000 Received: from VI1PR04MB5342.eurprd04.prod.outlook.com ([fe80::7daf:2591:dd7b:b608]) by VI1PR04MB5342.eurprd04.prod.outlook.com ([fe80::7daf:2591:dd7b:b608%7]) with mapi id 15.20.4628.020; Tue, 26 Oct 2021 06:57:52 +0000 From: Gaurav Jain To: u-boot@lists.denx.de Cc: Stefano Babic , Fabio Estevam , Peng Fan , Simon Glass , Priyanka Jain , Ye Li , Horia Geanta , Ji Luo , Franck Lenormand , Silvano Di Ninno , Sahil malhotra , Pankaj Gupta , Varun Sethi , "NXP i . MX U-Boot Team" , Shengzhou Liu , Mingkai Hu , Rajesh Bhagat , Meenakshi Aggarwal , Wasim Khan , Alison Wang , Pramod Kumar , Tang Yuantian , Adrian Alonso , Vladimir Oltean Subject: [PATCH v4 11/16] crypto/fsl: Fix kick_trng Date: Tue, 26 Oct 2021 12:25:49 +0530 Message-Id: <20211026065554.29009-12-gaurav.jain@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211026065554.29009-1-gaurav.jain@nxp.com> References: <20211026065554.29009-1-gaurav.jain@nxp.com> Content-Type: text/plain X-ClientProxiedBy: SGBP274CA0022.SGPP274.PROD.OUTLOOK.COM (2603:1096:4:b0::34) To VI1PR04MB5342.eurprd04.prod.outlook.com (2603:10a6:803:46::16) MIME-Version: 1.0 Received: from lsv03326.swis.in-blr01.nxp.com (14.142.151.118) by SGBP274CA0022.SGPP274.PROD.OUTLOOK.COM (2603:1096:4:b0::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.20 via Frontend Transport; Tue, 26 Oct 2021 06:57:46 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2ab68dbc-e935-407e-7d10-08d9984dedff X-MS-TrafficTypeDiagnostic: VI1PR04MB5135: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: N3hx5Of7SL/aBs7qVgIM+nX0dKKDXLXPQjo9ppHQXs1zhqnwoaXggOVnbMSVVEem5HB77NP+I+lpAMkFk2DlHsMGg7qY/vEXHYEDcKuMHW0KwFxfzqs93Viv8OeTmHqd9W1Rx7RJ43RtdcwHA4RE2YI4/3y30x0YmPqgDsNl7M1t//uW0LH9ew9a2948W4Cz+a2Zt3BPrFiB3jrFK4D1VY8ZFsqWpogO0AekoCtTg1DdcgQwYgdenWjFhFrYw5N/cCFwrXtn/MI7gcU5ya+6QpWfrTnzRpRIfc+oRMchQb5D+VGjOovDwDbCHCxRJTc8xekLiyR26EhJ0t5xOg+Tybfi4Iifx7b1VMkq1C1c0l/hv7eORQ+mqZIuWcElFxtqmkZoDy9Ky6Jm/ttwyHJaJ74RfRovthJ4c17KS9eet4QHAMpMNX/YHWB0T2Qx1/4cbpphb7CnIhk5q0jmPkQr+T2wwdveUOS4nxruHYZ39GQ5akC5xV7X6HkZHPdDcDzdJ0zFT3ztpnK/vGj8gqbM81ehHVpFPX/m/vOl6AsSL5ysjUQXYKkX33q8g+EpximoYafWuUECRUbIHPSV+ECy760Nq1pFhqtRbZOtUwotuxOLwiDo9ShJOfv9SSghgB4iRZBHvaNZU2rYD6oYxvkrXiaMdwBR4+TCAXYa27AmOIUANvpunFKCcbDd8XR4n1yLVRWbGC3+g+O0QVuRfqlyAS4H+Wtcu7ICmZXQByQTHUj9JC7e+0pi0q7Sw/ScTGyD X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR04MB5342.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(6486002)(956004)(54906003)(1076003)(508600001)(66556008)(316002)(66476007)(66946007)(83380400001)(2906002)(52116002)(26005)(36756003)(7696005)(38100700002)(186003)(1006002)(8936002)(38350700002)(5660300002)(4326008)(6916009)(55236004)(2616005)(86362001)(44832011)(8676002)(110426009); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?XJUy4m8GTNcVdbg0W3vQSU73Kccqe9KuwuzhMtpK5yLumMZBmWxmpqbSp2Mh?= =?us-ascii?Q?0YijxHTW+jamBRbOzE5oPgpijzYWDe6e65VhQ7FMJyIr9cjKRw/HJMzgheF6?= =?us-ascii?Q?h5JaJDbDlQQ8t+vSYBFmO3qK0Zp2IZI7DVoUSVs8rZWgrwV8j1d4ne6KSO/3?= =?us-ascii?Q?C+MD1KqGcilN0jJ/HddHMuQkn464B6GYAKrS+yrTKtN/t+mG/0b/AoEVRHgl?= =?us-ascii?Q?Fyjn7VlwdAkoEwfci4GevaqaSe1f27QbW+aZ8h/o33f4MeGd2qZAMqsRm7Mj?= =?us-ascii?Q?xWBm4Iyfq7eJJ79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qld6?= =?us-ascii?Q?ssrV+OTVzZ7nGlCVezrFfw+x6+6U/J2LzODexfrgb/qssDYnwWMxYTyMFHjp?= =?us-ascii?Q?h6D/I0bK8+Ne+e0dKQN0wglQhBPjtSnq5ngejdof57HW5ATZEybedBriseOG?= =?us-ascii?Q?ql9DSq61utvAQB5nTf8aSxuXfLl3w7ywnm4qDgb2l0mxNOpL4ThzusST3vbR?= =?us-ascii?Q?BBwGDkf69RIHRNvgPmDxvLVPZnZQY/1OVyPi1hIIVSazU/TJkLyh6MBkOBw2?= =?us-ascii?Q?S82Gg4XBKwh24BzMqvor63DjrVp3v9sImoVya1v7xsQtoXxW0rgQQnnLoMvA?= =?us-ascii?Q?mLdEIxvPfd0cdN6BWisZkIHxDUftXuX0jaX5RCwn54WRhMskwZV0QvWw3RDk?= =?us-ascii?Q?kj32OsEdKpxlUyFHuybkJQF94ZiitO+gqx5zIn4UMCEtLdE4rDrge/esFHH6?= =?us-ascii?Q?g0noO1VH+hsKBspHFjHM6K0=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2ab68dbc-e935-407e-7d10-08d9984dedff X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5342.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Oct 2021 06:57:52.4562 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 0tuh/ZR8pLYaJ2A5UhoGF3kl5DoIghDvvnAnmpjhWsAosG5y2Z1jfTX4JSzxfUrHy7ofBmhEAY2zSo+672zvlQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB5135 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Ye Li fix hwrng performance issue in kernel. Signed-off-by: Ye Li Acked-by: Gaurav Jain > --- drivers/crypto/fsl/jr.c | 109 ++++++++++++++++++++++++++++++++++------ include/fsl_sec.h | 1 + 2 files changed, 94 insertions(+), 16 deletions(-) diff --git a/drivers/crypto/fsl/jr.c b/drivers/crypto/fsl/jr.c index 0bfdf9070f..72f09a1d8b 100644 --- a/drivers/crypto/fsl/jr.c +++ b/drivers/crypto/fsl/jr.c @@ -602,30 +602,107 @@ static u8 get_rng_vid(ccsr_sec_t *sec) */ static void kick_trng(int ent_delay, ccsr_sec_t *sec) { + u32 samples = 512; /* number of bits to generate and test */ + u32 mono_min = 195; + u32 mono_max = 317; + u32 mono_range = mono_max - mono_min; + u32 poker_min = 1031; + u32 poker_max = 1600; + u32 poker_range = poker_max - poker_min + 1; + u32 retries = 2; + u32 lrun_max = 32; + s32 run_1_min = 27; + s32 run_1_max = 107; + s32 run_1_range = run_1_max - run_1_min; + s32 run_2_min = 7; + s32 run_2_max = 62; + s32 run_2_range = run_2_max - run_2_min; + s32 run_3_min = 0; + s32 run_3_max = 39; + s32 run_3_range = run_3_max - run_3_min; + s32 run_4_min = -1; + s32 run_4_max = 26; + s32 run_4_range = run_4_max - run_4_min; + s32 run_5_min = -1; + s32 run_5_max = 18; + s32 run_5_range = run_5_max - run_5_min; + s32 run_6_min = -1; + s32 run_6_max = 17; + s32 run_6_range = run_6_max - run_6_min; + u32 val; + struct rng4tst __iomem *rng = (struct rng4tst __iomem *)&sec->rng; - u32 val; - /* put RNG4 into program mode */ - sec_setbits32(&rng->rtmctl, RTMCTL_PRGM); - /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the - * length (in system clocks) of each Entropy sample taken - * */ + /* Put RNG in program mode */ + /* Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to + * properly invalidate the entropy in the entropy register and + * force re-generation. + */ + sec_setbits32(&rng->rtmctl, RTMCTL_PRGM | RTMCTL_ACC); + + /* Configure the RNG Entropy Delay + * Performance-wise, it does not make sense to + * set the delay to a value that is lower + * than the last one that worked (i.e. the state handles + * were instantiated properly. Thus, instead of wasting + * time trying to set the values controlling the sample + * frequency, the function simply returns. + */ val = sec_in32(&rng->rtsdctl); - val = (val & ~RTSDCTL_ENT_DLY_MASK) | - (ent_delay << RTSDCTL_ENT_DLY_SHIFT); + val &= RTSDCTL_ENT_DLY_MASK; + val >>= RTSDCTL_ENT_DLY_SHIFT; + if (ent_delay < val) { + /* Put RNG4 into run mode */ + sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM | RTMCTL_ACC); + return; + } + + val = (ent_delay << RTSDCTL_ENT_DLY_SHIFT) | samples; sec_out32(&rng->rtsdctl, val); - /* min. freq. count, equal to 1/4 of the entropy sample length */ - sec_out32(&rng->rtfreqmin, ent_delay >> 2); - /* disable maximum frequency count */ - sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE); + /* - * select raw sampling in both entropy shifter + * Recommended margins (min,max) for freq. count: + * freq_mul = RO_freq / TRNG_clk_freq + * rtfrqmin = (ent_delay x freq_mul) >> 1; + * rtfrqmax = (ent_delay x freq_mul) << 3; + * Given current deployments of CAAM in i.MX SoCs, and to simplify + * the configuration, we consider [1,16] to be a safe interval + * for the freq_mul and the limits of the interval are used to compute + * rtfrqmin, rtfrqmax + */ + sec_out32(&rng->rtfreqmin, ent_delay >> 1); + sec_out32(&rng->rtfreqmax, ent_delay << 7); + + sec_out32(&rng->rtscmisc, (retries << 16) | lrun_max); + sec_out32(&rng->rtpkrmax, poker_max); + sec_out32(&rng->rtpkrrng, poker_range); + sec_out32(&rng->rsvd1[0], (mono_range << 16) | mono_max); + sec_out32(&rng->rsvd1[1], (run_1_range << 16) | run_1_max); + sec_out32(&rng->rsvd1[2], (run_2_range << 16) | run_2_max); + sec_out32(&rng->rsvd1[3], (run_3_range << 16) | run_3_max); + sec_out32(&rng->rsvd1[4], (run_4_range << 16) | run_4_max); + sec_out32(&rng->rsvd1[5], (run_5_range << 16) | run_5_max); + sec_out32(&rng->rsvd1[6], (run_6_range << 16) | run_6_max); + + val = sec_in32(&rng->rtmctl); + /* + * Select raw sampling in both entropy shifter * and statistical checker */ - sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC); - /* put RNG4 into run mode */ - sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM); + val &= ~RTMCTL_SAMP_MODE_INVALID; + val |= RTMCTL_SAMP_MODE_RAW_ES_SC; + /* Put RNG4 into run mode */ + val &= ~(RTMCTL_PRGM | RTMCTL_ACC); + /*test with sample mode only */ + sec_out32(&rng->rtmctl, val); + + /* Clear the ERR bit in RTMCTL if set. The TRNG error can occur when the + * RNG clock is not within 1/2x to 8x the system clock. + * This error is possible if ROM code does not initialize the system PLLs + * immediately after PoR. + */ + /* setbits_le32(CAAM_RTMCTL, RTMCTL_ERR); */ } static int rng_init(uint8_t sec_idx, ccsr_sec_t *sec) diff --git a/include/fsl_sec.h b/include/fsl_sec.h index 7b6e3e2c20..2b3239414a 100644 --- a/include/fsl_sec.h +++ b/include/fsl_sec.h @@ -34,6 +34,7 @@ #if CONFIG_SYS_FSL_SEC_COMPAT >= 4 /* RNG4 TRNG test registers */ struct rng4tst { +#define RTMCTL_ACC 0x20 #define RTMCTL_PRGM 0x00010000 /* 1 -> program mode, 0 -> run mode */ #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0 /* use von Neumann data in both entropy shifter and -- 2.17.1