ath79/mikrotik: use routerbootpart partitions
[oweals/openwrt.git] / target / linux / layerscape / patches-5.4 / 804-crypto-0040-LF-292-1-crypto-caam-refactor-RNG-initialization.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 RNG (re-)initialization will be needed on pm resume path,
10 thus refactor the corresponding code out of the probe callback.
11
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>
17 ---
18  drivers/crypto/caam/ctrl.c | 189 ++++++++++++++++++++++++---------------------
19  1 file changed, 102 insertions(+), 87 deletions(-)
20
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
24  /*
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.
30   */
31 -static void kick_trng(struct platform_device *pdev, int ent_delay)
32 +static void kick_trng(struct device *dev, int ent_delay)
33  {
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;
39         u32 val;
40 @@ -571,10 +570,105 @@ static void caam_dma_dev_unregister(void
41         platform_device_unregister(data);
42  }
43  
44 +static int caam_ctrl_rng_init(struct device *dev)
45 +{
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;
49 +       u8 rng_vid;
50 +
51 +       if (ctrlpriv->era < 10) {
52 +               struct caam_perfmon __iomem *perfmon;
53 +
54 +               perfmon = ctrlpriv->total_jobrs ?
55 +                         (struct caam_perfmon *)&ctrlpriv->jr[0]->perfmon :
56 +                         (struct caam_perfmon *)&ctrl->perfmon;
57 +
58 +               rng_vid = (rd_reg32(&perfmon->cha_id_ls) &
59 +                          CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
60 +       } else {
61 +               struct version_regs __iomem *vreg;
62 +
63 +               vreg = ctrlpriv->total_jobrs ?
64 +                       (struct version_regs *)&ctrlpriv->jr[0]->vreg :
65 +                       (struct version_regs *)&ctrl->vreg;
66 +
67 +               rng_vid = (rd_reg32(&vreg->rng) & CHA_VER_VID_MASK) >>
68 +                         CHA_VER_VID_SHIFT;
69 +       }
70 +
71 +       /*
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.
75 +        */
76 +       if (!ctrlpriv->mc_en && rng_vid >= 4) {
77 +               ctrlpriv->rng4_sh_init =
78 +                       rd_reg32(&ctrl->r4tst[0].rdsta);
79 +               /*
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.
84 +                */
85 +               gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
86 +               ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
87 +               do {
88 +                       int inst_handles =
89 +                               rd_reg32(&ctrl->r4tst[0].rdsta) &
90 +                                                               RDSTA_IFMASK;
91 +                       /*
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.
98 +                        */
99 +                       if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
100 +                               dev_info(dev,
101 +                                        "Entropy delay = %u\n",
102 +                                        ent_delay);
103 +                               kick_trng(dev, ent_delay);
104 +                               ent_delay += 400;
105 +                       }
106 +                       /*
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
111 +                        * the RNG.
112 +                        */
113 +                       ret = instantiate_rng(dev, inst_handles,
114 +                                             gen_sk);
115 +                       if (ret == -EAGAIN)
116 +                               /*
117 +                                * if here, the loop will rerun,
118 +                                * so don't hog the CPU
119 +                                */
120 +                               cpu_relax();
121 +               } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
122 +               if (ret) {
123 +                       dev_err(dev, "failed to instantiate RNG");
124 +                       return ret;
125 +               }
126 +               /*
127 +                * Set handles init'ed by this module as the complement of the
128 +                * already initialized ones
129 +                */
130 +               ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
131 +
132 +               /* Enable RDB bit so that RNG works faster */
133 +               clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
134 +       }
135 +
136 +       return 0;
137 +}
138 +
139  /* Probe routine for CAAM top (controller) level */
140  static int caam_probe(struct platform_device *pdev)
141  {
142 -       int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
143 +       int ret, ring;
144         u64 caam_id;
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;
149  #endif
150         u32 scfgr, comp_params;
151 -       u8 rng_vid;
152         int pg_size;
153         int BLOCK_OFFSET = 0;
154         bool reg_access = true;
155 @@ -875,90 +968,12 @@ set_dma_mask:
156                         return ret;
157         }
158  
159 -       if (!reg_access)
160 -               goto report_live;
161 -
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;
165 -       } else {
166 -               struct version_regs __iomem *vreg;
167 -
168 -               vreg = ring ? (struct version_regs *)&ctrlpriv->jr[0]->vreg :
169 -                             (struct version_regs *)&ctrl->vreg;
170 -
171 -               rng_vid = (rd_reg32(&vreg->rng) & CHA_VER_VID_MASK) >>
172 -                          CHA_VER_VID_SHIFT;
173 -       }
174 -
175 -       /*
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.
179 -        */
180 -       if (!ctrlpriv->mc_en && rng_vid >= 4) {
181 -               ctrlpriv->rng4_sh_init =
182 -                       rd_reg32(&ctrl->r4tst[0].rdsta);
183 -               /*
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.
188 -                */
189 -               gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
190 -               ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
191 -               do {
192 -                       int inst_handles =
193 -                               rd_reg32(&ctrl->r4tst[0].rdsta) &
194 -                                                               RDSTA_IFMASK;
195 -                       /*
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.
202 -                        */
203 -                       if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
204 -                               dev_info(dev,
205 -                                        "Entropy delay = %u\n",
206 -                                        ent_delay);
207 -                               kick_trng(pdev, ent_delay);
208 -                               ent_delay += 400;
209 -                       }
210 -                       /*
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
215 -                        * the RNG.
216 -                        */
217 -                       ret = instantiate_rng(dev, inst_handles,
218 -                                             gen_sk);
219 -                       if (ret == -EAGAIN)
220 -                               /*
221 -                                * if here, the loop will rerun,
222 -                                * so don't hog the CPU
223 -                                */
224 -                               cpu_relax();
225 -               } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
226 -               if (ret) {
227 -                       dev_err(dev, "failed to instantiate RNG");
228 +       if (reg_access) {
229 +               ret = caam_ctrl_rng_init(dev);
230 +               if (ret)
231                         return ret;
232 -               }
233 -               /*
234 -                * Set handles init'ed by this module as the complement of the
235 -                * already initialized ones
236 -                */
237 -               ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
238 -
239 -               /* Enable RDB bit so that RNG works faster */
240 -               clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
241         }
242  
243 -       /* NOTE: RTIC detection ought to go here, around Si time */
244 -
245 -report_live:
246         caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 |
247                   (u64)rd_reg32(&perfmon->caam_id_ls);
248