tegra-aes.c 27 KB

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