]> Pileus Git - ~andy/linux/blob - drivers/crypto/caam/ctrl.c
crypto: caam - only query h/w in job ring dequeue path
[~andy/linux] / drivers / crypto / caam / ctrl.c
1 /*
2  * CAAM control-plane driver backend
3  * Controller-level driver, kernel property detection, initialization
4  *
5  * Copyright 2008-2012 Freescale Semiconductor, Inc.
6  */
7
8 #include "compat.h"
9 #include "regs.h"
10 #include "intern.h"
11 #include "jr.h"
12 #include "desc_constr.h"
13 #include "error.h"
14
15 static int caam_remove(struct platform_device *pdev)
16 {
17         struct device *ctrldev;
18         struct caam_drv_private *ctrlpriv;
19         struct caam_drv_private_jr *jrpriv;
20         struct caam_full __iomem *topregs;
21         int ring, ret = 0;
22
23         ctrldev = &pdev->dev;
24         ctrlpriv = dev_get_drvdata(ctrldev);
25         topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
26
27         /* shut down JobRs */
28         for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) {
29                 ret |= caam_jr_shutdown(ctrlpriv->jrdev[ring]);
30                 jrpriv = dev_get_drvdata(ctrlpriv->jrdev[ring]);
31                 irq_dispose_mapping(jrpriv->irq);
32         }
33
34         /* Shut down debug views */
35 #ifdef CONFIG_DEBUG_FS
36         debugfs_remove_recursive(ctrlpriv->dfs_root);
37 #endif
38
39         /* Unmap controller region */
40         iounmap(&topregs->ctrl);
41
42         kfree(ctrlpriv->jrdev);
43         kfree(ctrlpriv);
44
45         return ret;
46 }
47
48 /*
49  * Descriptor to instantiate RNG State Handle 0 in normal mode and
50  * load the JDKEK, TDKEK and TDSK registers
51  */
52 static void build_instantiation_desc(u32 *desc)
53 {
54         u32 *jump_cmd;
55
56         init_job_desc(desc, 0);
57
58         /* INIT RNG in non-test mode */
59         append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
60                          OP_ALG_AS_INIT);
61
62         /* wait for done */
63         jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
64         set_jump_tgt_here(desc, jump_cmd);
65
66         /*
67          * load 1 to clear written reg:
68          * resets the done interrrupt and returns the RNG to idle.
69          */
70         append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
71
72         /* generate secure keys (non-test) */
73         append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
74                          OP_ALG_RNG4_SK);
75 }
76
77 struct instantiate_result {
78         struct completion completion;
79         int err;
80 };
81
82 static void rng4_init_done(struct device *dev, u32 *desc, u32 err,
83                            void *context)
84 {
85         struct instantiate_result *instantiation = context;
86
87         if (err) {
88                 char tmp[CAAM_ERROR_STR_MAX];
89
90                 dev_err(dev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
91         }
92
93         instantiation->err = err;
94         complete(&instantiation->completion);
95 }
96
97 static int instantiate_rng(struct device *jrdev)
98 {
99         struct instantiate_result instantiation;
100
101         dma_addr_t desc_dma;
102         u32 *desc;
103         int ret;
104
105         desc = kmalloc(CAAM_CMD_SZ * 6, GFP_KERNEL | GFP_DMA);
106         if (!desc) {
107                 dev_err(jrdev, "cannot allocate RNG init descriptor memory\n");
108                 return -ENOMEM;
109         }
110
111         build_instantiation_desc(desc);
112         desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), DMA_TO_DEVICE);
113         init_completion(&instantiation.completion);
114         ret = caam_jr_enqueue(jrdev, desc, rng4_init_done, &instantiation);
115         if (!ret) {
116                 wait_for_completion_interruptible(&instantiation.completion);
117                 ret = instantiation.err;
118                 if (ret)
119                         dev_err(jrdev, "unable to instantiate RNG\n");
120         }
121
122         dma_unmap_single(jrdev, desc_dma, desc_bytes(desc), DMA_TO_DEVICE);
123
124         kfree(desc);
125
126         return ret;
127 }
128
129 /*
130  * By default, the TRNG runs for 200 clocks per sample;
131  * 800 clocks per sample generates better entropy.
132  */
133 static void kick_trng(struct platform_device *pdev)
134 {
135         struct device *ctrldev = &pdev->dev;
136         struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
137         struct caam_full __iomem *topregs;
138         struct rng4tst __iomem *r4tst;
139         u32 val;
140
141         topregs = (struct caam_full __iomem *)ctrlpriv->ctrl;
142         r4tst = &topregs->ctrl.r4tst[0];
143
144         /* put RNG4 into program mode */
145         setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
146         /* 800 clocks per sample */
147         val = rd_reg32(&r4tst->rtsdctl);
148         val = (val & ~RTSDCTL_ENT_DLY_MASK) | (800 << RTSDCTL_ENT_DLY_SHIFT);
149         wr_reg32(&r4tst->rtsdctl, val);
150         /* min. freq. count */
151         wr_reg32(&r4tst->rtfrqmin, 400);
152         /* max. freq. count */
153         wr_reg32(&r4tst->rtfrqmax, 6400);
154         /* put RNG4 into run mode */
155         clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
156 }
157
158 /* Probe routine for CAAM top (controller) level */
159 static int caam_probe(struct platform_device *pdev)
160 {
161         int ret, ring, rspec;
162         struct device *dev;
163         struct device_node *nprop, *np;
164         struct caam_ctrl __iomem *ctrl;
165         struct caam_full __iomem *topregs;
166         struct caam_drv_private *ctrlpriv;
167 #ifdef CONFIG_DEBUG_FS
168         struct caam_perfmon *perfmon;
169 #endif
170
171         ctrlpriv = kzalloc(sizeof(struct caam_drv_private), GFP_KERNEL);
172         if (!ctrlpriv)
173                 return -ENOMEM;
174
175         dev = &pdev->dev;
176         dev_set_drvdata(dev, ctrlpriv);
177         ctrlpriv->pdev = pdev;
178         nprop = pdev->dev.of_node;
179
180         /* Get configuration properties from device tree */
181         /* First, get register page */
182         ctrl = of_iomap(nprop, 0);
183         if (ctrl == NULL) {
184                 dev_err(dev, "caam: of_iomap() failed\n");
185                 return -ENOMEM;
186         }
187         ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl;
188
189         /* topregs used to derive pointers to CAAM sub-blocks only */
190         topregs = (struct caam_full __iomem *)ctrl;
191
192         /* Get the IRQ of the controller (for security violations only) */
193         ctrlpriv->secvio_irq = of_irq_to_resource(nprop, 0, NULL);
194
195         /*
196          * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
197          * long pointers in master configuration register
198          */
199         setbits32(&topregs->ctrl.mcr, MCFGR_WDENABLE |
200                   (sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
201
202         if (sizeof(dma_addr_t) == sizeof(u64))
203                 if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
204                         dma_set_mask(dev, DMA_BIT_MASK(40));
205                 else
206                         dma_set_mask(dev, DMA_BIT_MASK(36));
207         else
208                 dma_set_mask(dev, DMA_BIT_MASK(32));
209
210         /*
211          * Detect and enable JobRs
212          * First, find out how many ring spec'ed, allocate references
213          * for all, then go probe each one.
214          */
215         rspec = 0;
216         for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring")
217                 rspec++;
218         if (!rspec) {
219                 /* for backward compatible with device trees */
220                 for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring")
221                         rspec++;
222         }
223
224         ctrlpriv->jrdev = kzalloc(sizeof(struct device *) * rspec, GFP_KERNEL);
225         if (ctrlpriv->jrdev == NULL) {
226                 iounmap(&topregs->ctrl);
227                 return -ENOMEM;
228         }
229
230         ring = 0;
231         ctrlpriv->total_jobrs = 0;
232         for_each_compatible_node(np, NULL, "fsl,sec-v4.0-job-ring") {
233                 caam_jr_probe(pdev, np, ring);
234                 ctrlpriv->total_jobrs++;
235                 ring++;
236         }
237         if (!ring) {
238                 for_each_compatible_node(np, NULL, "fsl,sec4.0-job-ring") {
239                         caam_jr_probe(pdev, np, ring);
240                         ctrlpriv->total_jobrs++;
241                         ring++;
242                 }
243         }
244
245         /* Check to see if QI present. If so, enable */
246         ctrlpriv->qi_present = !!(rd_reg64(&topregs->ctrl.perfmon.comp_parms) &
247                                   CTPR_QI_MASK);
248         if (ctrlpriv->qi_present) {
249                 ctrlpriv->qi = (struct caam_queue_if __force *)&topregs->qi;
250                 /* This is all that's required to physically enable QI */
251                 wr_reg32(&topregs->qi.qi_control_lo, QICTL_DQEN);
252         }
253
254         /* If no QI and no rings specified, quit and go home */
255         if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
256                 dev_err(dev, "no queues configured, terminating\n");
257                 caam_remove(pdev);
258                 return -ENOMEM;
259         }
260
261         /*
262          * RNG4 based SECs (v5+) need special initialization prior
263          * to executing any descriptors
264          */
265         if (of_device_is_compatible(nprop, "fsl,sec-v5.0")) {
266                 kick_trng(pdev);
267                 ret = instantiate_rng(ctrlpriv->jrdev[0]);
268                 if (ret) {
269                         caam_remove(pdev);
270                         return ret;
271                 }
272         }
273
274         /* NOTE: RTIC detection ought to go here, around Si time */
275
276         /* Initialize queue allocator lock */
277         spin_lock_init(&ctrlpriv->jr_alloc_lock);
278
279         /* Report "alive" for developer to see */
280         dev_info(dev, "device ID = 0x%016llx\n",
281                  rd_reg64(&topregs->ctrl.perfmon.caam_id));
282         dev_info(dev, "job rings = %d, qi = %d\n",
283                  ctrlpriv->total_jobrs, ctrlpriv->qi_present);
284
285 #ifdef CONFIG_DEBUG_FS
286         /*
287          * FIXME: needs better naming distinction, as some amalgamation of
288          * "caam" and nprop->full_name. The OF name isn't distinctive,
289          * but does separate instances
290          */
291         perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
292
293         ctrlpriv->dfs_root = debugfs_create_dir("caam", NULL);
294         ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
295
296         /* Controller-level - performance monitor counters */
297         ctrlpriv->ctl_rq_dequeued =
298                 debugfs_create_u64("rq_dequeued",
299                                    S_IRUSR | S_IRGRP | S_IROTH,
300                                    ctrlpriv->ctl, &perfmon->req_dequeued);
301         ctrlpriv->ctl_ob_enc_req =
302                 debugfs_create_u64("ob_rq_encrypted",
303                                    S_IRUSR | S_IRGRP | S_IROTH,
304                                    ctrlpriv->ctl, &perfmon->ob_enc_req);
305         ctrlpriv->ctl_ib_dec_req =
306                 debugfs_create_u64("ib_rq_decrypted",
307                                    S_IRUSR | S_IRGRP | S_IROTH,
308                                    ctrlpriv->ctl, &perfmon->ib_dec_req);
309         ctrlpriv->ctl_ob_enc_bytes =
310                 debugfs_create_u64("ob_bytes_encrypted",
311                                    S_IRUSR | S_IRGRP | S_IROTH,
312                                    ctrlpriv->ctl, &perfmon->ob_enc_bytes);
313         ctrlpriv->ctl_ob_prot_bytes =
314                 debugfs_create_u64("ob_bytes_protected",
315                                    S_IRUSR | S_IRGRP | S_IROTH,
316                                    ctrlpriv->ctl, &perfmon->ob_prot_bytes);
317         ctrlpriv->ctl_ib_dec_bytes =
318                 debugfs_create_u64("ib_bytes_decrypted",
319                                    S_IRUSR | S_IRGRP | S_IROTH,
320                                    ctrlpriv->ctl, &perfmon->ib_dec_bytes);
321         ctrlpriv->ctl_ib_valid_bytes =
322                 debugfs_create_u64("ib_bytes_validated",
323                                    S_IRUSR | S_IRGRP | S_IROTH,
324                                    ctrlpriv->ctl, &perfmon->ib_valid_bytes);
325
326         /* Controller level - global status values */
327         ctrlpriv->ctl_faultaddr =
328                 debugfs_create_u64("fault_addr",
329                                    S_IRUSR | S_IRGRP | S_IROTH,
330                                    ctrlpriv->ctl, &perfmon->faultaddr);
331         ctrlpriv->ctl_faultdetail =
332                 debugfs_create_u32("fault_detail",
333                                    S_IRUSR | S_IRGRP | S_IROTH,
334                                    ctrlpriv->ctl, &perfmon->faultdetail);
335         ctrlpriv->ctl_faultstatus =
336                 debugfs_create_u32("fault_status",
337                                    S_IRUSR | S_IRGRP | S_IROTH,
338                                    ctrlpriv->ctl, &perfmon->status);
339
340         /* Internal covering keys (useful in non-secure mode only) */
341         ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];
342         ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
343         ctrlpriv->ctl_kek = debugfs_create_blob("kek",
344                                                 S_IRUSR |
345                                                 S_IRGRP | S_IROTH,
346                                                 ctrlpriv->ctl,
347                                                 &ctrlpriv->ctl_kek_wrap);
348
349         ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0];
350         ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
351         ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
352                                                  S_IRUSR |
353                                                  S_IRGRP | S_IROTH,
354                                                  ctrlpriv->ctl,
355                                                  &ctrlpriv->ctl_tkek_wrap);
356
357         ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0];
358         ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
359         ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
360                                                  S_IRUSR |
361                                                  S_IRGRP | S_IROTH,
362                                                  ctrlpriv->ctl,
363                                                  &ctrlpriv->ctl_tdsk_wrap);
364 #endif
365         return 0;
366 }
367
368 static struct of_device_id caam_match[] = {
369         {
370                 .compatible = "fsl,sec-v4.0",
371         },
372         {
373                 .compatible = "fsl,sec4.0",
374         },
375         {},
376 };
377 MODULE_DEVICE_TABLE(of, caam_match);
378
379 static struct platform_driver caam_driver = {
380         .driver = {
381                 .name = "caam",
382                 .owner = THIS_MODULE,
383                 .of_match_table = caam_match,
384         },
385         .probe       = caam_probe,
386         .remove      = __devexit_p(caam_remove),
387 };
388
389 module_platform_driver(caam_driver);
390
391 MODULE_LICENSE("GPL");
392 MODULE_DESCRIPTION("FSL CAAM request backend");
393 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");