2 * drivers/crypto/tegra-aes.c
4 * aes driver for NVIDIA tegra aes hardware
6 * Copyright (c) 2010, NVIDIA Corporation.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/clk.h>
28 #include <linux/platform_device.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
32 #include <linux/mutex.h>
33 #include <linux/interrupt.h>
34 #include <linux/completion.h>
35 #include <linux/workqueue.h>
37 #include <mach/arb_sema.h>
40 #include <crypto/scatterwalk.h>
41 #include <crypto/aes.h>
42 #include <crypto/internal/rng.h>
44 #include "tegra-aes.h"
46 #define FLAGS_MODE_MASK 0x000f
47 #define FLAGS_ENCRYPT BIT(0)
48 #define FLAGS_CBC BIT(1)
49 #define FLAGS_GIV BIT(2)
50 #define FLAGS_RNG BIT(3)
51 #define FLAGS_NEW_KEY BIT(4)
52 #define FLAGS_NEW_IV BIT(5)
53 #define FLAGS_INIT BIT(6)
54 #define FLAGS_FAST BIT(7)
58 * Defines AES engine Max process bytes size in one go, which takes 1 msec.
59 * AES engine spends about 176 cycles/16-bytes or 11 cycles/byte
60 * The duration CPU can use the BSE to 1 msec, then the number of available
61 * cycles of AVP/BSE is 216K. In this duration, AES can process 216/11 ~= 19KB
62 * Based on this AES_HW_DMA_BUFFER_SIZE_BYTES is configured to 16KB.
64 #define AES_HW_DMA_BUFFER_SIZE_BYTES 0x4000
67 * The key table length is 64 bytes
68 * (This includes first upto 32 bytes key + 16 bytes original initial vector
69 * and 16 bytes updated initial vector)
71 #define AES_HW_KEY_TABLE_LENGTH_BYTES 64
73 #define AES_HW_IV_SIZE 16
74 #define AES_HW_KEYSCHEDULE_LEN 256
75 #define ARB_SEMA_TIMEOUT 500
78 * The memory being used is divides as follows:
80 * 2. Original IV - 16 bytes
81 * 3. Updated IV - 16 bytes
82 * 4. Key schedule - 256 bytes
84 * 1+2+3 constitute the hw key table.
86 #define AES_IVKEY_SIZE (AES_HW_KEY_TABLE_LENGTH_BYTES + AES_HW_KEYSCHEDULE_LEN)
88 #define DEFAULT_RNG_BLK_SZ 16
90 /* As of now only 5 commands are USED for AES encryption/Decryption */
91 #define AES_HW_MAX_ICQ_LENGTH 5
93 #define ICQBITSHIFT_BLKCNT 0
95 /* memdma_vd command */
96 #define MEMDMA_DIR_DTOVRAM 0
97 #define MEMDMA_DIR_VTODRAM 1
98 #define MEMDMABITSHIFT_DIR 25
99 #define MEMDMABITSHIFT_NUM_WORDS 12
101 /* Define AES Interactive command Queue commands Bit positions */
103 ICQBITSHIFT_KEYTABLEADDR = 0,
104 ICQBITSHIFT_KEYTABLEID = 17,
105 ICQBITSHIFT_VRAMSEL = 23,
106 ICQBITSHIFT_TABLESEL = 24,
107 ICQBITSHIFT_OPCODE = 26,
110 /* Define Ucq opcodes required for AES operation */
112 UCQOPCODE_BLKSTARTENGINE = 0x0E,
113 UCQOPCODE_DMASETUP = 0x10,
114 UCQOPCODE_DMACOMPLETE = 0x11,
115 UCQOPCODE_SETTABLE = 0x15,
116 UCQOPCODE_MEMDMAVD = 0x22,
119 /* Define Aes command values */
121 UCQCMD_VRAM_SEL = 0x1,
122 UCQCMD_CRYPTO_TABLESEL = 0x3,
123 UCQCMD_KEYSCHEDTABLESEL = 0x4,
124 UCQCMD_KEYTABLESEL = 0x8,
127 #define UCQCMD_KEYTABLEADDRMASK 0x1FFFF
129 #define AES_NR_KEYSLOTS 8
130 #define SSK_SLOT_NUM 4
132 struct tegra_aes_slot {
133 struct list_head node;
138 static struct tegra_aes_slot ssk = {
139 .slot_num = SSK_SLOT_NUM,
143 struct tegra_aes_reqctx {
147 #define TEGRA_AES_QUEUE_LENGTH 50
149 struct tegra_aes_dev {
151 unsigned long phys_base;
152 void __iomem *io_base;
153 dma_addr_t ivkey_phys_base;
154 void __iomem *ivkey_base;
157 struct tegra_aes_ctx *ctx;
159 struct completion op_complete;
161 dma_addr_t dma_buf_in;
163 dma_addr_t dma_buf_out;
165 u8 dt[DEFAULT_RNG_BLK_SZ];
170 struct crypto_queue queue;
171 struct tegra_aes_slot *slots;
172 struct ablkcipher_request *req;
174 struct scatterlist *in_sg;
176 struct scatterlist *out_sg;
180 static struct tegra_aes_dev *aes_dev;
182 struct tegra_aes_ctx {
183 struct tegra_aes_dev *dd;
185 struct tegra_aes_slot *slot;
189 static struct tegra_aes_ctx rng_ctx = {
190 .flags = FLAGS_NEW_KEY,
191 .keylen = AES_KEYSIZE_128,
194 /* keep registered devices data here */
195 static LIST_HEAD(dev_list);
196 static DEFINE_SPINLOCK(list_lock);
197 static DEFINE_MUTEX(aes_lock);
199 static void aes_workqueue_handler(struct work_struct *work);
200 static DECLARE_WORK(aes_work, aes_workqueue_handler);
201 static struct workqueue_struct *aes_wq;
203 extern unsigned long long tegra_chip_uid(void);
205 static inline u32 aes_readl(struct tegra_aes_dev *dd, u32 offset)
207 return readl(dd->io_base + offset);
210 static inline void aes_writel(struct tegra_aes_dev *dd, u32 val, u32 offset)
212 writel(val, dd->io_base + offset);
215 static int aes_hw_init(struct tegra_aes_dev *dd)
219 ret = clk_enable(dd->pclk);
221 dev_err(dd->dev, "%s: pclock enable fail(%d)\n", __func__, ret);
225 ret = clk_enable(dd->iclk);
227 dev_err(dd->dev, "%s: iclock enable fail(%d)\n", __func__, ret);
228 clk_disable(dd->pclk);
232 ret = clk_set_rate(dd->iclk, 240000000);
234 dev_err(dd->dev, "%s: iclk set_rate fail(%d)\n", __func__, ret);
235 clk_disable(dd->iclk);
236 clk_disable(dd->pclk);
240 aes_writel(dd, 0x33, INT_ENB);
244 static void aes_hw_deinit(struct tegra_aes_dev *dd)
246 clk_disable(dd->iclk);
247 clk_disable(dd->pclk);
250 static int aes_start_crypt(struct tegra_aes_dev *dd, u32 in_addr, u32 out_addr,
251 int nblocks, int mode, bool upd_iv)
253 u32 cmdq[AES_HW_MAX_ICQ_LENGTH];
254 int qlen = 0, i, eng_busy, icq_empty, dma_busy, ret = 0;
257 cmdq[qlen++] = UCQOPCODE_DMASETUP << ICQBITSHIFT_OPCODE;
258 cmdq[qlen++] = in_addr;
259 cmdq[qlen++] = UCQOPCODE_BLKSTARTENGINE << ICQBITSHIFT_OPCODE |
260 (nblocks-1) << ICQBITSHIFT_BLKCNT;
261 cmdq[qlen++] = UCQOPCODE_DMACOMPLETE << ICQBITSHIFT_OPCODE;
263 value = aes_readl(dd, CMDQUE_CONTROL);
264 /* access SDRAM through AHB */
265 value &= ~CMDQ_CTRL_SRC_STM_SEL_FIELD;
266 value &= ~CMDQ_CTRL_DST_STM_SEL_FIELD;
267 value |= (CMDQ_CTRL_SRC_STM_SEL_FIELD | CMDQ_CTRL_DST_STM_SEL_FIELD |
268 CMDQ_CTRL_ICMDQEN_FIELD);
269 aes_writel(dd, value, CMDQUE_CONTROL);
270 dev_dbg(dd->dev, "cmd_q_ctrl=0x%x", value);
273 value |= CONFIG_ENDIAN_ENB_FIELD;
274 aes_writel(dd, value, CONFIG);
275 dev_dbg(dd->dev, "config=0x%x", value);
277 value = aes_readl(dd, SECURE_CONFIG_EXT);
278 value &= ~SECURE_OFFSET_CNT_FIELD;
279 aes_writel(dd, value, SECURE_CONFIG_EXT);
280 dev_dbg(dd->dev, "secure_cfg_xt=0x%x", value);
282 if (mode & FLAGS_CBC) {
283 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
284 ((dd->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
285 ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
286 (((mode & FLAGS_ENCRYPT) ? 2 : 3)
287 << SECURE_XOR_POS_SHIFT) |
288 (0 << SECURE_INPUT_SEL_SHIFT) |
289 (((mode & FLAGS_ENCRYPT) ? 2 : 3)
290 << SECURE_VCTRAM_SEL_SHIFT) |
291 ((mode & FLAGS_ENCRYPT) ? 1 : 0)
292 << SECURE_CORE_SEL_SHIFT |
293 (0 << SECURE_RNG_ENB_SHIFT) |
294 (0 << SECURE_HASH_ENB_SHIFT));
295 } else if (mode & FLAGS_RNG){
296 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
297 ((dd->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
298 ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
299 (0 << SECURE_XOR_POS_SHIFT) |
300 (0 << SECURE_INPUT_SEL_SHIFT) |
301 ((mode & FLAGS_ENCRYPT) ? 1 : 0)
302 << SECURE_CORE_SEL_SHIFT |
303 (1 << SECURE_RNG_ENB_SHIFT) |
304 (0 << SECURE_HASH_ENB_SHIFT));
306 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
307 ((dd->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
308 ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
309 (0 << SECURE_XOR_POS_SHIFT) |
310 (0 << SECURE_INPUT_SEL_SHIFT) |
311 (((mode & FLAGS_ENCRYPT) ? 1 : 0)
312 << SECURE_CORE_SEL_SHIFT) |
313 (0 << SECURE_RNG_ENB_SHIFT) |
314 (0 << SECURE_HASH_ENB_SHIFT));
316 dev_dbg(dd->dev, "secure_in_sel=0x%x", value);
317 aes_writel(dd, value, SECURE_INPUT_SELECT);
319 aes_writel(dd, out_addr, SECURE_DEST_ADDR);
320 INIT_COMPLETION(dd->op_complete);
322 for (i = 0; i < qlen - 1; i++) {
324 value = aes_readl(dd, INTR_STATUS);
325 eng_busy = value & (0x1);
326 icq_empty = value & (0x1<<3);
327 dma_busy = value & (0x1<<23);
328 } while (eng_busy & (!icq_empty) & dma_busy);
329 aes_writel(dd, cmdq[i], ICMDQUE_WR);
332 ret = wait_for_completion_timeout(&dd->op_complete, msecs_to_jiffies(150));
334 dev_err(dd->dev, "timed out (0x%x)\n",
335 aes_readl(dd, INTR_STATUS));
339 aes_writel(dd, cmdq[qlen - 1], ICMDQUE_WR);
343 static void aes_release_key_slot(struct tegra_aes_dev *dd)
345 spin_lock(&list_lock);
346 dd->ctx->slot->available = true;
347 dd->ctx->slot = NULL;
348 spin_unlock(&list_lock);
351 static struct tegra_aes_slot *aes_find_key_slot(struct tegra_aes_dev *dd)
353 struct tegra_aes_slot *slot = NULL;
356 spin_lock(&list_lock);
357 list_for_each_entry(slot, &dev_list, node) {
358 dev_dbg(dd->dev, "empty:%d, num:%d\n", slot->available,
360 if (slot->available) {
361 slot->available = false;
366 spin_unlock(&list_lock);
367 return found ? slot : NULL;
370 static int aes_set_key(struct tegra_aes_dev *dd)
373 struct tegra_aes_ctx *ctx = dd->ctx;
374 int i, eng_busy, icq_empty, dma_busy;
375 bool use_ssk = false;
378 dev_err(dd->dev, "%s: context invalid\n", __func__);
383 if (!dd->ctx->slot) {
384 dev_dbg(dd->dev, "using ssk");
385 dd->ctx->slot = &ssk;
389 /* disable key read from hw */
390 value = aes_readl(dd, SECURE_SEC_SEL0+(ctx->slot->slot_num*4));
391 value &= ~SECURE_SEL0_KEYREAD_ENB0_FIELD;
392 aes_writel(dd, value, SECURE_SEC_SEL0+(ctx->slot->slot_num*4));
394 /* enable key schedule generation in hardware */
395 value = aes_readl(dd, SECURE_CONFIG_EXT);
396 value &= ~SECURE_KEY_SCH_DIS_FIELD;
397 aes_writel(dd, value, SECURE_CONFIG_EXT);
399 /* select the key slot */
400 value = aes_readl(dd, SECURE_CONFIG);
401 value &= ~SECURE_KEY_INDEX_FIELD;
402 value |= (ctx->slot->slot_num << SECURE_KEY_INDEX_SHIFT);
403 aes_writel(dd, value, SECURE_CONFIG);
408 /* copy the key table from sdram to vram */
410 cmdq[0] = UCQOPCODE_MEMDMAVD << ICQBITSHIFT_OPCODE |
411 (MEMDMA_DIR_DTOVRAM << MEMDMABITSHIFT_DIR) |
412 (AES_HW_KEY_TABLE_LENGTH_BYTES/sizeof(u32))
413 << MEMDMABITSHIFT_NUM_WORDS;
414 cmdq[1] = (u32)dd->ivkey_phys_base;
416 for (i = 0; i < ARRAY_SIZE(cmdq); i++)
417 aes_writel(dd, cmdq[i], ICMDQUE_WR);
420 value = aes_readl(dd, INTR_STATUS);
421 eng_busy = value & (0x1);
422 icq_empty = value & (0x1<<3);
423 dma_busy = value & (0x1<<23);
424 } while (eng_busy & (!icq_empty) & dma_busy);
426 /* settable command to get key into internal registers */
428 value = UCQOPCODE_SETTABLE << ICQBITSHIFT_OPCODE |
429 UCQCMD_CRYPTO_TABLESEL << ICQBITSHIFT_TABLESEL |
430 UCQCMD_VRAM_SEL << ICQBITSHIFT_VRAMSEL |
431 (UCQCMD_KEYTABLESEL | ctx->slot->slot_num)
432 << ICQBITSHIFT_KEYTABLEID;
433 aes_writel(dd, value, ICMDQUE_WR);
435 value = aes_readl(dd, INTR_STATUS);
436 eng_busy = value & (0x1);
437 icq_empty = value & (0x1<<3);
438 } while (eng_busy & (!icq_empty));
444 static int tegra_aes_handle_req(struct tegra_aes_dev *dd)
446 struct crypto_async_request *async_req, *backlog;
447 struct tegra_aes_ctx *ctx;
448 struct tegra_aes_reqctx *rctx;
449 struct ablkcipher_request *req;
451 int dma_max = AES_HW_DMA_BUFFER_SIZE_BYTES;
452 int ret = 0, nblocks, total;
454 dma_addr_t addr_in, addr_out;
455 struct scatterlist *in_sg, *out_sg;
460 spin_lock_irqsave(&dd->lock, flags);
461 backlog = crypto_get_backlog(&dd->queue);
462 async_req = crypto_dequeue_request(&dd->queue);
464 clear_bit(FLAGS_BUSY, &dd->flags);
465 spin_unlock_irqrestore(&dd->lock, flags);
471 backlog->complete(backlog, -EINPROGRESS);
473 req = ablkcipher_request_cast(async_req);
475 dev_dbg(dd->dev, "%s: get new req\n", __func__);
477 /* take mutex to access the aes hw */
478 mutex_lock(&aes_lock);
480 /* assign new request to device */
482 dd->total = req->nbytes;
484 dd->in_sg = req->src;
486 dd->out_sg = req->dst;
491 if (!in_sg || !out_sg) {
492 mutex_unlock(&aes_lock);
497 rctx = ablkcipher_request_ctx(req);
498 ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
499 rctx->mode &= FLAGS_MODE_MASK;
500 dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
502 dd->iv = (u8 *)req->info;
503 dd->ivlen = AES_BLOCK_SIZE;
505 if ((dd->flags & FLAGS_CBC) && dd->iv)
506 dd->flags |= FLAGS_NEW_IV;
508 dd->flags &= ~FLAGS_NEW_IV;
511 if (dd->ctx != ctx) {
512 /* assign new context to device */
514 ctx->flags |= FLAGS_NEW_KEY;
517 /* take the hardware semaphore */
518 if (tegra_arb_mutex_lock_timeout(dd->res_id, ARB_SEMA_TIMEOUT) < 0) {
519 dev_err(dd->dev, "aes hardware not available\n");
520 mutex_unlock(&aes_lock);
524 ret = aes_hw_init(dd);
526 dev_err(dd->dev, "%s: hw init fail(%d)\n", __func__, ret);
532 /* set iv to the aes hw slot */
533 memset(dd->buf_in, 0 , AES_BLOCK_SIZE);
534 memcpy(dd->buf_in, dd->iv, dd->ivlen);
536 ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
537 (u32)dd->dma_buf_out, 1, FLAGS_CBC, false);
539 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
544 dev_dbg(dd->dev, "remain: 0x%x\n", total);
546 ret = dma_map_sg(dd->dev, in_sg, 1, DMA_TO_DEVICE);
548 dev_err(dd->dev, "dma_map_sg() error\n");
552 ret = dma_map_sg(dd->dev, out_sg, 1, DMA_FROM_DEVICE);
554 dev_err(dd->dev, "dma_map_sg() error\n");
555 dma_unmap_sg(dd->dev, dd->in_sg,
560 addr_in = sg_dma_address(in_sg);
561 addr_out = sg_dma_address(out_sg);
562 dd->flags |= FLAGS_FAST;
563 count = min((int)sg_dma_len(in_sg), (int)dma_max);
564 WARN_ON(sg_dma_len(in_sg) != sg_dma_len(out_sg));
565 nblocks = DIV_ROUND_UP(count, AES_BLOCK_SIZE);
567 ret = aes_start_crypt(dd, addr_in, addr_out, nblocks,
570 dma_unmap_sg(dd->dev, out_sg, 1, DMA_FROM_DEVICE);
571 dma_unmap_sg(dd->dev, in_sg, 1, DMA_TO_DEVICE);
574 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
577 dd->flags &= ~FLAGS_FAST;
579 dev_dbg(dd->dev, "out: copied 0x%x\n", count);
581 in_sg = sg_next(in_sg);
582 out_sg = sg_next(out_sg);
583 WARN_ON(((total != 0) && (!in_sg || !out_sg)));
590 /* release the hardware semaphore */
591 tegra_arb_mutex_unlock(dd->res_id);
595 /* release the mutex */
596 mutex_unlock(&aes_lock);
598 if (dd->req->base.complete)
599 dd->req->base.complete(&dd->req->base, ret);
601 dev_dbg(dd->dev, "%s: exit\n", __func__);
605 static int tegra_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
608 struct tegra_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
609 struct tegra_aes_dev *dd = aes_dev;
610 struct tegra_aes_slot *key_slot;
613 dev_err(dd->dev, "ctx=0x%x, dd=0x%x\n",
614 (unsigned int)ctx, (unsigned int)dd);
618 if ((keylen != AES_KEYSIZE_128) && (keylen != AES_KEYSIZE_192) &&
619 (keylen != AES_KEYSIZE_256)) {
620 dev_err(dd->dev, "unsupported key size\n");
624 dev_dbg(dd->dev, "keylen: %d\n", keylen);
630 aes_release_key_slot(dd);
632 key_slot = aes_find_key_slot(dd);
634 dev_err(dd->dev, "no empty slot\n");
638 ctx->slot = key_slot;
639 ctx->keylen = keylen;
640 ctx->flags |= FLAGS_NEW_KEY;
643 memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
644 memcpy(dd->ivkey_base, key, keylen);
646 dev_dbg(dd->dev, "done\n");
650 static void aes_workqueue_handler(struct work_struct *work)
652 struct tegra_aes_dev *dd = aes_dev;
655 set_bit(FLAGS_BUSY, &dd->flags);
658 ret = tegra_aes_handle_req(dd);
662 static irqreturn_t aes_irq(int irq, void *dev_id)
664 struct tegra_aes_dev *dd = (struct tegra_aes_dev *)dev_id;
665 u32 value = aes_readl(dd, INTR_STATUS);
667 dev_dbg(dd->dev, "irq_stat: 0x%x", value);
668 if (!((value & ENGINE_BUSY_FIELD) & !(value & ICQ_EMPTY_FIELD)))
669 complete(&dd->op_complete);
674 static int tegra_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
676 struct tegra_aes_reqctx *rctx = ablkcipher_request_ctx(req);
677 struct tegra_aes_dev *dd = aes_dev;
682 dev_dbg(dd->dev, "nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
683 !!(mode & FLAGS_ENCRYPT),
684 !!(mode & FLAGS_CBC));
688 spin_lock_irqsave(&dd->lock, flags);
689 err = ablkcipher_enqueue_request(&dd->queue, req);
690 busy = test_and_set_bit(FLAGS_BUSY, &dd->flags);
691 spin_unlock_irqrestore(&dd->lock, flags);
694 queue_work(aes_wq, &aes_work);
699 static int tegra_aes_ecb_encrypt(struct ablkcipher_request *req)
701 return tegra_aes_crypt(req, FLAGS_ENCRYPT);
704 static int tegra_aes_ecb_decrypt(struct ablkcipher_request *req)
706 return tegra_aes_crypt(req, 0);
709 static int tegra_aes_cbc_encrypt(struct ablkcipher_request *req)
711 return tegra_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
714 static int tegra_aes_cbc_decrypt(struct ablkcipher_request *req)
716 return tegra_aes_crypt(req, FLAGS_CBC);
719 static int tegra_aes_get_random(struct crypto_rng *tfm, u8 *rdata,
722 struct tegra_aes_dev *dd = aes_dev;
723 struct tegra_aes_ctx *ctx = &rng_ctx;
725 u8 *dest = rdata, *dt = dd->dt;
727 /* take mutex to access the aes hw */
728 mutex_lock(&aes_lock);
730 /* take the hardware semaphore */
731 if (tegra_arb_mutex_lock_timeout(dd->res_id, ARB_SEMA_TIMEOUT) < 0) {
732 dev_err(dd->dev, "aes hardware not available\n");
733 mutex_unlock(&aes_lock);
737 ret = aes_hw_init(dd);
739 dev_err(dd->dev, "%s: hw init fail(%d)\n", __func__, ret);
746 dd->flags = FLAGS_ENCRYPT | FLAGS_RNG;
748 memset(dd->buf_in, 0, AES_BLOCK_SIZE);
749 memcpy(dd->buf_in, dt, DEFAULT_RNG_BLK_SZ);
751 ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
752 (u32)dd->dma_buf_out, 1, dd->flags, true);
754 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
758 memcpy(dest, dd->buf_out, dlen);
761 for (i = DEFAULT_RNG_BLK_SZ - 1; i >= 0; i--) {
771 /* release the hardware semaphore */
772 tegra_arb_mutex_unlock(dd->res_id);
773 mutex_unlock(&aes_lock);
774 dev_dbg(dd->dev, "%s: done\n", __func__);
778 static int tegra_aes_rng_reset(struct crypto_rng *tfm, u8 *seed,
781 struct tegra_aes_dev *dd = aes_dev;
782 struct tegra_aes_ctx *ctx = &rng_ctx;
783 struct tegra_aes_slot *key_slot;
790 dev_err(dd->dev, "ctx=0x%x, dd=0x%x\n",
791 (unsigned int)ctx, (unsigned int)dd);
795 if (slen < (DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128)) {
796 dev_err(dd->dev, "seed size invalid");
800 /* take mutex to access the aes hw */
801 mutex_lock(&aes_lock);
804 key_slot = aes_find_key_slot(dd);
806 dev_err(dd->dev, "no empty slot\n");
807 mutex_unlock(&aes_lock);
810 ctx->slot = key_slot;
817 ctx->keylen = AES_KEYSIZE_128;
818 ctx->flags |= FLAGS_NEW_KEY;
820 /* copy the key to the key slot */
821 memset(dd->ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
822 memcpy(dd->ivkey_base, seed + DEFAULT_RNG_BLK_SZ, AES_KEYSIZE_128);
827 dd->flags = FLAGS_ENCRYPT | FLAGS_RNG;
829 /* take the hardware semaphore */
830 if (tegra_arb_mutex_lock_timeout(dd->res_id, ARB_SEMA_TIMEOUT) < 0) {
831 dev_err(dd->dev, "aes hardware not available\n");
832 mutex_unlock(&aes_lock);
836 ret = aes_hw_init(dd);
838 dev_err(dd->dev, "%s: hw init fail(%d)\n", __func__, ret);
844 /* set seed to the aes hw slot */
845 memset(dd->buf_in, 0, AES_BLOCK_SIZE);
846 memcpy(dd->buf_in, dd->iv, DEFAULT_RNG_BLK_SZ);
847 ret = aes_start_crypt(dd, (u32)dd->dma_buf_in,
848 (u32)dd->dma_buf_out, 1, FLAGS_CBC, false);
850 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
854 if (dd->ivlen >= (2 * DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128)) {
855 dt = dd->iv + DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128;
858 nsec = timespec_to_ns(&ts);
860 nsec ^= dd->ctr << 56;
863 tmp[1] = tegra_chip_uid();
866 memcpy(dd->dt, dt, DEFAULT_RNG_BLK_SZ);
872 /* release the hardware semaphore */
873 tegra_arb_mutex_unlock(dd->res_id);
874 mutex_unlock(&aes_lock);
876 dev_dbg(dd->dev, "%s: done\n", __func__);
880 static int tegra_aes_cra_init(struct crypto_tfm *tfm)
882 tfm->crt_ablkcipher.reqsize = sizeof(struct tegra_aes_reqctx);
887 static struct crypto_alg algs[] = {
889 .cra_name = "disabled_ecb(aes)",
890 .cra_driver_name = "ecb-aes-tegra",
892 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
893 .cra_blocksize = AES_BLOCK_SIZE,
894 .cra_ctxsize = sizeof(struct tegra_aes_ctx),
896 .cra_type = &crypto_ablkcipher_type,
897 .cra_module = THIS_MODULE,
898 .cra_init = tegra_aes_cra_init,
899 .cra_u.ablkcipher = {
900 .min_keysize = AES_MIN_KEY_SIZE,
901 .max_keysize = AES_MAX_KEY_SIZE,
902 .setkey = tegra_aes_setkey,
903 .encrypt = tegra_aes_ecb_encrypt,
904 .decrypt = tegra_aes_ecb_decrypt,
907 .cra_name = "disabled_cbc(aes)",
908 .cra_driver_name = "cbc-aes-tegra",
910 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
911 .cra_blocksize = AES_BLOCK_SIZE,
912 .cra_ctxsize = sizeof(struct tegra_aes_ctx),
914 .cra_type = &crypto_ablkcipher_type,
915 .cra_module = THIS_MODULE,
916 .cra_init = tegra_aes_cra_init,
917 .cra_u.ablkcipher = {
918 .min_keysize = AES_MIN_KEY_SIZE,
919 .max_keysize = AES_MAX_KEY_SIZE,
920 .ivsize = AES_MIN_KEY_SIZE,
921 .setkey = tegra_aes_setkey,
922 .encrypt = tegra_aes_cbc_encrypt,
923 .decrypt = tegra_aes_cbc_decrypt,
926 .cra_name = "disabled_ansi_cprng",
927 .cra_driver_name = "rng-aes-tegra",
929 .cra_flags = CRYPTO_ALG_TYPE_RNG,
930 .cra_ctxsize = sizeof(struct tegra_aes_ctx),
931 .cra_type = &crypto_rng_type,
932 .cra_module = THIS_MODULE,
933 .cra_init = tegra_aes_cra_init,
935 .rng_make_random = tegra_aes_get_random,
936 .rng_reset = tegra_aes_rng_reset,
937 .seedsize = AES_KEYSIZE_128 + (2 * DEFAULT_RNG_BLK_SZ),
942 static int tegra_aes_probe(struct platform_device *pdev)
944 struct device *dev = &pdev->dev;
945 struct tegra_aes_dev *dd;
946 struct resource *res;
947 int err = -ENOMEM, i = 0, j;
952 dd = kzalloc(sizeof(struct tegra_aes_dev), GFP_KERNEL);
954 dev_err(dev, "unable to alloc data struct.\n");
958 platform_set_drvdata(pdev, dd);
960 dd->slots = kzalloc(sizeof(struct tegra_aes_slot) * AES_NR_KEYSLOTS,
962 if (dd->slots == NULL) {
963 dev_err(dev, "unable to alloc slot struct.\n");
967 spin_lock_init(&dd->lock);
968 crypto_init_queue(&dd->queue, TEGRA_AES_QUEUE_LENGTH);
970 /* Get the module base address */
971 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
973 dev_err(dev, "invalid resource type: base\n");
977 dd->phys_base = res->start;
979 dd->io_base = ioremap(dd->phys_base, resource_size(res));
981 dev_err(dev, "can't ioremap phys_base\n");
986 dd->res_id = TEGRA_ARB_AES;
988 /* Initialise the master bsev clock */
989 dd->pclk = clk_get(dev, "bsev");
991 dev_err(dev, "pclock intialization failed.\n");
996 /* Initialize the vde clock */
997 dd->iclk = clk_get(dev, "vde");
999 dev_err(dev, "iclock intialization failed.\n");
1005 * the foll contiguous memory is allocated as follows -
1006 * - hardware key table
1009 dd->ivkey_base = dma_alloc_coherent(dev, SZ_512, &dd->ivkey_phys_base,
1011 if (!dd->ivkey_base) {
1012 dev_err(dev, "can not allocate iv/key buffer\n");
1017 dd->buf_in = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1018 &dd->dma_buf_in, GFP_KERNEL);
1020 dev_err(dev, "can not allocate dma-in buffer\n");
1025 dd->buf_out = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1026 &dd->dma_buf_out, GFP_KERNEL);
1028 dev_err(dev, "can not allocate dma-out buffer\n");
1033 init_completion(&dd->op_complete);
1034 aes_wq = alloc_workqueue("aes_wq", WQ_HIGHPRI, 16);
1036 dev_err(dev, "alloc_workqueue failed\n");
1041 err = request_irq(INT_VDE_BSE_V, aes_irq, IRQF_TRIGGER_HIGH,
1044 dev_err(dev, "request_irq failed\n");
1048 spin_lock_init(&list_lock);
1049 spin_lock(&list_lock);
1050 for (i = 0; i < AES_NR_KEYSLOTS; i++) {
1051 dd->slots[i].available = true;
1052 dd->slots[i].slot_num = i;
1053 INIT_LIST_HEAD(&dd->slots[i].node);
1054 list_add_tail(&dd->slots[i].node, &dev_list);
1056 spin_unlock(&list_lock);
1059 for (i = 0; i < ARRAY_SIZE(algs); i++) {
1060 INIT_LIST_HEAD(&algs[i].cra_list);
1061 err = crypto_register_alg(&algs[i]);
1066 dev_info(dev, "registered");
1070 for (j = 0; j < i; j++)
1071 crypto_unregister_alg(&algs[j]);
1073 dma_free_coherent(dev, SZ_512, dd->ivkey_base,
1074 dd->ivkey_phys_base);
1076 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1077 dd->buf_in, dd->dma_buf_in);
1079 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1080 dd->buf_out, dd->dma_buf_out);
1082 iounmap(dd->io_base);
1088 destroy_workqueue(aes_wq);
1089 free_irq(INT_VDE_BSE_V, dd);
1090 spin_lock(&list_lock);
1091 list_del(&dev_list);
1092 spin_unlock(&list_lock);
1097 dev_err(dev, "%s: initialization failed.\n", __func__);
1101 static int __devexit tegra_aes_remove(struct platform_device *pdev)
1103 struct device *dev = &pdev->dev;
1104 struct tegra_aes_dev *dd = platform_get_drvdata(pdev);
1110 cancel_work_sync(&aes_work);
1111 destroy_workqueue(aes_wq);
1112 free_irq(INT_VDE_BSE_V, dd);
1113 spin_lock(&list_lock);
1114 list_del(&dev_list);
1115 spin_unlock(&list_lock);
1117 for (i = 0; i < ARRAY_SIZE(algs); i++)
1118 crypto_unregister_alg(&algs[i]);
1120 dma_free_coherent(dev, SZ_512, dd->ivkey_base,
1121 dd->ivkey_phys_base);
1122 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1123 dd->buf_in, dd->dma_buf_in);
1124 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1125 dd->buf_out, dd->dma_buf_out);
1126 iounmap(dd->io_base);
1136 static struct platform_driver tegra_aes_driver = {
1137 .probe = tegra_aes_probe,
1138 .remove = __devexit_p(tegra_aes_remove),
1140 .name = "tegra-aes",
1141 .owner = THIS_MODULE,
1145 static int __init tegra_aes_mod_init(void)
1147 mutex_init(&aes_lock);
1148 INIT_LIST_HEAD(&dev_list);
1149 return platform_driver_register(&tegra_aes_driver);
1152 static void __exit tegra_aes_mod_exit(void)
1154 platform_driver_unregister(&tegra_aes_driver);
1157 module_init(tegra_aes_mod_init);
1158 module_exit(tegra_aes_mod_exit);
1160 MODULE_DESCRIPTION("Tegra AES hw acceleration support.");
1161 MODULE_AUTHOR("NVIDIA Corporation");
1162 MODULE_LICENSE("GPLv2");