blob: 19adf1c7faa2316c6f4791806c0683ee4b925856 [file] [log] [blame]
Varun Wadekarf1df57d2012-01-13 16:38:37 +11001/*
2 * drivers/crypto/tegra-aes.c
3 *
4 * Driver for NVIDIA Tegra AES hardware engine residing inside the
5 * Bit Stream Engine for Video (BSEV) hardware block.
6 *
7 * The programming sequence for this engine is with the help
8 * of commands which travel via a command queue residing between the
9 * CPU and the BSEV block. The BSEV engine has an internal RAM (VRAM)
10 * where the final input plaintext, keys and the IV have to be copied
11 * before starting the encrypt/decrypt operation.
12 *
13 * Copyright (c) 2010, NVIDIA Corporation.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 * more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 */
29
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/errno.h>
33#include <linux/kernel.h>
34#include <linux/clk.h>
35#include <linux/platform_device.h>
36#include <linux/scatterlist.h>
37#include <linux/dma-mapping.h>
38#include <linux/io.h>
39#include <linux/mutex.h>
40#include <linux/interrupt.h>
41#include <linux/completion.h>
42#include <linux/workqueue.h>
43
44#include <mach/clk.h>
45
46#include <crypto/scatterwalk.h>
47#include <crypto/aes.h>
48#include <crypto/internal/rng.h>
49
50#include "tegra-aes.h"
51
52#define FLAGS_MODE_MASK 0x00FF
53#define FLAGS_ENCRYPT BIT(0)
54#define FLAGS_CBC BIT(1)
55#define FLAGS_GIV BIT(2)
56#define FLAGS_RNG BIT(3)
57#define FLAGS_OFB BIT(4)
58#define FLAGS_NEW_KEY BIT(5)
59#define FLAGS_NEW_IV BIT(6)
60#define FLAGS_INIT BIT(7)
61#define FLAGS_FAST BIT(8)
62#define FLAGS_BUSY 9
63
64/*
65 * Defines AES engine Max process bytes size in one go, which takes 1 msec.
66 * AES engine spends about 176 cycles/16-bytes or 11 cycles/byte
67 * The duration CPU can use the BSE to 1 msec, then the number of available
68 * cycles of AVP/BSE is 216K. In this duration, AES can process 216/11 ~= 19KB
69 * Based on this AES_HW_DMA_BUFFER_SIZE_BYTES is configured to 16KB.
70 */
71#define AES_HW_DMA_BUFFER_SIZE_BYTES 0x4000
72
73/*
74 * The key table length is 64 bytes
75 * (This includes first upto 32 bytes key + 16 bytes original initial vector
76 * and 16 bytes updated initial vector)
77 */
78#define AES_HW_KEY_TABLE_LENGTH_BYTES 64
79
80/*
81 * The memory being used is divides as follows:
82 * 1. Key - 32 bytes
83 * 2. Original IV - 16 bytes
84 * 3. Updated IV - 16 bytes
85 * 4. Key schedule - 256 bytes
86 *
87 * 1+2+3 constitute the hw key table.
88 */
89#define AES_HW_IV_SIZE 16
90#define AES_HW_KEYSCHEDULE_LEN 256
91#define AES_IVKEY_SIZE (AES_HW_KEY_TABLE_LENGTH_BYTES + AES_HW_KEYSCHEDULE_LEN)
92
93/* Define commands required for AES operation */
94enum {
95 CMD_BLKSTARTENGINE = 0x0E,
96 CMD_DMASETUP = 0x10,
97 CMD_DMACOMPLETE = 0x11,
98 CMD_SETTABLE = 0x15,
99 CMD_MEMDMAVD = 0x22,
100};
101
102/* Define sub-commands */
103enum {
104 SUBCMD_VRAM_SEL = 0x1,
105 SUBCMD_CRYPTO_TABLE_SEL = 0x3,
106 SUBCMD_KEY_TABLE_SEL = 0x8,
107};
108
109/* memdma_vd command */
110#define MEMDMA_DIR_DTOVRAM 0 /* sdram -> vram */
111#define MEMDMA_DIR_VTODRAM 1 /* vram -> sdram */
112#define MEMDMA_DIR_SHIFT 25
113#define MEMDMA_NUM_WORDS_SHIFT 12
114
115/* command queue bit shifts */
116enum {
117 CMDQ_KEYTABLEADDR_SHIFT = 0,
118 CMDQ_KEYTABLEID_SHIFT = 17,
119 CMDQ_VRAMSEL_SHIFT = 23,
120 CMDQ_TABLESEL_SHIFT = 24,
121 CMDQ_OPCODE_SHIFT = 26,
122};
123
124/*
125 * The secure key slot contains a unique secure key generated
126 * and loaded by the bootloader. This slot is marked as non-accessible
127 * to the kernel.
128 */
129#define SSK_SLOT_NUM 4
130
131#define AES_NR_KEYSLOTS 8
132#define TEGRA_AES_QUEUE_LENGTH 50
133#define DEFAULT_RNG_BLK_SZ 16
134
135/* The command queue depth */
136#define AES_HW_MAX_ICQ_LENGTH 5
137
138struct tegra_aes_slot {
139 struct list_head node;
140 int slot_num;
141};
142
143static struct tegra_aes_slot ssk = {
144 .slot_num = SSK_SLOT_NUM,
145};
146
147struct tegra_aes_reqctx {
148 unsigned long mode;
149};
150
151struct tegra_aes_dev {
152 struct device *dev;
153 void __iomem *io_base;
154 dma_addr_t ivkey_phys_base;
155 void __iomem *ivkey_base;
156 struct clk *aes_clk;
157 struct tegra_aes_ctx *ctx;
158 int irq;
159 unsigned long flags;
160 struct completion op_complete;
161 u32 *buf_in;
162 dma_addr_t dma_buf_in;
163 u32 *buf_out;
164 dma_addr_t dma_buf_out;
165 u8 *iv;
166 u8 dt[DEFAULT_RNG_BLK_SZ];
167 int ivlen;
168 u64 ctr;
169 spinlock_t lock;
170 struct crypto_queue queue;
171 struct tegra_aes_slot *slots;
172 struct ablkcipher_request *req;
173 size_t total;
174 struct scatterlist *in_sg;
175 size_t in_offset;
176 struct scatterlist *out_sg;
177 size_t out_offset;
178};
179
180static struct tegra_aes_dev *aes_dev;
181
182struct tegra_aes_ctx {
183 struct tegra_aes_dev *dd;
184 unsigned long flags;
185 struct tegra_aes_slot *slot;
186 u8 key[AES_MAX_KEY_SIZE];
187 size_t keylen;
188};
189
190static struct tegra_aes_ctx rng_ctx = {
191 .flags = FLAGS_NEW_KEY,
192 .keylen = AES_KEYSIZE_128,
193};
194
195/* keep registered devices data here */
196static struct list_head dev_list;
197static DEFINE_SPINLOCK(list_lock);
198static DEFINE_MUTEX(aes_lock);
199
200static void aes_workqueue_handler(struct work_struct *work);
201static DECLARE_WORK(aes_work, aes_workqueue_handler);
202static struct workqueue_struct *aes_wq;
203
204extern unsigned long long tegra_chip_uid(void);
205
206static inline u32 aes_readl(struct tegra_aes_dev *dd, u32 offset)
207{
208 return readl(dd->io_base + offset);
209}
210
211static inline void aes_writel(struct tegra_aes_dev *dd, u32 val, u32 offset)
212{
213 writel(val, dd->io_base + offset);
214}
215
216static int aes_start_crypt(struct tegra_aes_dev *dd, u32 in_addr, u32 out_addr,
217 int nblocks, int mode, bool upd_iv)
218{
219 u32 cmdq[AES_HW_MAX_ICQ_LENGTH];
220 int i, eng_busy, icq_empty, ret;
221 u32 value;
222
223 /* reset all the interrupt bits */
224 aes_writel(dd, 0xFFFFFFFF, TEGRA_AES_INTR_STATUS);
225
226 /* enable error, dma xfer complete interrupts */
227 aes_writel(dd, 0x33, TEGRA_AES_INT_ENB);
228
229 cmdq[0] = CMD_DMASETUP << CMDQ_OPCODE_SHIFT;
230 cmdq[1] = in_addr;
231 cmdq[2] = CMD_BLKSTARTENGINE << CMDQ_OPCODE_SHIFT | (nblocks-1);
232 cmdq[3] = CMD_DMACOMPLETE << CMDQ_OPCODE_SHIFT;
233
234 value = aes_readl(dd, TEGRA_AES_CMDQUE_CONTROL);
235 /* access SDRAM through AHB */
236 value &= ~TEGRA_AES_CMDQ_CTRL_SRC_STM_SEL_FIELD;
237 value &= ~TEGRA_AES_CMDQ_CTRL_DST_STM_SEL_FIELD;
238 value |= TEGRA_AES_CMDQ_CTRL_SRC_STM_SEL_FIELD |
239 TEGRA_AES_CMDQ_CTRL_DST_STM_SEL_FIELD |
240 TEGRA_AES_CMDQ_CTRL_ICMDQEN_FIELD;
241 aes_writel(dd, value, TEGRA_AES_CMDQUE_CONTROL);
242 dev_dbg(dd->dev, "cmd_q_ctrl=0x%x", value);
243
244 value = (0x1 << TEGRA_AES_SECURE_INPUT_ALG_SEL_SHIFT) |
245 ((dd->ctx->keylen * 8) <<
246 TEGRA_AES_SECURE_INPUT_KEY_LEN_SHIFT) |
247 ((u32)upd_iv << TEGRA_AES_SECURE_IV_SELECT_SHIFT);
248
249 if (mode & FLAGS_CBC) {
250 value |= ((((mode & FLAGS_ENCRYPT) ? 2 : 3)
251 << TEGRA_AES_SECURE_XOR_POS_SHIFT) |
252 (((mode & FLAGS_ENCRYPT) ? 2 : 3)
253 << TEGRA_AES_SECURE_VCTRAM_SEL_SHIFT) |
254 ((mode & FLAGS_ENCRYPT) ? 1 : 0)
255 << TEGRA_AES_SECURE_CORE_SEL_SHIFT);
256 } else if (mode & FLAGS_OFB) {
257 value |= ((TEGRA_AES_SECURE_XOR_POS_FIELD) |
258 (2 << TEGRA_AES_SECURE_INPUT_SEL_SHIFT) |
259 (TEGRA_AES_SECURE_CORE_SEL_FIELD));
260 } else if (mode & FLAGS_RNG) {
261 value |= (((mode & FLAGS_ENCRYPT) ? 1 : 0)
262 << TEGRA_AES_SECURE_CORE_SEL_SHIFT |
263 TEGRA_AES_SECURE_RNG_ENB_FIELD);
264 } else {
265 value |= (((mode & FLAGS_ENCRYPT) ? 1 : 0)
266 << TEGRA_AES_SECURE_CORE_SEL_SHIFT);
267 }
268
269 dev_dbg(dd->dev, "secure_in_sel=0x%x", value);
270 aes_writel(dd, value, TEGRA_AES_SECURE_INPUT_SELECT);
271
272 aes_writel(dd, out_addr, TEGRA_AES_SECURE_DEST_ADDR);
273 INIT_COMPLETION(dd->op_complete);
274
275 for (i = 0; i < AES_HW_MAX_ICQ_LENGTH - 1; i++) {
276 do {
277 value = aes_readl(dd, TEGRA_AES_INTR_STATUS);
278 eng_busy = value & TEGRA_AES_ENGINE_BUSY_FIELD;
279 icq_empty = value & TEGRA_AES_ICQ_EMPTY_FIELD;
280 } while (eng_busy & (!icq_empty));
281 aes_writel(dd, cmdq[i], TEGRA_AES_ICMDQUE_WR);
282 }
283
284 ret = wait_for_completion_timeout(&dd->op_complete,
285 msecs_to_jiffies(150));
286 if (ret == 0) {
287 dev_err(dd->dev, "timed out (0x%x)\n",
288 aes_readl(dd, TEGRA_AES_INTR_STATUS));
289 return -ETIMEDOUT;
290 }
291
292 aes_writel(dd, cmdq[AES_HW_MAX_ICQ_LENGTH - 1], TEGRA_AES_ICMDQUE_WR);
293 return 0;
294}
295
296static void aes_release_key_slot(struct tegra_aes_slot *slot)
297{
298 if (slot->slot_num == SSK_SLOT_NUM)
299 return;
300
301 spin_lock(&list_lock);
302 list_add_tail(&slot->node, &dev_list);
303 slot = NULL;
304 spin_unlock(&list_lock);
305}
306
307static struct tegra_aes_slot *aes_find_key_slot(void)
308{
309 struct tegra_aes_slot *slot = NULL;
310 struct list_head *new_head;
311 int empty;
312
313 spin_lock(&list_lock);
314 empty = list_empty(&dev_list);
315 if (!empty) {
316 slot = list_entry(&dev_list, struct tegra_aes_slot, node);
317 new_head = dev_list.next;
318 list_del(&dev_list);
319 dev_list.next = new_head->next;
320 dev_list.prev = NULL;
321 }
322 spin_unlock(&list_lock);
323
324 return slot;
325}
326
327static int aes_set_key(struct tegra_aes_dev *dd)
328{
329 u32 value, cmdq[2];
330 struct tegra_aes_ctx *ctx = dd->ctx;
331 int eng_busy, icq_empty, dma_busy;
332 bool use_ssk = false;
333
334 /* use ssk? */
335 if (!dd->ctx->slot) {
336 dev_dbg(dd->dev, "using ssk");
337 dd->ctx->slot = &ssk;
338 use_ssk = true;
339 }
340
341 /* enable key schedule generation in hardware */
342 value = aes_readl(dd, TEGRA_AES_SECURE_CONFIG_EXT);
343 value &= ~TEGRA_AES_SECURE_KEY_SCH_DIS_FIELD;
344 aes_writel(dd, value, TEGRA_AES_SECURE_CONFIG_EXT);
345
346 /* select the key slot */
347 value = aes_readl(dd, TEGRA_AES_SECURE_CONFIG);
348 value &= ~TEGRA_AES_SECURE_KEY_INDEX_FIELD;
349 value |= (ctx->slot->slot_num << TEGRA_AES_SECURE_KEY_INDEX_SHIFT);
350 aes_writel(dd, value, TEGRA_AES_SECURE_CONFIG);
351
352 if (use_ssk)
353 return 0;
354
355 /* copy the key table from sdram to vram */
356 cmdq[0] = CMD_MEMDMAVD << CMDQ_OPCODE_SHIFT |
357 MEMDMA_DIR_DTOVRAM << MEMDMA_DIR_SHIFT |
358 AES_HW_KEY_TABLE_LENGTH_BYTES / sizeof(u32) <<
359 MEMDMA_NUM_WORDS_SHIFT;
360 cmdq[1] = (u32)dd->ivkey_phys_base;
361
362 aes_writel(dd, cmdq[0], TEGRA_AES_ICMDQUE_WR);
363 aes_writel(dd, cmdq[1], TEGRA_AES_ICMDQUE_WR);
364
365 do {
366 value = aes_readl(dd, TEGRA_AES_INTR_STATUS);
367 eng_busy = value & TEGRA_AES_ENGINE_BUSY_FIELD;
368 icq_empty = value & TEGRA_AES_ICQ_EMPTY_FIELD;
369 dma_busy = value & TEGRA_AES_DMA_BUSY_FIELD;
370 } while (eng_busy & (!icq_empty) & dma_busy);
371
372 /* settable command to get key into internal registers */
373 value = CMD_SETTABLE << CMDQ_OPCODE_SHIFT |
374 SUBCMD_CRYPTO_TABLE_SEL << CMDQ_TABLESEL_SHIFT |
375 SUBCMD_VRAM_SEL << CMDQ_VRAMSEL_SHIFT |
376 (SUBCMD_KEY_TABLE_SEL | ctx->slot->slot_num) <<
377 CMDQ_KEYTABLEID_SHIFT;
378 aes_writel(dd, value, TEGRA_AES_ICMDQUE_WR);
379
380 do {
381 value = aes_readl(dd, TEGRA_AES_INTR_STATUS);
382 eng_busy = value & TEGRA_AES_ENGINE_BUSY_FIELD;
383 icq_empty = value & TEGRA_AES_ICQ_EMPTY_FIELD;
384 } while (eng_busy & (!icq_empty));
385
386 return 0;
387}
388
389static int tegra_aes_handle_req(struct tegra_aes_dev *dd)
390{
391 struct crypto_async_request *async_req, *backlog;
392 struct crypto_ablkcipher *tfm;
393 struct tegra_aes_ctx *ctx;
394 struct tegra_aes_reqctx *rctx;
395 struct ablkcipher_request *req;
396 unsigned long flags;
397 int dma_max = AES_HW_DMA_BUFFER_SIZE_BYTES;
398 int ret = 0, nblocks, total;
399 int count = 0;
400 dma_addr_t addr_in, addr_out;
401 struct scatterlist *in_sg, *out_sg;
402
403 if (!dd)
404 return -EINVAL;
405
406 spin_lock_irqsave(&dd->lock, flags);
407 backlog = crypto_get_backlog(&dd->queue);
408 async_req = crypto_dequeue_request(&dd->queue);
409 if (!async_req)
410 clear_bit(FLAGS_BUSY, &dd->flags);
411 spin_unlock_irqrestore(&dd->lock, flags);
412
413 if (!async_req)
414 return -ENODATA;
415
416 if (backlog)
417 backlog->complete(backlog, -EINPROGRESS);
418
419 req = ablkcipher_request_cast(async_req);
420
421 dev_dbg(dd->dev, "%s: get new req\n", __func__);
422
423 if (!req->src || !req->dst)
424 return -EINVAL;
425
426 /* take mutex to access the aes hw */
427 mutex_lock(&aes_lock);
428
429 /* assign new request to device */
430 dd->req = req;
431 dd->total = req->nbytes;
432 dd->in_offset = 0;
433 dd->in_sg = req->src;
434 dd->out_offset = 0;
435 dd->out_sg = req->dst;
436
437 in_sg = dd->in_sg;
438 out_sg = dd->out_sg;
439
440 total = dd->total;
441
442 tfm = crypto_ablkcipher_reqtfm(req);
443 rctx = ablkcipher_request_ctx(req);
444 ctx = crypto_ablkcipher_ctx(tfm);
445 rctx->mode &= FLAGS_MODE_MASK;
446 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
447
448 dd->iv = (u8 *)req->info;
449 dd->ivlen = crypto_ablkcipher_ivsize(tfm);
450
451 /* assign new context to device */
452 ctx->dd = dd;
453 dd->ctx = ctx;
454
455 if (ctx->flags & FLAGS_NEW_KEY) {
456 /* copy the key */
457 memcpy(dd->ivkey_base, ctx->key, ctx->keylen);
458 memset(dd->ivkey_base + ctx->keylen, 0, AES_HW_KEY_TABLE_LENGTH_BYTES - ctx->keylen);
459 aes_set_key(dd);
460 ctx->flags &= ~FLAGS_NEW_KEY;
461 }
462
463 if (((dd->flags & FLAGS_CBC) || (dd->flags & FLAGS_OFB)) && dd->iv) {
464 /* set iv to the aes hw slot
465 * Hw generates updated iv only after iv is set in slot.
466 * So key and iv is passed asynchronously.
467 */
468 memcpy(dd->buf_in, dd->iv, dd->ivlen);
469
470 ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
471 dd->dma_buf_out, 1, FLAGS_CBC, false);
472 if (ret < 0) {
473 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
474 goto out;
475 }
476 }
477
478 while (total) {
479 dev_dbg(dd->dev, "remain: %d\n", total);
480 ret = dma_map_sg(dd->dev, in_sg, 1, DMA_TO_DEVICE);
481 if (!ret) {
482 dev_err(dd->dev, "dma_map_sg() error\n");
483 goto out;
484 }
485
486 ret = dma_map_sg(dd->dev, out_sg, 1, DMA_FROM_DEVICE);
487 if (!ret) {
488 dev_err(dd->dev, "dma_map_sg() error\n");
489 dma_unmap_sg(dd->dev, dd->in_sg,
490 1, DMA_TO_DEVICE);
491 goto out;
492 }
493
494 addr_in = sg_dma_address(in_sg);
495 addr_out = sg_dma_address(out_sg);
496 dd->flags |= FLAGS_FAST;
497 count = min_t(int, sg_dma_len(in_sg), dma_max);
498 WARN_ON(sg_dma_len(in_sg) != sg_dma_len(out_sg));
499 nblocks = DIV_ROUND_UP(count, AES_BLOCK_SIZE);
500
501 ret = aes_start_crypt(dd, addr_in, addr_out, nblocks,
502 dd->flags, true);
503
504 dma_unmap_sg(dd->dev, out_sg, 1, DMA_FROM_DEVICE);
505 dma_unmap_sg(dd->dev, in_sg, 1, DMA_TO_DEVICE);
506
507 if (ret < 0) {
508 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
509 goto out;
510 }
511 dd->flags &= ~FLAGS_FAST;
512
513 dev_dbg(dd->dev, "out: copied %d\n", count);
514 total -= count;
515 in_sg = sg_next(in_sg);
516 out_sg = sg_next(out_sg);
517 WARN_ON(((total != 0) && (!in_sg || !out_sg)));
518 }
519
520out:
521 mutex_unlock(&aes_lock);
522
523 dd->total = total;
524
525 if (dd->req->base.complete)
526 dd->req->base.complete(&dd->req->base, ret);
527
528 dev_dbg(dd->dev, "%s: exit\n", __func__);
529 return ret;
530}
531
532static int tegra_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
533 unsigned int keylen)
534{
535 struct tegra_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
536 struct tegra_aes_dev *dd = aes_dev;
537 struct tegra_aes_slot *key_slot;
538
539 if ((keylen != AES_KEYSIZE_128) && (keylen != AES_KEYSIZE_192) &&
540 (keylen != AES_KEYSIZE_256)) {
541 dev_err(dd->dev, "unsupported key size\n");
542 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
543 return -EINVAL;
544 }
545
546 dev_dbg(dd->dev, "keylen: %d\n", keylen);
547
548 ctx->dd = dd;
549
550 if (key) {
551 if (!ctx->slot) {
552 key_slot = aes_find_key_slot();
553 if (!key_slot) {
554 dev_err(dd->dev, "no empty slot\n");
555 return -ENOMEM;
556 }
557
558 ctx->slot = key_slot;
559 }
560
561 memcpy(ctx->key, key, keylen);
562 ctx->keylen = keylen;
563 }
564
565 ctx->flags |= FLAGS_NEW_KEY;
566 dev_dbg(dd->dev, "done\n");
567 return 0;
568}
569
570static void aes_workqueue_handler(struct work_struct *work)
571{
572 struct tegra_aes_dev *dd = aes_dev;
573 int ret;
574
Prashant Gaikwad0df1ed42012-06-05 09:59:43 +0530575 ret = clk_prepare_enable(dd->aes_clk);
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100576 if (ret)
577 BUG_ON("clock enable failed");
578
579 /* empty the crypto queue and then return */
580 do {
581 ret = tegra_aes_handle_req(dd);
582 } while (!ret);
583
Prashant Gaikwad0df1ed42012-06-05 09:59:43 +0530584 clk_disable_unprepare(dd->aes_clk);
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100585}
586
587static irqreturn_t aes_irq(int irq, void *dev_id)
588{
589 struct tegra_aes_dev *dd = (struct tegra_aes_dev *)dev_id;
590 u32 value = aes_readl(dd, TEGRA_AES_INTR_STATUS);
591 int busy = test_bit(FLAGS_BUSY, &dd->flags);
592
593 if (!busy) {
594 dev_dbg(dd->dev, "spurious interrupt\n");
595 return IRQ_NONE;
596 }
597
598 dev_dbg(dd->dev, "irq_stat: 0x%x\n", value);
599 if (value & TEGRA_AES_INT_ERROR_MASK)
600 aes_writel(dd, TEGRA_AES_INT_ERROR_MASK, TEGRA_AES_INTR_STATUS);
601
602 if (!(value & TEGRA_AES_ENGINE_BUSY_FIELD))
603 complete(&dd->op_complete);
604 else
605 return IRQ_NONE;
606
607 return IRQ_HANDLED;
608}
609
610static int tegra_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
611{
612 struct tegra_aes_reqctx *rctx = ablkcipher_request_ctx(req);
613 struct tegra_aes_dev *dd = aes_dev;
614 unsigned long flags;
615 int err = 0;
616 int busy;
617
618 dev_dbg(dd->dev, "nbytes: %d, enc: %d, cbc: %d, ofb: %d\n",
619 req->nbytes, !!(mode & FLAGS_ENCRYPT),
620 !!(mode & FLAGS_CBC), !!(mode & FLAGS_OFB));
621
622 rctx->mode = mode;
623
624 spin_lock_irqsave(&dd->lock, flags);
625 err = ablkcipher_enqueue_request(&dd->queue, req);
626 busy = test_and_set_bit(FLAGS_BUSY, &dd->flags);
627 spin_unlock_irqrestore(&dd->lock, flags);
628
629 if (!busy)
630 queue_work(aes_wq, &aes_work);
631
632 return err;
633}
634
635static int tegra_aes_ecb_encrypt(struct ablkcipher_request *req)
636{
637 return tegra_aes_crypt(req, FLAGS_ENCRYPT);
638}
639
640static int tegra_aes_ecb_decrypt(struct ablkcipher_request *req)
641{
642 return tegra_aes_crypt(req, 0);
643}
644
645static int tegra_aes_cbc_encrypt(struct ablkcipher_request *req)
646{
647 return tegra_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
648}
649
650static int tegra_aes_cbc_decrypt(struct ablkcipher_request *req)
651{
652 return tegra_aes_crypt(req, FLAGS_CBC);
653}
654
655static int tegra_aes_ofb_encrypt(struct ablkcipher_request *req)
656{
657 return tegra_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_OFB);
658}
659
660static int tegra_aes_ofb_decrypt(struct ablkcipher_request *req)
661{
662 return tegra_aes_crypt(req, FLAGS_OFB);
663}
664
665static int tegra_aes_get_random(struct crypto_rng *tfm, u8 *rdata,
666 unsigned int dlen)
667{
668 struct tegra_aes_dev *dd = aes_dev;
669 struct tegra_aes_ctx *ctx = &rng_ctx;
670 int ret, i;
671 u8 *dest = rdata, *dt = dd->dt;
672
673 /* take mutex to access the aes hw */
674 mutex_lock(&aes_lock);
675
Prashant Gaikwad0df1ed42012-06-05 09:59:43 +0530676 ret = clk_prepare_enable(dd->aes_clk);
Wei Yongjun3200da82012-10-21 19:56:42 +0800677 if (ret) {
678 mutex_unlock(&aes_lock);
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100679 return ret;
Wei Yongjun3200da82012-10-21 19:56:42 +0800680 }
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100681
682 ctx->dd = dd;
683 dd->ctx = ctx;
684 dd->flags = FLAGS_ENCRYPT | FLAGS_RNG;
685
686 memcpy(dd->buf_in, dt, DEFAULT_RNG_BLK_SZ);
687
688 ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
689 (u32)dd->dma_buf_out, 1, dd->flags, true);
690 if (ret < 0) {
691 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
692 dlen = ret;
693 goto out;
694 }
695 memcpy(dest, dd->buf_out, dlen);
696
697 /* update the DT */
698 for (i = DEFAULT_RNG_BLK_SZ - 1; i >= 0; i--) {
699 dt[i] += 1;
700 if (dt[i] != 0)
701 break;
702 }
703
704out:
Prashant Gaikwad0df1ed42012-06-05 09:59:43 +0530705 clk_disable_unprepare(dd->aes_clk);
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100706 mutex_unlock(&aes_lock);
707
708 dev_dbg(dd->dev, "%s: done\n", __func__);
709 return dlen;
710}
711
712static int tegra_aes_rng_reset(struct crypto_rng *tfm, u8 *seed,
713 unsigned int slen)
714{
715 struct tegra_aes_dev *dd = aes_dev;
716 struct tegra_aes_ctx *ctx = &rng_ctx;
717 struct tegra_aes_slot *key_slot;
718 struct timespec ts;
719 int ret = 0;
720 u64 nsec, tmp[2];
721 u8 *dt;
722
723 if (!ctx || !dd) {
724 dev_err(dd->dev, "ctx=0x%x, dd=0x%x\n",
725 (unsigned int)ctx, (unsigned int)dd);
726 return -EINVAL;
727 }
728
729 if (slen < (DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128)) {
730 dev_err(dd->dev, "seed size invalid");
731 return -ENOMEM;
732 }
733
734 /* take mutex to access the aes hw */
735 mutex_lock(&aes_lock);
736
737 if (!ctx->slot) {
738 key_slot = aes_find_key_slot();
739 if (!key_slot) {
740 dev_err(dd->dev, "no empty slot\n");
741 mutex_unlock(&aes_lock);
742 return -ENOMEM;
743 }
744 ctx->slot = key_slot;
745 }
746
747 ctx->dd = dd;
748 dd->ctx = ctx;
749 dd->ctr = 0;
750
751 ctx->keylen = AES_KEYSIZE_128;
752 ctx->flags |= FLAGS_NEW_KEY;
753
754 /* copy the key to the key slot */
755 memcpy(dd->ivkey_base, seed + DEFAULT_RNG_BLK_SZ, AES_KEYSIZE_128);
756 memset(dd->ivkey_base + AES_KEYSIZE_128, 0, AES_HW_KEY_TABLE_LENGTH_BYTES - AES_KEYSIZE_128);
757
758 dd->iv = seed;
759 dd->ivlen = slen;
760
761 dd->flags = FLAGS_ENCRYPT | FLAGS_RNG;
762
Prashant Gaikwad0df1ed42012-06-05 09:59:43 +0530763 ret = clk_prepare_enable(dd->aes_clk);
Wei Yongjun3200da82012-10-21 19:56:42 +0800764 if (ret) {
765 mutex_unlock(&aes_lock);
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100766 return ret;
Wei Yongjun3200da82012-10-21 19:56:42 +0800767 }
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100768
769 aes_set_key(dd);
770
771 /* set seed to the aes hw slot */
772 memcpy(dd->buf_in, dd->iv, DEFAULT_RNG_BLK_SZ);
773 ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
774 dd->dma_buf_out, 1, FLAGS_CBC, false);
775 if (ret < 0) {
776 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
777 goto out;
778 }
779
780 if (dd->ivlen >= (2 * DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128)) {
781 dt = dd->iv + DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128;
782 } else {
783 getnstimeofday(&ts);
784 nsec = timespec_to_ns(&ts);
785 do_div(nsec, 1000);
786 nsec ^= dd->ctr << 56;
787 dd->ctr++;
788 tmp[0] = nsec;
789 tmp[1] = tegra_chip_uid();
790 dt = (u8 *)tmp;
791 }
792 memcpy(dd->dt, dt, DEFAULT_RNG_BLK_SZ);
793
794out:
Prashant Gaikwad0df1ed42012-06-05 09:59:43 +0530795 clk_disable_unprepare(dd->aes_clk);
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100796 mutex_unlock(&aes_lock);
797
798 dev_dbg(dd->dev, "%s: done\n", __func__);
799 return ret;
800}
801
802static int tegra_aes_cra_init(struct crypto_tfm *tfm)
803{
804 tfm->crt_ablkcipher.reqsize = sizeof(struct tegra_aes_reqctx);
805
806 return 0;
807}
808
809void tegra_aes_cra_exit(struct crypto_tfm *tfm)
810{
811 struct tegra_aes_ctx *ctx =
812 crypto_ablkcipher_ctx((struct crypto_ablkcipher *)tfm);
813
814 if (ctx && ctx->slot)
815 aes_release_key_slot(ctx->slot);
816}
817
818static struct crypto_alg algs[] = {
819 {
820 .cra_name = "ecb(aes)",
821 .cra_driver_name = "ecb-aes-tegra",
822 .cra_priority = 300,
823 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
824 .cra_blocksize = AES_BLOCK_SIZE,
825 .cra_alignmask = 3,
826 .cra_type = &crypto_ablkcipher_type,
827 .cra_u.ablkcipher = {
828 .min_keysize = AES_MIN_KEY_SIZE,
829 .max_keysize = AES_MAX_KEY_SIZE,
830 .setkey = tegra_aes_setkey,
831 .encrypt = tegra_aes_ecb_encrypt,
832 .decrypt = tegra_aes_ecb_decrypt,
833 },
834 }, {
835 .cra_name = "cbc(aes)",
836 .cra_driver_name = "cbc-aes-tegra",
837 .cra_priority = 300,
838 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
839 .cra_blocksize = AES_BLOCK_SIZE,
840 .cra_alignmask = 3,
841 .cra_type = &crypto_ablkcipher_type,
842 .cra_u.ablkcipher = {
843 .min_keysize = AES_MIN_KEY_SIZE,
844 .max_keysize = AES_MAX_KEY_SIZE,
845 .ivsize = AES_MIN_KEY_SIZE,
846 .setkey = tegra_aes_setkey,
847 .encrypt = tegra_aes_cbc_encrypt,
848 .decrypt = tegra_aes_cbc_decrypt,
849 }
850 }, {
851 .cra_name = "ofb(aes)",
852 .cra_driver_name = "ofb-aes-tegra",
853 .cra_priority = 300,
854 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
855 .cra_blocksize = AES_BLOCK_SIZE,
856 .cra_alignmask = 3,
857 .cra_type = &crypto_ablkcipher_type,
858 .cra_u.ablkcipher = {
859 .min_keysize = AES_MIN_KEY_SIZE,
860 .max_keysize = AES_MAX_KEY_SIZE,
861 .ivsize = AES_MIN_KEY_SIZE,
862 .setkey = tegra_aes_setkey,
863 .encrypt = tegra_aes_ofb_encrypt,
864 .decrypt = tegra_aes_ofb_decrypt,
865 }
866 }, {
867 .cra_name = "ansi_cprng",
868 .cra_driver_name = "rng-aes-tegra",
869 .cra_flags = CRYPTO_ALG_TYPE_RNG,
870 .cra_ctxsize = sizeof(struct tegra_aes_ctx),
871 .cra_type = &crypto_rng_type,
872 .cra_u.rng = {
873 .rng_make_random = tegra_aes_get_random,
874 .rng_reset = tegra_aes_rng_reset,
875 .seedsize = AES_KEYSIZE_128 + (2 * DEFAULT_RNG_BLK_SZ),
876 }
877 }
878};
879
880static int tegra_aes_probe(struct platform_device *pdev)
881{
882 struct device *dev = &pdev->dev;
883 struct tegra_aes_dev *dd;
884 struct resource *res;
885 int err = -ENOMEM, i = 0, j;
886
887 dd = devm_kzalloc(dev, sizeof(struct tegra_aes_dev), GFP_KERNEL);
888 if (dd == NULL) {
889 dev_err(dev, "unable to alloc data struct.\n");
890 return err;
891 }
892
893 dd->dev = dev;
894 platform_set_drvdata(pdev, dd);
895
896 dd->slots = devm_kzalloc(dev, sizeof(struct tegra_aes_slot) *
897 AES_NR_KEYSLOTS, GFP_KERNEL);
898 if (dd->slots == NULL) {
899 dev_err(dev, "unable to alloc slot struct.\n");
900 goto out;
901 }
902
903 spin_lock_init(&dd->lock);
904 crypto_init_queue(&dd->queue, TEGRA_AES_QUEUE_LENGTH);
905
906 /* Get the module base address */
907 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
908 if (!res) {
909 dev_err(dev, "invalid resource type: base\n");
910 err = -ENODEV;
911 goto out;
912 }
913
914 if (!devm_request_mem_region(&pdev->dev, res->start,
915 resource_size(res),
916 dev_name(&pdev->dev))) {
917 dev_err(&pdev->dev, "Couldn't request MEM resource\n");
918 return -ENODEV;
919 }
920
921 dd->io_base = devm_ioremap(dev, res->start, resource_size(res));
922 if (!dd->io_base) {
923 dev_err(dev, "can't ioremap register space\n");
924 err = -ENOMEM;
925 goto out;
926 }
927
928 /* Initialize the vde clock */
929 dd->aes_clk = clk_get(dev, "vde");
930 if (IS_ERR(dd->aes_clk)) {
931 dev_err(dev, "iclock intialization failed.\n");
932 err = -ENODEV;
933 goto out;
934 }
935
936 err = clk_set_rate(dd->aes_clk, ULONG_MAX);
937 if (err) {
938 dev_err(dd->dev, "iclk set_rate fail(%d)\n", err);
939 goto out;
940 }
941
942 /*
943 * the foll contiguous memory is allocated as follows -
944 * - hardware key table
945 * - key schedule
946 */
947 dd->ivkey_base = dma_alloc_coherent(dev, AES_HW_KEY_TABLE_LENGTH_BYTES,
948 &dd->ivkey_phys_base,
949 GFP_KERNEL);
950 if (!dd->ivkey_base) {
951 dev_err(dev, "can not allocate iv/key buffer\n");
952 err = -ENOMEM;
953 goto out;
954 }
955
956 dd->buf_in = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
957 &dd->dma_buf_in, GFP_KERNEL);
958 if (!dd->buf_in) {
959 dev_err(dev, "can not allocate dma-in buffer\n");
960 err = -ENOMEM;
961 goto out;
962 }
963
964 dd->buf_out = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
965 &dd->dma_buf_out, GFP_KERNEL);
966 if (!dd->buf_out) {
967 dev_err(dev, "can not allocate dma-out buffer\n");
968 err = -ENOMEM;
969 goto out;
970 }
971
972 init_completion(&dd->op_complete);
973 aes_wq = alloc_workqueue("tegra_aes_wq", WQ_HIGHPRI | WQ_UNBOUND, 1);
974 if (!aes_wq) {
975 dev_err(dev, "alloc_workqueue failed\n");
Peter Senna Tschudin35c41db2012-09-17 19:28:28 +0200976 err = -ENOMEM;
Varun Wadekarf1df57d2012-01-13 16:38:37 +1100977 goto out;
978 }
979
980 /* get the irq */
981 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
982 if (!res) {
983 dev_err(dev, "invalid resource type: base\n");
984 err = -ENODEV;
985 goto out;
986 }
987 dd->irq = res->start;
988
989 err = devm_request_irq(dev, dd->irq, aes_irq, IRQF_TRIGGER_HIGH |
990 IRQF_SHARED, "tegra-aes", dd);
991 if (err) {
992 dev_err(dev, "request_irq failed\n");
993 goto out;
994 }
995
996 mutex_init(&aes_lock);
997 INIT_LIST_HEAD(&dev_list);
998
999 spin_lock_init(&list_lock);
1000 spin_lock(&list_lock);
1001 for (i = 0; i < AES_NR_KEYSLOTS; i++) {
1002 if (i == SSK_SLOT_NUM)
1003 continue;
1004 dd->slots[i].slot_num = i;
1005 INIT_LIST_HEAD(&dd->slots[i].node);
1006 list_add_tail(&dd->slots[i].node, &dev_list);
1007 }
1008 spin_unlock(&list_lock);
1009
1010 aes_dev = dd;
1011 for (i = 0; i < ARRAY_SIZE(algs); i++) {
Varun Wadekarf1df57d2012-01-13 16:38:37 +11001012 algs[i].cra_priority = 300;
1013 algs[i].cra_ctxsize = sizeof(struct tegra_aes_ctx);
1014 algs[i].cra_module = THIS_MODULE;
1015 algs[i].cra_init = tegra_aes_cra_init;
1016 algs[i].cra_exit = tegra_aes_cra_exit;
1017
1018 err = crypto_register_alg(&algs[i]);
1019 if (err)
1020 goto out;
1021 }
1022
1023 dev_info(dev, "registered");
1024 return 0;
1025
1026out:
1027 for (j = 0; j < i; j++)
1028 crypto_unregister_alg(&algs[j]);
1029 if (dd->ivkey_base)
1030 dma_free_coherent(dev, AES_HW_KEY_TABLE_LENGTH_BYTES,
1031 dd->ivkey_base, dd->ivkey_phys_base);
1032 if (dd->buf_in)
1033 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1034 dd->buf_in, dd->dma_buf_in);
1035 if (dd->buf_out)
1036 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1037 dd->buf_out, dd->dma_buf_out);
1038 if (IS_ERR(dd->aes_clk))
1039 clk_put(dd->aes_clk);
1040 if (aes_wq)
1041 destroy_workqueue(aes_wq);
1042 spin_lock(&list_lock);
1043 list_del(&dev_list);
1044 spin_unlock(&list_lock);
1045
1046 aes_dev = NULL;
1047
1048 dev_err(dev, "%s: initialization failed.\n", __func__);
1049 return err;
1050}
1051
1052static int __devexit tegra_aes_remove(struct platform_device *pdev)
1053{
1054 struct device *dev = &pdev->dev;
1055 struct tegra_aes_dev *dd = platform_get_drvdata(pdev);
1056 int i;
1057
1058 for (i = 0; i < ARRAY_SIZE(algs); i++)
1059 crypto_unregister_alg(&algs[i]);
1060
1061 cancel_work_sync(&aes_work);
1062 destroy_workqueue(aes_wq);
1063 spin_lock(&list_lock);
1064 list_del(&dev_list);
1065 spin_unlock(&list_lock);
1066
1067 dma_free_coherent(dev, AES_HW_KEY_TABLE_LENGTH_BYTES,
1068 dd->ivkey_base, dd->ivkey_phys_base);
1069 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1070 dd->buf_in, dd->dma_buf_in);
1071 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1072 dd->buf_out, dd->dma_buf_out);
1073 clk_put(dd->aes_clk);
1074 aes_dev = NULL;
1075
1076 return 0;
1077}
1078
1079static struct of_device_id tegra_aes_of_match[] __devinitdata = {
1080 { .compatible = "nvidia,tegra20-aes", },
1081 { .compatible = "nvidia,tegra30-aes", },
1082 { },
1083};
1084
1085static struct platform_driver tegra_aes_driver = {
1086 .probe = tegra_aes_probe,
1087 .remove = __devexit_p(tegra_aes_remove),
1088 .driver = {
1089 .name = "tegra-aes",
1090 .owner = THIS_MODULE,
1091 .of_match_table = tegra_aes_of_match,
1092 },
1093};
1094
1095module_platform_driver(tegra_aes_driver);
1096
1097MODULE_DESCRIPTION("Tegra AES/OFB/CPRNG hw acceleration support.");
1098MODULE_AUTHOR("NVIDIA Corporation");
1099MODULE_LICENSE("GPL v2");