blob: 70f1e6f3733623a65ec236deea1b4d1f07a5040d [file] [log] [blame]
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +05301/* * CAAM control-plane driver backend
Kim Phillips8e8ec592011-03-13 16:54:26 +08002 * Controller-level driver, kernel property detection, initialization
3 *
Kim Phillips281922a2012-06-22 19:48:52 -05004 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kim Phillips8e8ec592011-03-13 16:54:26 +08005 */
6
Himangi Saraogi4776d382014-05-27 23:55:48 +05307#include <linux/device.h>
Rob Herring5af50732013-09-17 14:28:33 -05008#include <linux/of_address.h>
9#include <linux/of_irq.h>
10
Kim Phillips8e8ec592011-03-13 16:54:26 +080011#include "compat.h"
12#include "regs.h"
13#include "intern.h"
14#include "jr.h"
Kim Phillips281922a2012-06-22 19:48:52 -050015#include "desc_constr.h"
16#include "error.h"
Kim Phillips8e8ec592011-03-13 16:54:26 +080017
Kim Phillips281922a2012-06-22 19:48:52 -050018/*
19 * Descriptor to instantiate RNG State Handle 0 in normal mode and
20 * load the JDKEK, TDKEK and TDSK registers
21 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030022static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
Kim Phillips281922a2012-06-22 19:48:52 -050023{
Alex Porosanu1005bcc2013-09-09 18:56:34 +030024 u32 *jump_cmd, op_flags;
Kim Phillips281922a2012-06-22 19:48:52 -050025
26 init_job_desc(desc, 0);
27
Alex Porosanu1005bcc2013-09-09 18:56:34 +030028 op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
29 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
30
Kim Phillips281922a2012-06-22 19:48:52 -050031 /* INIT RNG in non-test mode */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030032 append_operation(desc, op_flags);
Kim Phillips281922a2012-06-22 19:48:52 -050033
Alex Porosanu1005bcc2013-09-09 18:56:34 +030034 if (!handle && do_sk) {
35 /*
36 * For SH0, Secure Keys must be generated as well
37 */
Kim Phillips281922a2012-06-22 19:48:52 -050038
Alex Porosanu1005bcc2013-09-09 18:56:34 +030039 /* wait for done */
40 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
41 set_jump_tgt_here(desc, jump_cmd);
Kim Phillips281922a2012-06-22 19:48:52 -050042
Alex Porosanu1005bcc2013-09-09 18:56:34 +030043 /*
44 * load 1 to clear written reg:
45 * resets the done interrrupt and returns the RNG to idle.
46 */
47 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
48
49 /* Initialize State Handle */
50 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
51 OP_ALG_AAI_RNG4_SK);
52 }
Alex Porosanud5e4e992013-09-09 18:56:28 +030053
54 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
Kim Phillips281922a2012-06-22 19:48:52 -050055}
56
Alex Porosanub1f996e02013-09-09 18:56:32 +030057/* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030058static void build_deinstantiation_desc(u32 *desc, int handle)
Alex Porosanub1f996e02013-09-09 18:56:32 +030059{
60 init_job_desc(desc, 0);
61
62 /* Uninstantiate State Handle 0 */
63 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
Alex Porosanu1005bcc2013-09-09 18:56:34 +030064 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
Alex Porosanub1f996e02013-09-09 18:56:32 +030065
66 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
67}
Alex Porosanu04cddbf2013-09-09 18:56:31 +030068
69/*
70 * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
71 * the software (no JR/QI used).
72 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +030073 * @status - descriptor status, after being run
74 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +030075 * Return: - 0 if no error occurred
76 * - -ENODEV if the DECO couldn't be acquired
77 * - -EAGAIN if an error occurred while executing the descriptor
78 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +030079static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
80 u32 *status)
Kim Phillips281922a2012-06-22 19:48:52 -050081{
Ruchika Gupta997ad292013-07-04 11:26:03 +053082 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +053083 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
84 struct caam_deco __iomem *deco = ctrlpriv->deco;
Ruchika Gupta997ad292013-07-04 11:26:03 +053085 unsigned int timeout = 100000;
Alex Porosanu04cddbf2013-09-09 18:56:31 +030086 u32 deco_dbg_reg, flags;
Alex Porosanub1f996e02013-09-09 18:56:32 +030087 int i;
Ruchika Gupta997ad292013-07-04 11:26:03 +053088
Ruchika Gupta17157c92014-06-23 17:42:33 +053089
Horia Geanta8f1da7b2014-07-21 16:03:21 +030090 if (ctrlpriv->virt_en == 1) {
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +053091 setbits32(&ctrl->deco_rsr, DECORSR_JR0);
Ruchika Gupta17157c92014-06-23 17:42:33 +053092
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +053093 while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
Horia Geanta8f1da7b2014-07-21 16:03:21 +030094 --timeout)
95 cpu_relax();
96
97 timeout = 100000;
98 }
Ruchika Gupta17157c92014-06-23 17:42:33 +053099
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530100 setbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530101
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530102 while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
Ruchika Gupta997ad292013-07-04 11:26:03 +0530103 --timeout)
104 cpu_relax();
105
106 if (!timeout) {
107 dev_err(ctrldev, "failed to acquire DECO 0\n");
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530108 clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300109 return -ENODEV;
Kim Phillips281922a2012-06-22 19:48:52 -0500110 }
111
Ruchika Gupta997ad292013-07-04 11:26:03 +0530112 for (i = 0; i < desc_len(desc); i++)
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530113 wr_reg32(&deco->descbuf[i], *(desc + i));
Kim Phillips281922a2012-06-22 19:48:52 -0500114
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300115 flags = DECO_JQCR_WHL;
116 /*
117 * If the descriptor length is longer than 4 words, then the
118 * FOUR bit in JRCTRL register must be set.
119 */
120 if (desc_len(desc) >= 4)
121 flags |= DECO_JQCR_FOUR;
122
123 /* Instruct the DECO to execute it */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530124 wr_reg32(&deco->jr_ctl_hi, flags);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530125
126 timeout = 10000000;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300127 do {
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530128 deco_dbg_reg = rd_reg32(&deco->desc_dbg);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300129 /*
130 * If an error occured in the descriptor, then
131 * the DECO status field will be set to 0x0D
132 */
133 if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
134 DESC_DBG_DECO_STAT_HOST_ERR)
135 break;
Ruchika Gupta997ad292013-07-04 11:26:03 +0530136 cpu_relax();
Alex Porosanu84cf4822013-09-09 18:56:30 +0300137 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530138
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530139 *status = rd_reg32(&deco->op_status_hi) &
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300140 DECO_OP_STATUS_HI_ERR_MASK;
141
Ruchika Gupta17157c92014-06-23 17:42:33 +0530142 if (ctrlpriv->virt_en == 1)
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530143 clrbits32(&ctrl->deco_rsr, DECORSR_JR0);
Ruchika Gupta17157c92014-06-23 17:42:33 +0530144
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300145 /* Mark the DECO as free */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530146 clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300147
148 if (!timeout)
149 return -EAGAIN;
150
151 return 0;
152}
153
154/*
155 * instantiate_rng - builds and executes a descriptor on DECO0,
156 * which initializes the RNG block.
157 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300158 * @state_handle_mask - bitmask containing the instantiation status
159 * for the RNG4 state handles which exist in
160 * the RNG4 block: 1 if it's been instantiated
161 * by an external entry, 0 otherwise.
162 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
163 * Caution: this can be done only once; if the keys need to be
164 * regenerated, a POR is required
165 *
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300166 * Return: - 0 if no error occurred
167 * - -ENOMEM if there isn't enough memory to allocate the descriptor
168 * - -ENODEV if DECO0 couldn't be acquired
169 * - -EAGAIN if an error occurred when executing the descriptor
170 * f.i. there was a RNG hardware error due to not "good enough"
171 * entropy being aquired.
172 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300173static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
174 int gen_sk)
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300175{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300176 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530177 struct caam_ctrl __iomem *ctrl;
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300178 struct rng4tst __iomem *r4tst;
179 u32 *desc, status, rdsta_val;
180 int ret = 0, sh_idx;
181
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530182 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
183 r4tst = &ctrl->r4tst[0];
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300184
185 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
186 if (!desc)
187 return -ENOMEM;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300188
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300189 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
190 /*
191 * If the corresponding bit is set, this state handle
192 * was initialized by somebody else, so it's left alone.
193 */
194 if ((1 << sh_idx) & state_handle_mask)
195 continue;
196
197 /* Create the descriptor for instantiating RNG State Handle */
198 build_instantiation_desc(desc, sh_idx, gen_sk);
199
200 /* Try to run it through DECO0 */
201 ret = run_descriptor_deco0(ctrldev, desc, &status);
202
203 /*
204 * If ret is not 0, or descriptor status is not 0, then
205 * something went wrong. No need to try the next state
206 * handle (if available), bail out here.
207 * Also, if for some reason, the State Handle didn't get
208 * instantiated although the descriptor has finished
209 * without any error (HW optimizations for later
210 * CAAM eras), then try again.
211 */
212 rdsta_val =
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530213 rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK;
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300214 if (status || !(rdsta_val & (1 << sh_idx)))
215 ret = -EAGAIN;
216 if (ret)
217 break;
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300218 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
219 /* Clear the contents before recreating the descriptor */
220 memset(desc, 0x00, CAAM_CMD_SZ * 7);
Ruchika Gupta997ad292013-07-04 11:26:03 +0530221 }
222
Kim Phillips281922a2012-06-22 19:48:52 -0500223 kfree(desc);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300224
Kim Phillips281922a2012-06-22 19:48:52 -0500225 return ret;
226}
227
228/*
Alex Porosanub1f996e02013-09-09 18:56:32 +0300229 * deinstantiate_rng - builds and executes a descriptor on DECO0,
230 * which deinitializes the RNG block.
231 * @ctrldev - pointer to device
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300232 * @state_handle_mask - bitmask containing the instantiation status
233 * for the RNG4 state handles which exist in
234 * the RNG4 block: 1 if it's been instantiated
Alex Porosanub1f996e02013-09-09 18:56:32 +0300235 *
236 * Return: - 0 if no error occurred
237 * - -ENOMEM if there isn't enough memory to allocate the descriptor
238 * - -ENODEV if DECO0 couldn't be acquired
239 * - -EAGAIN if an error occurred when executing the descriptor
Kim Phillips281922a2012-06-22 19:48:52 -0500240 */
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300241static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
Alex Porosanub1f996e02013-09-09 18:56:32 +0300242{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300243 u32 *desc, status;
244 int sh_idx, ret = 0;
Alex Porosanub1f996e02013-09-09 18:56:32 +0300245
246 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
247 if (!desc)
248 return -ENOMEM;
249
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300250 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
251 /*
252 * If the corresponding bit is set, then it means the state
253 * handle was initialized by us, and thus it needs to be
254 * deintialized as well
255 */
256 if ((1 << sh_idx) & state_handle_mask) {
257 /*
258 * Create the descriptor for deinstantating this state
259 * handle
260 */
261 build_deinstantiation_desc(desc, sh_idx);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300262
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300263 /* Try to run it through DECO0 */
264 ret = run_descriptor_deco0(ctrldev, desc, &status);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300265
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300266 if (ret || status) {
267 dev_err(ctrldev,
268 "Failed to deinstantiate RNG4 SH%d\n",
269 sh_idx);
270 break;
271 }
272 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
273 }
274 }
Alex Porosanub1f996e02013-09-09 18:56:32 +0300275
276 kfree(desc);
277
278 return ret;
279}
280
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300281static int caam_remove(struct platform_device *pdev)
282{
283 struct device *ctrldev;
284 struct caam_drv_private *ctrlpriv;
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530285 struct caam_ctrl __iomem *ctrl;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300286 int ring, ret = 0;
287
288 ctrldev = &pdev->dev;
289 ctrlpriv = dev_get_drvdata(ctrldev);
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530290 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300291
Ruchika Gupta313ea292013-10-25 12:01:01 +0530292 /* Remove platform devices for JobRs */
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300293 for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
Ruchika Gupta313ea292013-10-25 12:01:01 +0530294 if (ctrlpriv->jrpdev[ring])
295 of_device_unregister(ctrlpriv->jrpdev[ring]);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300296 }
297
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300298 /* De-initialize RNG state handles initialized by this driver. */
299 if (ctrlpriv->rng4_sh_init)
300 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
Alex Porosanub1f996e02013-09-09 18:56:32 +0300301
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300302 /* Shut down debug views */
303#ifdef CONFIG_DEBUG_FS
304 debugfs_remove_recursive(ctrlpriv->dfs_root);
305#endif
306
307 /* Unmap controller region */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530308 iounmap(&ctrl);
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300309
Kim Phillips281922a2012-06-22 19:48:52 -0500310 return ret;
311}
312
313/*
Alex Porosanu84cf4822013-09-09 18:56:30 +0300314 * kick_trng - sets the various parameters for enabling the initialization
315 * of the RNG4 block in CAAM
316 * @pdev - pointer to the platform device
317 * @ent_delay - Defines the length (in system clocks) of each entropy sample.
Kim Phillips281922a2012-06-22 19:48:52 -0500318 */
Alex Porosanu84cf4822013-09-09 18:56:30 +0300319static void kick_trng(struct platform_device *pdev, int ent_delay)
Kim Phillips281922a2012-06-22 19:48:52 -0500320{
321 struct device *ctrldev = &pdev->dev;
322 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530323 struct caam_ctrl __iomem *ctrl;
Kim Phillips281922a2012-06-22 19:48:52 -0500324 struct rng4tst __iomem *r4tst;
325 u32 val;
326
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530327 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
328 r4tst = &ctrl->r4tst[0];
Kim Phillips281922a2012-06-22 19:48:52 -0500329
330 /* put RNG4 into program mode */
331 setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300332
333 /*
334 * Performance-wise, it does not make sense to
335 * set the delay to a value that is lower
336 * than the last one that worked (i.e. the state handles
337 * were instantiated properly. Thus, instead of wasting
338 * time trying to set the values controlling the sample
339 * frequency, the function simply returns.
340 */
341 val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
342 >> RTSDCTL_ENT_DLY_SHIFT;
343 if (ent_delay <= val) {
344 /* put RNG4 into run mode */
345 clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
346 return;
347 }
348
Kim Phillips281922a2012-06-22 19:48:52 -0500349 val = rd_reg32(&r4tst->rtsdctl);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300350 val = (val & ~RTSDCTL_ENT_DLY_MASK) |
351 (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
Kim Phillips281922a2012-06-22 19:48:52 -0500352 wr_reg32(&r4tst->rtsdctl, val);
Alex Porosanu84cf4822013-09-09 18:56:30 +0300353 /* min. freq. count, equal to 1/4 of the entropy sample length */
354 wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
Alex Porosanub061f3f2014-08-11 11:40:15 +0300355 /* disable maximum frequency count */
356 wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
Alex Porosanue5ffbfc2014-08-11 11:40:17 +0300357 /* read the control register */
358 val = rd_reg32(&r4tst->rtmctl);
359 /*
360 * select raw sampling in both entropy shifter
361 * and statistical checker
362 */
363 setbits32(&val, RTMCTL_SAMP_MODE_RAW_ES_SC);
Kim Phillips281922a2012-06-22 19:48:52 -0500364 /* put RNG4 into run mode */
Alex Porosanue5ffbfc2014-08-11 11:40:17 +0300365 clrbits32(&val, RTMCTL_PRGM);
366 /* write back the control register */
367 wr_reg32(&r4tst->rtmctl, val);
Kim Phillips281922a2012-06-22 19:48:52 -0500368}
369
Alex Porosanu82c2f962012-07-11 11:06:11 +0800370/**
371 * caam_get_era() - Return the ERA of the SEC on SoC, based
Alex Porosanu883619a2014-02-06 10:27:19 +0200372 * on "sec-era" propery in the DTS. This property is updated by u-boot.
Alex Porosanu82c2f962012-07-11 11:06:11 +0800373 **/
Alex Porosanu883619a2014-02-06 10:27:19 +0200374int caam_get_era(void)
Alex Porosanu82c2f962012-07-11 11:06:11 +0800375{
Alex Porosanu883619a2014-02-06 10:27:19 +0200376 struct device_node *caam_node;
377 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
378 const uint32_t *prop = (uint32_t *)of_get_property(caam_node,
379 "fsl,sec-era",
380 NULL);
381 return prop ? *prop : -ENOTSUPP;
382 }
Alex Porosanu82c2f962012-07-11 11:06:11 +0800383
384 return -ENOTSUPP;
385}
386EXPORT_SYMBOL(caam_get_era);
387
Kim Phillips8e8ec592011-03-13 16:54:26 +0800388/* Probe routine for CAAM top (controller) level */
Kim Phillips2930d492011-05-14 22:07:55 -0500389static int caam_probe(struct platform_device *pdev)
Kim Phillips8e8ec592011-03-13 16:54:26 +0800390{
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300391 int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
Alex Porosanu82c2f962012-07-11 11:06:11 +0800392 u64 caam_id;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800393 struct device *dev;
394 struct device_node *nprop, *np;
395 struct caam_ctrl __iomem *ctrl;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800396 struct caam_drv_private *ctrlpriv;
Kim Phillips23457bc2011-06-05 16:42:54 -0500397#ifdef CONFIG_DEBUG_FS
398 struct caam_perfmon *perfmon;
399#endif
Ruchika Gupta17157c92014-06-23 17:42:33 +0530400 u32 scfgr, comp_params;
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530401 u32 cha_vid_ls;
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530402 int pg_size;
403 int BLOCK_OFFSET = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800404
Himangi Saraogi4776d382014-05-27 23:55:48 +0530405 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(struct caam_drv_private),
406 GFP_KERNEL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800407 if (!ctrlpriv)
408 return -ENOMEM;
409
410 dev = &pdev->dev;
411 dev_set_drvdata(dev, ctrlpriv);
412 ctrlpriv->pdev = pdev;
413 nprop = pdev->dev.of_node;
414
415 /* Get configuration properties from device tree */
416 /* First, get register page */
417 ctrl = of_iomap(nprop, 0);
418 if (ctrl == NULL) {
419 dev_err(dev, "caam: of_iomap() failed\n");
420 return -ENOMEM;
421 }
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530422 /* Finding the page size for using the CTPR_MS register */
423 comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
424 pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800425
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530426 /* Allocating the BLOCK_OFFSET based on the supported page size on
427 * the platform
428 */
429 if (pg_size == 0)
430 BLOCK_OFFSET = PG_SIZE_4K;
431 else
432 BLOCK_OFFSET = PG_SIZE_64K;
433
434 ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
435 ctrlpriv->assure = (struct caam_assurance __force *)
436 ((uint8_t *)ctrl +
437 BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
438 );
439 ctrlpriv->deco = (struct caam_deco __force *)
440 ((uint8_t *)ctrl +
441 BLOCK_OFFSET * DECO_BLOCK_NUMBER
442 );
Kim Phillips8e8ec592011-03-13 16:54:26 +0800443
444 /* Get the IRQ of the controller (for security violations only) */
Thierry Redingf7578492013-09-18 15:24:44 +0200445 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800446
447 /*
448 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
Kim Phillipse13af182012-06-22 19:48:51 -0500449 * long pointers in master configuration register
Kim Phillips8e8ec592011-03-13 16:54:26 +0800450 */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530451 setbits32(&ctrl->mcr, MCFGR_WDENABLE |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800452 (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
453
Ruchika Gupta17157c92014-06-23 17:42:33 +0530454 /*
455 * Read the Compile Time paramters and SCFGR to determine
456 * if Virtualization is enabled for this platform
457 */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530458 scfgr = rd_reg32(&ctrl->scfgr);
Ruchika Gupta17157c92014-06-23 17:42:33 +0530459
460 ctrlpriv->virt_en = 0;
461 if (comp_params & CTPR_MS_VIRT_EN_INCL) {
462 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
463 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
464 */
465 if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
466 (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
467 (scfgr & SCFGR_VIRT_EN)))
468 ctrlpriv->virt_en = 1;
469 } else {
470 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
471 if (comp_params & CTPR_MS_VIRT_EN_POR)
472 ctrlpriv->virt_en = 1;
473 }
474
475 if (ctrlpriv->virt_en == 1)
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530476 setbits32(&ctrl->jrstart, JRSTART_JR0_START |
Ruchika Gupta17157c92014-06-23 17:42:33 +0530477 JRSTART_JR1_START | JRSTART_JR2_START |
478 JRSTART_JR3_START);
479
Kim Phillips8e8ec592011-03-13 16:54:26 +0800480 if (sizeof(dma_addr_t) == sizeof(u64))
Kim Phillipse13af182012-06-22 19:48:51 -0500481 if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
Horia Geantaa2ac2872014-07-11 15:34:47 +0300482 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
Kim Phillipse13af182012-06-22 19:48:51 -0500483 else
Horia Geantaa2ac2872014-07-11 15:34:47 +0300484 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36));
Kim Phillipse13af182012-06-22 19:48:51 -0500485 else
Horia Geantaa2ac2872014-07-11 15:34:47 +0300486 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Kim Phillips8e8ec592011-03-13 16:54:26 +0800487
Kim Phillips8e8ec592011-03-13 16:54:26 +0800488 /*
489 * Detect and enable JobRs
490 * First, find out how many ring spec'ed, allocate references
491 * for all, then go probe each one.
492 */
493 rspec = 0;
Nitesh Lal0a63b092014-02-09 09:59:13 +0800494 for_each_available_child_of_node(nprop, np)
495 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
496 of_device_is_compatible(np, "fsl,sec4.0-job-ring"))
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800497 rspec++;
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800498
Himangi Saraogi4776d382014-05-27 23:55:48 +0530499 ctrlpriv->jrpdev = devm_kzalloc(&pdev->dev,
500 sizeof(struct platform_device *) * rspec,
501 GFP_KERNEL);
Ruchika Gupta313ea292013-10-25 12:01:01 +0530502 if (ctrlpriv->jrpdev == NULL) {
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530503 iounmap(&ctrl);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800504 return -ENOMEM;
505 }
506
507 ring = 0;
508 ctrlpriv->total_jobrs = 0;
Nitesh Lal0a63b092014-02-09 09:59:13 +0800509 for_each_available_child_of_node(nprop, np)
510 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
511 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
Ruchika Gupta313ea292013-10-25 12:01:01 +0530512 ctrlpriv->jrpdev[ring] =
513 of_platform_device_create(np, NULL, dev);
514 if (!ctrlpriv->jrpdev[ring]) {
515 pr_warn("JR%d Platform device creation error\n",
516 ring);
517 continue;
518 }
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530519 ctrlpriv->jr[ring] = (struct caam_job_ring __force *)
520 ((uint8_t *)ctrl +
521 (ring + JR_BLOCK_NUMBER) *
522 BLOCK_OFFSET
523 );
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800524 ctrlpriv->total_jobrs++;
525 ring++;
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530526 }
Kim Phillips8e8ec592011-03-13 16:54:26 +0800527
528 /* Check to see if QI present. If so, enable */
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530529 ctrlpriv->qi_present =
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530530 !!(rd_reg32(&ctrl->perfmon.comp_parms_ms) &
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530531 CTPR_MS_QI_MASK);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800532 if (ctrlpriv->qi_present) {
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530533 ctrlpriv->qi = (struct caam_queue_if __force *)
534 ((uint8_t *)ctrl +
535 BLOCK_OFFSET * QI_BLOCK_NUMBER
536 );
Kim Phillips8e8ec592011-03-13 16:54:26 +0800537 /* This is all that's required to physically enable QI */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530538 wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800539 }
540
541 /* If no QI and no rings specified, quit and go home */
542 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
543 dev_err(dev, "no queues configured, terminating\n");
544 caam_remove(pdev);
545 return -ENOMEM;
546 }
547
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530548 cha_vid_ls = rd_reg32(&ctrl->perfmon.cha_id_ls);
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530549
Kim Phillips281922a2012-06-22 19:48:52 -0500550 /*
Ruchika Gupta986dfbc2013-04-26 15:44:54 +0530551 * If SEC has RNG version >= 4 and RNG state handle has not been
Alex Porosanu84cf4822013-09-09 18:56:30 +0300552 * already instantiated, do RNG instantiation
Kim Phillips281922a2012-06-22 19:48:52 -0500553 */
Ruchika Guptaeb1139c2014-06-23 15:08:28 +0530554 if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300555 ctrlpriv->rng4_sh_init =
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530556 rd_reg32(&ctrl->r4tst[0].rdsta);
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300557 /*
558 * If the secure keys (TDKEK, JDKEK, TDSK), were already
559 * generated, signal this to the function that is instantiating
560 * the state handles. An error would occur if RNG4 attempts
561 * to regenerate these keys before the next POR.
562 */
563 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
564 ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
Alex Porosanu84cf4822013-09-09 18:56:30 +0300565 do {
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300566 int inst_handles =
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530567 rd_reg32(&ctrl->r4tst[0].rdsta) &
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300568 RDSTA_IFMASK;
569 /*
570 * If either SH were instantiated by somebody else
571 * (e.g. u-boot) then it is assumed that the entropy
572 * parameters are properly set and thus the function
573 * setting these (kick_trng(...)) is skipped.
574 * Also, if a handle was instantiated, do not change
575 * the TRNG parameters.
576 */
577 if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
Alex Porosanueeaa1722014-08-11 11:40:16 +0300578 dev_info(dev,
579 "Entropy delay = %u\n",
580 ent_delay);
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300581 kick_trng(pdev, ent_delay);
582 ent_delay += 400;
583 }
584 /*
585 * if instantiate_rng(...) fails, the loop will rerun
586 * and the kick_trng(...) function will modfiy the
587 * upper and lower limits of the entropy sampling
588 * interval, leading to a sucessful initialization of
589 * the RNG.
590 */
591 ret = instantiate_rng(dev, inst_handles,
592 gen_sk);
Alex Porosanueeaa1722014-08-11 11:40:16 +0300593 if (ret == -EAGAIN)
594 /*
595 * if here, the loop will rerun,
596 * so don't hog the CPU
597 */
598 cpu_relax();
Alex Porosanu04cddbf2013-09-09 18:56:31 +0300599 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
Kim Phillips281922a2012-06-22 19:48:52 -0500600 if (ret) {
Alex Porosanu84cf4822013-09-09 18:56:30 +0300601 dev_err(dev, "failed to instantiate RNG");
Kim Phillips281922a2012-06-22 19:48:52 -0500602 caam_remove(pdev);
603 return ret;
604 }
Alex Porosanu1005bcc2013-09-09 18:56:34 +0300605 /*
606 * Set handles init'ed by this module as the complement of the
607 * already initialized ones
608 */
609 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
Vakul Garg575c1bd2013-03-12 13:55:21 +0530610
611 /* Enable RDB bit so that RNG works faster */
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530612 setbits32(&ctrl->scfgr, SCFGR_RDBENABLE);
Kim Phillips281922a2012-06-22 19:48:52 -0500613 }
614
Kim Phillips8e8ec592011-03-13 16:54:26 +0800615 /* NOTE: RTIC detection ought to go here, around Si time */
616
Nitesh Narayan Lalfb4562b2014-09-01 15:00:44 +0530617 caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
618 (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
Alex Porosanu82c2f962012-07-11 11:06:11 +0800619
Kim Phillips8e8ec592011-03-13 16:54:26 +0800620 /* Report "alive" for developer to see */
Alex Porosanu82c2f962012-07-11 11:06:11 +0800621 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
Alex Porosanu883619a2014-02-06 10:27:19 +0200622 caam_get_era());
Kim Phillips8e8ec592011-03-13 16:54:26 +0800623 dev_info(dev, "job rings = %d, qi = %d\n",
624 ctrlpriv->total_jobrs, ctrlpriv->qi_present);
625
626#ifdef CONFIG_DEBUG_FS
627 /*
628 * FIXME: needs better naming distinction, as some amalgamation of
629 * "caam" and nprop->full_name. The OF name isn't distinctive,
630 * but does separate instances
631 */
632 perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
633
Nitesh Narayan Lal178f8272014-07-01 19:54:54 +0530634 ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800635 ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
636
637 /* Controller-level - performance monitor counters */
638 ctrlpriv->ctl_rq_dequeued =
639 debugfs_create_u64("rq_dequeued",
Al Viroeda65cc2011-07-24 04:32:53 -0400640 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800641 ctrlpriv->ctl, &perfmon->req_dequeued);
642 ctrlpriv->ctl_ob_enc_req =
643 debugfs_create_u64("ob_rq_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400644 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800645 ctrlpriv->ctl, &perfmon->ob_enc_req);
646 ctrlpriv->ctl_ib_dec_req =
647 debugfs_create_u64("ib_rq_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400648 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800649 ctrlpriv->ctl, &perfmon->ib_dec_req);
650 ctrlpriv->ctl_ob_enc_bytes =
651 debugfs_create_u64("ob_bytes_encrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400652 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800653 ctrlpriv->ctl, &perfmon->ob_enc_bytes);
654 ctrlpriv->ctl_ob_prot_bytes =
655 debugfs_create_u64("ob_bytes_protected",
Al Viroeda65cc2011-07-24 04:32:53 -0400656 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800657 ctrlpriv->ctl, &perfmon->ob_prot_bytes);
658 ctrlpriv->ctl_ib_dec_bytes =
659 debugfs_create_u64("ib_bytes_decrypted",
Al Viroeda65cc2011-07-24 04:32:53 -0400660 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800661 ctrlpriv->ctl, &perfmon->ib_dec_bytes);
662 ctrlpriv->ctl_ib_valid_bytes =
663 debugfs_create_u64("ib_bytes_validated",
Al Viroeda65cc2011-07-24 04:32:53 -0400664 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800665 ctrlpriv->ctl, &perfmon->ib_valid_bytes);
666
667 /* Controller level - global status values */
668 ctrlpriv->ctl_faultaddr =
669 debugfs_create_u64("fault_addr",
Al Viroeda65cc2011-07-24 04:32:53 -0400670 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800671 ctrlpriv->ctl, &perfmon->faultaddr);
672 ctrlpriv->ctl_faultdetail =
673 debugfs_create_u32("fault_detail",
Al Viroeda65cc2011-07-24 04:32:53 -0400674 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800675 ctrlpriv->ctl, &perfmon->faultdetail);
676 ctrlpriv->ctl_faultstatus =
677 debugfs_create_u32("fault_status",
Al Viroeda65cc2011-07-24 04:32:53 -0400678 S_IRUSR | S_IRGRP | S_IROTH,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800679 ctrlpriv->ctl, &perfmon->status);
680
681 /* Internal covering keys (useful in non-secure mode only) */
682 ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
683 ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
684 ctrlpriv->ctl_kek = debugfs_create_blob("kek",
Al Viroeda65cc2011-07-24 04:32:53 -0400685 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800686 S_IRGRP | S_IROTH,
687 ctrlpriv->ctl,
688 &ctrlpriv->ctl_kek_wrap);
689
690 ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
691 ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
692 ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
Al Viroeda65cc2011-07-24 04:32:53 -0400693 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800694 S_IRGRP | S_IROTH,
695 ctrlpriv->ctl,
696 &ctrlpriv->ctl_tkek_wrap);
697
698 ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
699 ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
700 ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
Al Viroeda65cc2011-07-24 04:32:53 -0400701 S_IRUSR |
Kim Phillips8e8ec592011-03-13 16:54:26 +0800702 S_IRGRP | S_IROTH,
703 ctrlpriv->ctl,
704 &ctrlpriv->ctl_tdsk_wrap);
705#endif
706 return 0;
707}
708
709static struct of_device_id caam_match[] = {
710 {
Kim Phillips54e198d2011-03-23 21:15:44 +0800711 .compatible = "fsl,sec-v4.0",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800712 },
Shengzhou Liua0ea0f62012-03-21 14:09:10 +0800713 {
714 .compatible = "fsl,sec4.0",
715 },
Kim Phillips8e8ec592011-03-13 16:54:26 +0800716 {},
717};
718MODULE_DEVICE_TABLE(of, caam_match);
719
Kim Phillips2930d492011-05-14 22:07:55 -0500720static struct platform_driver caam_driver = {
Kim Phillips8e8ec592011-03-13 16:54:26 +0800721 .driver = {
722 .name = "caam",
Kim Phillips8e8ec592011-03-13 16:54:26 +0800723 .of_match_table = caam_match,
724 },
725 .probe = caam_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800726 .remove = caam_remove,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800727};
728
Axel Lin741e8c22011-11-26 21:26:19 +0800729module_platform_driver(caam_driver);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800730
731MODULE_LICENSE("GPL");
732MODULE_DESCRIPTION("FSL CAAM request backend");
733MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");