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 AE2D1C433F5 for ; Tue, 5 Oct 2021 05:42:37 +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 15615611C1 for ; Tue, 5 Oct 2021 05:42:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 15615611C1 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 EEBBF83155; Tue, 5 Oct 2021 07:41: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="QeuB5+Rj"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 820FE82054; Tue, 5 Oct 2021 07:41:38 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on060f.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0c::60f]) (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 10CDF82054 for ; Tue, 5 Oct 2021 07:41:35 +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=CQ6Hto85BeoZHJzmL93w35clP+y3lYfYwohwNWuxahdFrhd7eNAx108GBODLEI5ra/NebKKci/CntsdR6TVqrtfnqPrz2D9aRV9k9Fz8xtlBbN61bOK2Ym2Sqn5HoCB9y8Q0Wi6Kp9AEFGKHjyYdv98WQcyHhyq5P09WkjynxeM3z96tdfMvsEeKNkc5L1QWnJohwCPszmhwS4AstbAdU6aIXi1OBFLjipM9v+htgkaF2WDQo6mz598jfRyEEcgeb2qXWiDARiQ0qHmEmk3COuQ3F7Mf6vicm/ScM+UO993IkVkawwdFAlNX60k/Z7w7TuZ0QAybLwLm6idRRxTZWA== 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=ll7ZkhG2f8d2xOPx4x303B8V5rNcszsXr6KGD2Mr+IyZ2RPzL6hOg3WsDUmRNlfrpRvEZKK9w7VaCysv7B68817JC6n7Ba/dGP/irjZzFavFyoprFeWjk8mDLhsOIK9QUD5PpP3TD2JQ/WVXvoXVsDuY3FU+b0gyJDRwbsC/NkPM1nJgSIeQnzW8ZZUZB99OeLUAXYFtydGf0VyHWU2uPepLX74sNlY1MPGWlxe5GIvIhC2E7QzzmUVeRJu8gkAxQePcWcvReGLfp3i78ooAlg30P1tfEe1DwkBckVhxhZ5oa5IbrQRR2sjJG57ThDoPtHbF5k1rHIbSG8hd9zPJ0A== 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=QeuB5+Rj4GlF5RhcGUkKpaUivvDWXO8scmX4GL/7ZB1Keq5R3pN9NfxliPMWe/IfEWMVl/C3h7hEBLUkHoGVLODf6pAtQyFkTTuc1QdJP34VeRi4k0GE5BG9hMs7FUZd2g4+UnL+vj9GIwmz1rcY+NNchp34F1N7kmYUnU4792I= Authentication-Results: lists.denx.de; dkim=none (message not signed) header.d=none;lists.denx.de; dmarc=none action=none header.from=nxp.com; Received: from VI1PR04MB5342.eurprd04.prod.outlook.com (2603:10a6:803:46::16) by VE1PR04MB7470.eurprd04.prod.outlook.com (2603:10a6:800:1a3::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.19; Tue, 5 Oct 2021 05:41:34 +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.4566.022; Tue, 5 Oct 2021 05:41:34 +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 v3 11/16] crypto/fsl: Fix kick_trng Date: Tue, 5 Oct 2021 11:09:24 +0530 Message-Id: <20211005053929.9454-12-gaurav.jain@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211005053929.9454-1-gaurav.jain@nxp.com> References: <20211005053929.9454-1-gaurav.jain@nxp.com> Content-Type: text/plain X-ClientProxiedBy: SG2P153CA0023.APCP153.PROD.OUTLOOK.COM (2603:1096:4:c7::10) 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 SG2P153CA0023.APCP153.PROD.OUTLOOK.COM (2603:1096:4:c7::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.3 via Frontend Transport; Tue, 5 Oct 2021 05:41:27 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ca5287fa-cd37-426f-9897-08d987c2ca4f X-MS-TrafficTypeDiagnostic: VE1PR04MB7470: X-MS-Exchange-Transport-Forked: True 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: cq1zEHfmn1iFAg86fu8IfnarehH0ccOCKTQARnizrqwDssnaNdqnBgotcbJ9AjjvVSOfOJTRi1vZLIc3VL0qi1u7+3niQ6Un3kwBZR8m3kyTahr8NetlJ+yjb0NNRqWFqYHDJABy6glyqBqF+KnwyxlCKSWRR5YYj6B+wkKR/ZTuPgHh7X5E4/zUyxxPeNdKbw1Yup+Ob7sfYk1zs7lBnlE5vbA6nhqO4fi8RAyHTFDMeuD2BoZeXI2KjnLzT0YTPS8l1AQTo3pYzS2NwkFUg4UNI7F5i/qgF/thzhR9sh3Mem9vPocmnYvYiTXnmJLl30fbno4Qiu3NX++J8iyxVKv4eZZTKwZeKj58SrTqefS+f8rK/Z+G/oOgKzDcz/bZla7SxKKCHsXv6GJg2wMNTnO3fHIQOKvyHIseHWMjZu8rzd6UmlWMKbkWuJJfjkq2CPlejHoNZOs/XiKVKFzi/tLFxfb7GKINSoBjzr2/1SQybUGTb8w0IV5zJPek5SeRfjMquFfeXfdyuGKQJTzlsIAzUGmSgtvZJKHMrXl3H6pzCQKNaIguimAjgePS6XlneygURYJ78tU477vqA4Vxg6LS3yrwX9jQ6IHPuFyLkQ2z9iDfi5lMGlhbr3Np6y30+WUw560FnaJf7q8617BRbRWkuOZFZT21ldDULvIpk6KKW52OejOy+e+U9EJffJf2f8wjvLy7ngqxFWd5pHwihYO3LcDrNdQYQWmIc7CoQno= 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)(54906003)(1006002)(66476007)(316002)(6486002)(4326008)(44832011)(1076003)(38100700002)(508600001)(66556008)(66946007)(38350700002)(6666004)(5660300002)(8936002)(186003)(86362001)(7696005)(956004)(2616005)(26005)(83380400001)(36756003)(55236004)(8676002)(6916009)(52116002)(2906002)(110426009); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?3WmCb1SKl+w9jeb6hgcAxDwA4VvkiJzioQbOo2kC7otqkBX384Gb16hfqC5X?= =?us-ascii?Q?9+GjklbN/18ZUPFL3ccKbGZB02dJcVj4216hAfi+RDynGykHxi9aorPb+40U?= =?us-ascii?Q?UB3BdNNeMNMX8Vf79zdYL9sTLUbA9oUZX5xlpoF85QDWH08EDjgSm7ni0YkC?= =?us-ascii?Q?KOdjYCPikCCoWjeT5k2WUy/XeqRKqFCWVdHHxL7cYj6qBEq/+0yPHCUHZ2U+?= =?us-ascii?Q?TsrGo3iDBsJM7NQndv6vLeuQG4cwVSdRq3KLQ/wiRcKDeHWZCTzAwMKoGP04?= =?us-ascii?Q?gutBUnwgXEqfknmnfJ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q?Ux8x5yjjJXPBSZYj6lZQPsf3VmRVvZ6dFTQM24ya8E2TIN1rHTyhDWAUg0cA?= =?us-ascii?Q?1xvCmwrkSQxxBKT+HlzUmEvE6bkaIa2CKnVoZDdmhtBU69nwlGII3zMf/USy?= =?us-ascii?Q?88sZ2S9e44rhUIciZ7AbzPpZ?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ca5287fa-cd37-426f-9897-08d987c2ca4f X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5342.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Oct 2021 05:41:33.9443 (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: PrSJmkg55mpxHH+TzrlJagdKaxFfyinNa/3+qg9F0Waat7TO6An+94PGg29nC1Wt0AnoMhNUB8T8MQU2WuNKEA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1PR04MB7470 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