tegra-aes.c 27 KB

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