tegra-aes.c 27 KB

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