Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * Support for Marvell's Cryptographic Engine and Security Accelerator (CESA)
3 * that can be found on the following platform: Orion, Kirkwood, Armada. This
4 * driver supports the TDMA engine on platforms on which it is available.
5 *
6 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Author: Arnaud Ebalard <arno@natisbad.org>
8 *
9 * This work is based on an initial version written by
10 * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 */
16
17#include <linux/delay.h>
18#include <linux/genalloc.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/kthread.h>
22#include <linux/mbus.h>
23#include <linux/platform_device.h>
24#include <linux/scatterlist.h>
25#include <linux/slab.h>
26#include <linux/module.h>
27#include <linux/clk.h>
28#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/of_irq.h>
31
32#include "cesa.h"
33
34/* Limit of the crypto queue before reaching the backlog */
35#define CESA_CRYPTO_DEFAULT_MAX_QLEN 128
36
37struct mv_cesa_dev *cesa_dev;
38
39struct crypto_async_request *
40mv_cesa_dequeue_req_locked(struct mv_cesa_engine *engine,
41 struct crypto_async_request **backlog)
42{
43 struct crypto_async_request *req;
44
45 *backlog = crypto_get_backlog(&engine->queue);
46 req = crypto_dequeue_request(&engine->queue);
47
48 if (!req)
49 return NULL;
50
51 return req;
52}
53
54static void mv_cesa_rearm_engine(struct mv_cesa_engine *engine)
55{
56 struct crypto_async_request *req = NULL, *backlog = NULL;
57 struct mv_cesa_ctx *ctx;
58
59
60 spin_lock_bh(&engine->lock);
61 if (!engine->req) {
62 req = mv_cesa_dequeue_req_locked(engine, &backlog);
63 engine->req = req;
64 }
65 spin_unlock_bh(&engine->lock);
66
67 if (!req)
68 return;
69
70 if (backlog)
71 backlog->complete(backlog, -EINPROGRESS);
72
73 ctx = crypto_tfm_ctx(req->tfm);
74 ctx->ops->step(req);
75}
76
77static int mv_cesa_std_process(struct mv_cesa_engine *engine, u32 status)
78{
79 struct crypto_async_request *req;
80 struct mv_cesa_ctx *ctx;
81 int res;
82
83 req = engine->req;
84 ctx = crypto_tfm_ctx(req->tfm);
85 res = ctx->ops->process(req, status);
86
87 if (res == 0) {
88 ctx->ops->complete(req);
89 mv_cesa_engine_enqueue_complete_request(engine, req);
90 } else if (res == -EINPROGRESS) {
91 ctx->ops->step(req);
92 }
93
94 return res;
95}
96
97static int mv_cesa_int_process(struct mv_cesa_engine *engine, u32 status)
98{
99 if (engine->chain.first && engine->chain.last)
100 return mv_cesa_tdma_process(engine, status);
101
102 return mv_cesa_std_process(engine, status);
103}
104
105static inline void
106mv_cesa_complete_req(struct mv_cesa_ctx *ctx, struct crypto_async_request *req,
107 int res)
108{
109 ctx->ops->cleanup(req);
110 local_bh_disable();
111 req->complete(req, res);
112 local_bh_enable();
113}
114
115static irqreturn_t mv_cesa_int(int irq, void *priv)
116{
117 struct mv_cesa_engine *engine = priv;
118 struct crypto_async_request *req;
119 struct mv_cesa_ctx *ctx;
120 u32 status, mask;
121 irqreturn_t ret = IRQ_NONE;
122
123 while (true) {
124 int res;
125
126 mask = mv_cesa_get_int_mask(engine);
127 status = readl(engine->regs + CESA_SA_INT_STATUS);
128
129 if (!(status & mask))
130 break;
131
132 /*
133 * TODO: avoid clearing the FPGA_INT_STATUS if this not
134 * relevant on some platforms.
135 */
136 writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS);
137 writel(~status, engine->regs + CESA_SA_INT_STATUS);
138
139 /* Process fetched requests */
140 res = mv_cesa_int_process(engine, status & mask);
141 ret = IRQ_HANDLED;
142
143 spin_lock_bh(&engine->lock);
144 req = engine->req;
145 if (res != -EINPROGRESS)
146 engine->req = NULL;
147 spin_unlock_bh(&engine->lock);
148
149 ctx = crypto_tfm_ctx(req->tfm);
150
151 if (res && res != -EINPROGRESS)
152 mv_cesa_complete_req(ctx, req, res);
153
154 /* Launch the next pending request */
155 mv_cesa_rearm_engine(engine);
156
157 /* Iterate over the complete queue */
158 while (true) {
159 req = mv_cesa_engine_dequeue_complete_request(engine);
160 if (!req)
161 break;
162
163 ctx = crypto_tfm_ctx(req->tfm);
164 mv_cesa_complete_req(ctx, req, 0);
165 }
166 }
167
168 return ret;
169}
170
171int mv_cesa_queue_req(struct crypto_async_request *req,
172 struct mv_cesa_req *creq)
173{
174 int ret;
175 struct mv_cesa_engine *engine = creq->engine;
176
177 spin_lock_bh(&engine->lock);
178 ret = crypto_enqueue_request(&engine->queue, req);
179 if ((mv_cesa_req_get_type(creq) == CESA_DMA_REQ) &&
180 (ret == -EINPROGRESS || ret == -EBUSY))
181 mv_cesa_tdma_chain(engine, creq);
182 spin_unlock_bh(&engine->lock);
183
184 if (ret != -EINPROGRESS)
185 return ret;
186
187 mv_cesa_rearm_engine(engine);
188
189 return -EINPROGRESS;
190}
191
192static int mv_cesa_add_algs(struct mv_cesa_dev *cesa)
193{
194 int ret;
195 int i, j;
196
197 for (i = 0; i < cesa->caps->ncipher_algs; i++) {
198 ret = crypto_register_skcipher(cesa->caps->cipher_algs[i]);
199 if (ret)
200 goto err_unregister_crypto;
201 }
202
203 for (i = 0; i < cesa->caps->nahash_algs; i++) {
204 ret = crypto_register_ahash(cesa->caps->ahash_algs[i]);
205 if (ret)
206 goto err_unregister_ahash;
207 }
208
209 return 0;
210
211err_unregister_ahash:
212 for (j = 0; j < i; j++)
213 crypto_unregister_ahash(cesa->caps->ahash_algs[j]);
214 i = cesa->caps->ncipher_algs;
215
216err_unregister_crypto:
217 for (j = 0; j < i; j++)
218 crypto_unregister_skcipher(cesa->caps->cipher_algs[j]);
219
220 return ret;
221}
222
223static void mv_cesa_remove_algs(struct mv_cesa_dev *cesa)
224{
225 int i;
226
227 for (i = 0; i < cesa->caps->nahash_algs; i++)
228 crypto_unregister_ahash(cesa->caps->ahash_algs[i]);
229
230 for (i = 0; i < cesa->caps->ncipher_algs; i++)
231 crypto_unregister_skcipher(cesa->caps->cipher_algs[i]);
232}
233
234static struct skcipher_alg *orion_cipher_algs[] = {
235 &mv_cesa_ecb_des_alg,
236 &mv_cesa_cbc_des_alg,
237 &mv_cesa_ecb_des3_ede_alg,
238 &mv_cesa_cbc_des3_ede_alg,
239 &mv_cesa_ecb_aes_alg,
240 &mv_cesa_cbc_aes_alg,
241};
242
243static struct ahash_alg *orion_ahash_algs[] = {
244 &mv_md5_alg,
245 &mv_sha1_alg,
246 &mv_ahmac_md5_alg,
247 &mv_ahmac_sha1_alg,
248};
249
250static struct skcipher_alg *armada_370_cipher_algs[] = {
251 &mv_cesa_ecb_des_alg,
252 &mv_cesa_cbc_des_alg,
253 &mv_cesa_ecb_des3_ede_alg,
254 &mv_cesa_cbc_des3_ede_alg,
255 &mv_cesa_ecb_aes_alg,
256 &mv_cesa_cbc_aes_alg,
257};
258
259static struct ahash_alg *armada_370_ahash_algs[] = {
260 &mv_md5_alg,
261 &mv_sha1_alg,
262 &mv_sha256_alg,
263 &mv_ahmac_md5_alg,
264 &mv_ahmac_sha1_alg,
265 &mv_ahmac_sha256_alg,
266};
267
268static const struct mv_cesa_caps orion_caps = {
269 .nengines = 1,
270 .cipher_algs = orion_cipher_algs,
271 .ncipher_algs = ARRAY_SIZE(orion_cipher_algs),
272 .ahash_algs = orion_ahash_algs,
273 .nahash_algs = ARRAY_SIZE(orion_ahash_algs),
274 .has_tdma = false,
275};
276
277static const struct mv_cesa_caps kirkwood_caps = {
278 .nengines = 1,
279 .cipher_algs = orion_cipher_algs,
280 .ncipher_algs = ARRAY_SIZE(orion_cipher_algs),
281 .ahash_algs = orion_ahash_algs,
282 .nahash_algs = ARRAY_SIZE(orion_ahash_algs),
283 .has_tdma = true,
284};
285
286static const struct mv_cesa_caps armada_370_caps = {
287 .nengines = 1,
288 .cipher_algs = armada_370_cipher_algs,
289 .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
290 .ahash_algs = armada_370_ahash_algs,
291 .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
292 .has_tdma = true,
293};
294
295static const struct mv_cesa_caps armada_xp_caps = {
296 .nengines = 2,
297 .cipher_algs = armada_370_cipher_algs,
298 .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
299 .ahash_algs = armada_370_ahash_algs,
300 .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
301 .has_tdma = true,
302};
303
304static const struct of_device_id mv_cesa_of_match_table[] = {
305 { .compatible = "marvell,orion-crypto", .data = &orion_caps },
306 { .compatible = "marvell,kirkwood-crypto", .data = &kirkwood_caps },
307 { .compatible = "marvell,dove-crypto", .data = &kirkwood_caps },
308 { .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps },
309 { .compatible = "marvell,armada-xp-crypto", .data = &armada_xp_caps },
310 { .compatible = "marvell,armada-375-crypto", .data = &armada_xp_caps },
311 { .compatible = "marvell,armada-38x-crypto", .data = &armada_xp_caps },
312 {}
313};
314MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table);
315
316static void
317mv_cesa_conf_mbus_windows(struct mv_cesa_engine *engine,
318 const struct mbus_dram_target_info *dram)
319{
320 void __iomem *iobase = engine->regs;
321 int i;
322
323 for (i = 0; i < 4; i++) {
324 writel(0, iobase + CESA_TDMA_WINDOW_CTRL(i));
325 writel(0, iobase + CESA_TDMA_WINDOW_BASE(i));
326 }
327
328 for (i = 0; i < dram->num_cs; i++) {
329 const struct mbus_dram_window *cs = dram->cs + i;
330
331 writel(((cs->size - 1) & 0xffff0000) |
332 (cs->mbus_attr << 8) |
333 (dram->mbus_dram_target_id << 4) | 1,
334 iobase + CESA_TDMA_WINDOW_CTRL(i));
335 writel(cs->base, iobase + CESA_TDMA_WINDOW_BASE(i));
336 }
337}
338
339static int mv_cesa_dev_dma_init(struct mv_cesa_dev *cesa)
340{
341 struct device *dev = cesa->dev;
342 struct mv_cesa_dev_dma *dma;
343
344 if (!cesa->caps->has_tdma)
345 return 0;
346
347 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
348 if (!dma)
349 return -ENOMEM;
350
351 dma->tdma_desc_pool = dmam_pool_create("tdma_desc", dev,
352 sizeof(struct mv_cesa_tdma_desc),
353 16, 0);
354 if (!dma->tdma_desc_pool)
355 return -ENOMEM;
356
357 dma->op_pool = dmam_pool_create("cesa_op", dev,
358 sizeof(struct mv_cesa_op_ctx), 16, 0);
359 if (!dma->op_pool)
360 return -ENOMEM;
361
362 dma->cache_pool = dmam_pool_create("cesa_cache", dev,
363 CESA_MAX_HASH_BLOCK_SIZE, 1, 0);
364 if (!dma->cache_pool)
365 return -ENOMEM;
366
367 dma->padding_pool = dmam_pool_create("cesa_padding", dev, 72, 1, 0);
368 if (!dma->padding_pool)
369 return -ENOMEM;
370
371 cesa->dma = dma;
372
373 return 0;
374}
375
376static int mv_cesa_get_sram(struct platform_device *pdev, int idx)
377{
378 struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
379 struct mv_cesa_engine *engine = &cesa->engines[idx];
380 const char *res_name = "sram";
381 struct resource *res;
382
383 engine->pool = of_gen_pool_get(cesa->dev->of_node,
384 "marvell,crypto-srams", idx);
385 if (engine->pool) {
386 engine->sram = gen_pool_dma_alloc(engine->pool,
387 cesa->sram_size,
388 &engine->sram_dma);
389 if (engine->sram)
390 return 0;
391
392 engine->pool = NULL;
393 return -ENOMEM;
394 }
395
396 if (cesa->caps->nengines > 1) {
397 if (!idx)
398 res_name = "sram0";
399 else
400 res_name = "sram1";
401 }
402
403 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
404 res_name);
405 if (!res || resource_size(res) < cesa->sram_size)
406 return -EINVAL;
407
408 engine->sram = devm_ioremap_resource(cesa->dev, res);
409 if (IS_ERR(engine->sram))
410 return PTR_ERR(engine->sram);
411
412 engine->sram_dma = phys_to_dma(cesa->dev,
413 (phys_addr_t)res->start);
414
415 return 0;
416}
417
418static void mv_cesa_put_sram(struct platform_device *pdev, int idx)
419{
420 struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
421 struct mv_cesa_engine *engine = &cesa->engines[idx];
422
423 if (!engine->pool)
424 return;
425
426 gen_pool_free(engine->pool, (unsigned long)engine->sram,
427 cesa->sram_size);
428}
429
430static int mv_cesa_probe(struct platform_device *pdev)
431{
432 const struct mv_cesa_caps *caps = &orion_caps;
433 const struct mbus_dram_target_info *dram;
434 const struct of_device_id *match;
435 struct device *dev = &pdev->dev;
436 struct mv_cesa_dev *cesa;
437 struct mv_cesa_engine *engines;
438 struct resource *res;
439 int irq, ret, i;
440 u32 sram_size;
441
442 if (cesa_dev) {
443 dev_err(&pdev->dev, "Only one CESA device authorized\n");
444 return -EEXIST;
445 }
446
447 if (dev->of_node) {
448 match = of_match_node(mv_cesa_of_match_table, dev->of_node);
449 if (!match || !match->data)
450 return -ENOTSUPP;
451
452 caps = match->data;
453 }
454
455 cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL);
456 if (!cesa)
457 return -ENOMEM;
458
459 cesa->caps = caps;
460 cesa->dev = dev;
461
462 sram_size = CESA_SA_DEFAULT_SRAM_SIZE;
463 of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size",
464 &sram_size);
465 if (sram_size < CESA_SA_MIN_SRAM_SIZE)
466 sram_size = CESA_SA_MIN_SRAM_SIZE;
467
468 cesa->sram_size = sram_size;
469 cesa->engines = devm_kzalloc(dev, caps->nengines * sizeof(*engines),
470 GFP_KERNEL);
471 if (!cesa->engines)
472 return -ENOMEM;
473
474 spin_lock_init(&cesa->lock);
475
476 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
477 cesa->regs = devm_ioremap_resource(dev, res);
478 if (IS_ERR(cesa->regs))
479 return PTR_ERR(cesa->regs);
480
481 ret = mv_cesa_dev_dma_init(cesa);
482 if (ret)
483 return ret;
484
485 dram = mv_mbus_dram_info_nooverlap();
486
487 platform_set_drvdata(pdev, cesa);
488
489 for (i = 0; i < caps->nengines; i++) {
490 struct mv_cesa_engine *engine = &cesa->engines[i];
491 char res_name[7];
492
493 engine->id = i;
494 spin_lock_init(&engine->lock);
495
496 ret = mv_cesa_get_sram(pdev, i);
497 if (ret)
498 goto err_cleanup;
499
500 irq = platform_get_irq(pdev, i);
501 if (irq < 0) {
502 ret = irq;
503 goto err_cleanup;
504 }
505
506 /*
507 * Not all platforms can gate the CESA clocks: do not complain
508 * if the clock does not exist.
509 */
510 snprintf(res_name, sizeof(res_name), "cesa%d", i);
511 engine->clk = devm_clk_get(dev, res_name);
512 if (IS_ERR(engine->clk)) {
513 engine->clk = devm_clk_get(dev, NULL);
514 if (IS_ERR(engine->clk))
515 engine->clk = NULL;
516 }
517
518 snprintf(res_name, sizeof(res_name), "cesaz%d", i);
519 engine->zclk = devm_clk_get(dev, res_name);
520 if (IS_ERR(engine->zclk))
521 engine->zclk = NULL;
522
523 ret = clk_prepare_enable(engine->clk);
524 if (ret)
525 goto err_cleanup;
526
527 ret = clk_prepare_enable(engine->zclk);
528 if (ret)
529 goto err_cleanup;
530
531 engine->regs = cesa->regs + CESA_ENGINE_OFF(i);
532
533 if (dram && cesa->caps->has_tdma)
534 mv_cesa_conf_mbus_windows(engine, dram);
535
536 writel(0, engine->regs + CESA_SA_INT_STATUS);
537 writel(CESA_SA_CFG_STOP_DIG_ERR,
538 engine->regs + CESA_SA_CFG);
539 writel(engine->sram_dma & CESA_SA_SRAM_MSK,
540 engine->regs + CESA_SA_DESC_P0);
541
542 ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int,
543 IRQF_ONESHOT,
544 dev_name(&pdev->dev),
545 engine);
546 if (ret)
547 goto err_cleanup;
548
549 crypto_init_queue(&engine->queue, CESA_CRYPTO_DEFAULT_MAX_QLEN);
550 atomic_set(&engine->load, 0);
551 INIT_LIST_HEAD(&engine->complete_queue);
552 }
553
554 cesa_dev = cesa;
555
556 ret = mv_cesa_add_algs(cesa);
557 if (ret) {
558 cesa_dev = NULL;
559 goto err_cleanup;
560 }
561
562 dev_info(dev, "CESA device successfully registered\n");
563
564 return 0;
565
566err_cleanup:
567 for (i = 0; i < caps->nengines; i++) {
568 clk_disable_unprepare(cesa->engines[i].zclk);
569 clk_disable_unprepare(cesa->engines[i].clk);
570 mv_cesa_put_sram(pdev, i);
571 }
572
573 return ret;
574}
575
576static int mv_cesa_remove(struct platform_device *pdev)
577{
578 struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
579 int i;
580
581 mv_cesa_remove_algs(cesa);
582
583 for (i = 0; i < cesa->caps->nengines; i++) {
584 clk_disable_unprepare(cesa->engines[i].zclk);
585 clk_disable_unprepare(cesa->engines[i].clk);
586 mv_cesa_put_sram(pdev, i);
587 }
588
589 return 0;
590}
591
592static const struct platform_device_id mv_cesa_plat_id_table[] = {
593 { .name = "mv_crypto" },
594 { /* sentinel */ },
595};
596MODULE_DEVICE_TABLE(platform, mv_cesa_plat_id_table);
597
598static struct platform_driver marvell_cesa = {
599 .probe = mv_cesa_probe,
600 .remove = mv_cesa_remove,
601 .id_table = mv_cesa_plat_id_table,
602 .driver = {
603 .name = "marvell-cesa",
604 .of_match_table = mv_cesa_of_match_table,
605 },
606};
607module_platform_driver(marvell_cesa);
608
609MODULE_ALIAS("platform:mv_crypto");
610MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
611MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>");
612MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
613MODULE_LICENSE("GPL v2");