1 From 43f8f404e2e8cd81baa4d89706e40901c466c7bb Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Horia=20Geant=C4=83?= <horia.geanta@nxp.com>
3 Date: Fri, 21 Feb 2020 11:48:39 +0100
4 Subject: [PATCH] LF-292-1 crypto: caam - refactor RNG initialization
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 RNG (re-)initialization will be needed on pm resume path,
10 thus refactor the corresponding code out of the probe callback.
12 Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
13 Reviewed-by: Valentin Ciocoi Radulescu <valentin.ciocoi@nxp.com>
14 Signed-off-by: Franck LENORMAND <franck.lenormand@nxp.com>
15 Signed-off-by: Leonard Crestez <leonard.crestez@nxp.com>
16 Signed-off-by: Dong Aisheng <aisheng.dong@nxp.com>
18 drivers/crypto/caam/ctrl.c | 189 ++++++++++++++++++++++++---------------------
19 1 file changed, 102 insertions(+), 87 deletions(-)
21 --- a/drivers/crypto/caam/ctrl.c
22 +++ b/drivers/crypto/caam/ctrl.c
23 @@ -327,13 +327,12 @@ static int instantiate_rng(struct device
25 * kick_trng - sets the various parameters for enabling the initialization
26 * of the RNG4 block in CAAM
27 - * @pdev - pointer to the platform device
28 + * @dev - pointer to the controller device
29 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
31 -static void kick_trng(struct platform_device *pdev, int ent_delay)
32 +static void kick_trng(struct device *dev, int ent_delay)
34 - struct device *ctrldev = &pdev->dev;
35 - struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
36 + struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
37 struct caam_ctrl __iomem *ctrl;
38 struct rng4tst __iomem *r4tst;
40 @@ -571,10 +570,105 @@ static void caam_dma_dev_unregister(void
41 platform_device_unregister(data);
44 +static int caam_ctrl_rng_init(struct device *dev)
46 + struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev);
47 + struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
48 + int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
51 + if (ctrlpriv->era < 10) {
52 + struct caam_perfmon __iomem *perfmon;
54 + perfmon = ctrlpriv->total_jobrs ?
55 + (struct caam_perfmon *)&ctrlpriv->jr[0]->perfmon :
56 + (struct caam_perfmon *)&ctrl->perfmon;
58 + rng_vid = (rd_reg32(&perfmon->cha_id_ls) &
59 + CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
61 + struct version_regs __iomem *vreg;
63 + vreg = ctrlpriv->total_jobrs ?
64 + (struct version_regs *)&ctrlpriv->jr[0]->vreg :
65 + (struct version_regs *)&ctrl->vreg;
67 + rng_vid = (rd_reg32(&vreg->rng) & CHA_VER_VID_MASK) >>
72 + * If SEC has RNG version >= 4 and RNG state handle has not been
73 + * already instantiated, do RNG instantiation
74 + * In case of SoCs with Management Complex, RNG is managed by MC f/w.
76 + if (!ctrlpriv->mc_en && rng_vid >= 4) {
77 + ctrlpriv->rng4_sh_init =
78 + rd_reg32(&ctrl->r4tst[0].rdsta);
80 + * If the secure keys (TDKEK, JDKEK, TDSK), were already
81 + * generated, signal this to the function that is instantiating
82 + * the state handles. An error would occur if RNG4 attempts
83 + * to regenerate these keys before the next POR.
85 + gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
86 + ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
89 + rd_reg32(&ctrl->r4tst[0].rdsta) &
92 + * If either SH were instantiated by somebody else
93 + * (e.g. u-boot) then it is assumed that the entropy
94 + * parameters are properly set and thus the function
95 + * setting these (kick_trng(...)) is skipped.
96 + * Also, if a handle was instantiated, do not change
97 + * the TRNG parameters.
99 + if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
101 + "Entropy delay = %u\n",
103 + kick_trng(dev, ent_delay);
107 + * if instantiate_rng(...) fails, the loop will rerun
108 + * and the kick_trng(...) function will modify the
109 + * upper and lower limits of the entropy sampling
110 + * interval, leading to a sucessful initialization of
113 + ret = instantiate_rng(dev, inst_handles,
115 + if (ret == -EAGAIN)
117 + * if here, the loop will rerun,
118 + * so don't hog the CPU
121 + } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
123 + dev_err(dev, "failed to instantiate RNG");
127 + * Set handles init'ed by this module as the complement of the
128 + * already initialized ones
130 + ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
132 + /* Enable RDB bit so that RNG works faster */
133 + clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
139 /* Probe routine for CAAM top (controller) level */
140 static int caam_probe(struct platform_device *pdev)
142 - int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
145 const struct soc_device_attribute *imx_soc_match;
146 static struct platform_device_info caam_dma_pdev_info = {
147 @@ -592,7 +686,6 @@ static int caam_probe(struct platform_de
148 struct dentry *dfs_root;
150 u32 scfgr, comp_params;
153 int BLOCK_OFFSET = 0;
154 bool reg_access = true;
155 @@ -875,90 +968,12 @@ set_dma_mask:
162 - if (ctrlpriv->era < 10) {
163 - rng_vid = (rd_reg32(&perfmon->cha_id_ls) &
164 - CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
166 - struct version_regs __iomem *vreg;
168 - vreg = ring ? (struct version_regs *)&ctrlpriv->jr[0]->vreg :
169 - (struct version_regs *)&ctrl->vreg;
171 - rng_vid = (rd_reg32(&vreg->rng) & CHA_VER_VID_MASK) >>
176 - * If SEC has RNG version >= 4 and RNG state handle has not been
177 - * already instantiated, do RNG instantiation
178 - * In case of SoCs with Management Complex, RNG is managed by MC f/w.
180 - if (!ctrlpriv->mc_en && rng_vid >= 4) {
181 - ctrlpriv->rng4_sh_init =
182 - rd_reg32(&ctrl->r4tst[0].rdsta);
184 - * If the secure keys (TDKEK, JDKEK, TDSK), were already
185 - * generated, signal this to the function that is instantiating
186 - * the state handles. An error would occur if RNG4 attempts
187 - * to regenerate these keys before the next POR.
189 - gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
190 - ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
193 - rd_reg32(&ctrl->r4tst[0].rdsta) &
196 - * If either SH were instantiated by somebody else
197 - * (e.g. u-boot) then it is assumed that the entropy
198 - * parameters are properly set and thus the function
199 - * setting these (kick_trng(...)) is skipped.
200 - * Also, if a handle was instantiated, do not change
201 - * the TRNG parameters.
203 - if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
205 - "Entropy delay = %u\n",
207 - kick_trng(pdev, ent_delay);
211 - * if instantiate_rng(...) fails, the loop will rerun
212 - * and the kick_trng(...) function will modfiy the
213 - * upper and lower limits of the entropy sampling
214 - * interval, leading to a sucessful initialization of
217 - ret = instantiate_rng(dev, inst_handles,
219 - if (ret == -EAGAIN)
221 - * if here, the loop will rerun,
222 - * so don't hog the CPU
225 - } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
227 - dev_err(dev, "failed to instantiate RNG");
229 + ret = caam_ctrl_rng_init(dev);
234 - * Set handles init'ed by this module as the complement of the
235 - * already initialized ones
237 - ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
239 - /* Enable RDB bit so that RNG works faster */
240 - clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
243 - /* NOTE: RTIC detection ought to go here, around Si time */
246 caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 |
247 (u64)rd_reg32(&perfmon->caam_id_ls);