Skip to content

Commit 1abc896

Browse files
Meenakshi88herbertx
authored andcommitted
crypto: caam - optimize RNG sample size
TRNG "sample size" (the total number of entropy samples that will be taken during entropy generation) default / POR value is very conservatively set to 2500. Let's set it to 512, the same as the caam driver in U-boot (drivers/crypto/fsl_caam.c) does. This solves the issue of RNG performance dropping after a suspend/resume cycle on parts where caam loses power, since the initial U-boot setttings are lost and kernel does not restore them when resuming. Note: when changing the sample size, the self-test parameters need to be updated accordingly. Signed-off-by: Horia Geantă <[email protected]> Signed-off-by: Meenakshi Aggarwal <[email protected]> Reviewed-by: Gaurav Jain <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 2be0d80 commit 1abc896

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

drivers/crypto/caam/ctrl.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static void kick_trng(struct device *dev, int ent_delay)
358358
struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
359359
struct caam_ctrl __iomem *ctrl;
360360
struct rng4tst __iomem *r4tst;
361-
u32 val;
361+
u32 val, rtsdctl;
362362

363363
ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
364364
r4tst = &ctrl->r4tst[0];
@@ -374,26 +374,38 @@ static void kick_trng(struct device *dev, int ent_delay)
374374
* Performance-wise, it does not make sense to
375375
* set the delay to a value that is lower
376376
* than the last one that worked (i.e. the state handles
377-
* were instantiated properly. Thus, instead of wasting
378-
* time trying to set the values controlling the sample
379-
* frequency, the function simply returns.
377+
* were instantiated properly).
380378
*/
381-
val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
382-
>> RTSDCTL_ENT_DLY_SHIFT;
383-
if (ent_delay <= val)
384-
goto start_rng;
385-
386-
val = rd_reg32(&r4tst->rtsdctl);
387-
val = (val & ~RTSDCTL_ENT_DLY_MASK) |
388-
(ent_delay << RTSDCTL_ENT_DLY_SHIFT);
389-
wr_reg32(&r4tst->rtsdctl, val);
390-
/* min. freq. count, equal to 1/4 of the entropy sample length */
391-
wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
392-
/* max. freq. count, equal to 16 times the entropy sample length */
393-
wr_reg32(&r4tst->rtfrqmax, ent_delay << 4);
394-
/* read the control register */
395-
val = rd_reg32(&r4tst->rtmctl);
396-
start_rng:
379+
rtsdctl = rd_reg32(&r4tst->rtsdctl);
380+
val = (rtsdctl & RTSDCTL_ENT_DLY_MASK) >> RTSDCTL_ENT_DLY_SHIFT;
381+
if (ent_delay > val) {
382+
val = ent_delay;
383+
/* min. freq. count, equal to 1/4 of the entropy sample length */
384+
wr_reg32(&r4tst->rtfrqmin, val >> 2);
385+
/* max. freq. count, equal to 16 times the entropy sample length */
386+
wr_reg32(&r4tst->rtfrqmax, val << 4);
387+
}
388+
389+
wr_reg32(&r4tst->rtsdctl, (val << RTSDCTL_ENT_DLY_SHIFT) |
390+
RTSDCTL_SAMP_SIZE_VAL);
391+
392+
/*
393+
* To avoid reprogramming the self-test parameters over and over again,
394+
* use RTSDCTL[SAMP_SIZE] as an indicator.
395+
*/
396+
if ((rtsdctl & RTSDCTL_SAMP_SIZE_MASK) != RTSDCTL_SAMP_SIZE_VAL) {
397+
wr_reg32(&r4tst->rtscmisc, (2 << 16) | 32);
398+
wr_reg32(&r4tst->rtpkrrng, 570);
399+
wr_reg32(&r4tst->rtpkrmax, 1600);
400+
wr_reg32(&r4tst->rtscml, (122 << 16) | 317);
401+
wr_reg32(&r4tst->rtscrl[0], (80 << 16) | 107);
402+
wr_reg32(&r4tst->rtscrl[1], (57 << 16) | 62);
403+
wr_reg32(&r4tst->rtscrl[2], (39 << 16) | 39);
404+
wr_reg32(&r4tst->rtscrl[3], (27 << 16) | 26);
405+
wr_reg32(&r4tst->rtscrl[4], (19 << 16) | 18);
406+
wr_reg32(&r4tst->rtscrl[5], (18 << 16) | 17);
407+
}
408+
397409
/*
398410
* select raw sampling in both entropy shifter
399411
* and statistical checker; ; put RNG4 into run mode

drivers/crypto/caam/regs.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* CAAM hardware register-level view
44
*
55
* Copyright 2008-2011 Freescale Semiconductor, Inc.
6-
* Copyright 2018 NXP
6+
* Copyright 2018, 2023 NXP
77
*/
88

99
#ifndef REGS_H
@@ -523,6 +523,8 @@ struct rng4tst {
523523
#define RTSDCTL_ENT_DLY_MASK (0xffff << RTSDCTL_ENT_DLY_SHIFT)
524524
#define RTSDCTL_ENT_DLY_MIN 3200
525525
#define RTSDCTL_ENT_DLY_MAX 12800
526+
#define RTSDCTL_SAMP_SIZE_MASK 0xffff
527+
#define RTSDCTL_SAMP_SIZE_VAL 512
526528
u32 rtsdctl; /* seed control register */
527529
union {
528530
u32 rtsblim; /* PRGM=1: sparse bit limit register */
@@ -534,7 +536,15 @@ struct rng4tst {
534536
u32 rtfrqmax; /* PRGM=1: freq. count max. limit register */
535537
u32 rtfrqcnt; /* PRGM=0: freq. count register */
536538
};
537-
u32 rsvd1[40];
539+
union {
540+
u32 rtscmc; /* statistical check run monobit count */
541+
u32 rtscml; /* statistical check run monobit limit */
542+
};
543+
union {
544+
u32 rtscrc[6]; /* statistical check run length count */
545+
u32 rtscrl[6]; /* statistical check run length limit */
546+
};
547+
u32 rsvd1[33];
538548
#define RDSTA_SKVT 0x80000000
539549
#define RDSTA_SKVN 0x40000000
540550
#define RDSTA_PR0 BIT(4)

0 commit comments

Comments
 (0)