mxs-dcp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. * Freescale i.MX23/i.MX28 Data Co-Processor driver
  3. *
  4. * Copyright (C) 2013 Marek Vasut <marex@denx.de>
  5. *
  6. * The code contained herein is licensed under the GNU General Public
  7. * License. You may obtain a copy of the GNU General Public License
  8. * Version 2 or later at the following locations:
  9. *
  10. * http://www.opensource.org/licenses/gpl-license.html
  11. * http://www.gnu.org/copyleft/gpl.html
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kthread.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/stmp_device.h>
  22. #include <crypto/aes.h>
  23. #include <crypto/sha.h>
  24. #include <crypto/internal/hash.h>
  25. #include <crypto/internal/skcipher.h>
  26. #define DCP_MAX_CHANS 4
  27. #define DCP_BUF_SZ PAGE_SIZE
  28. #define DCP_ALIGNMENT 64
  29. /* DCP DMA descriptor. */
  30. struct dcp_dma_desc {
  31. uint32_t next_cmd_addr;
  32. uint32_t control0;
  33. uint32_t control1;
  34. uint32_t source;
  35. uint32_t destination;
  36. uint32_t size;
  37. uint32_t payload;
  38. uint32_t status;
  39. };
  40. /* Coherent aligned block for bounce buffering. */
  41. struct dcp_coherent_block {
  42. uint8_t aes_in_buf[DCP_BUF_SZ];
  43. uint8_t aes_out_buf[DCP_BUF_SZ];
  44. uint8_t sha_in_buf[DCP_BUF_SZ];
  45. uint8_t aes_key[2 * AES_KEYSIZE_128];
  46. struct dcp_dma_desc desc[DCP_MAX_CHANS];
  47. };
  48. struct dcp {
  49. struct device *dev;
  50. void __iomem *base;
  51. uint32_t caps;
  52. struct dcp_coherent_block *coh;
  53. struct completion completion[DCP_MAX_CHANS];
  54. struct mutex mutex[DCP_MAX_CHANS];
  55. struct task_struct *thread[DCP_MAX_CHANS];
  56. struct crypto_queue queue[DCP_MAX_CHANS];
  57. };
  58. enum dcp_chan {
  59. DCP_CHAN_HASH_SHA = 0,
  60. DCP_CHAN_CRYPTO = 2,
  61. };
  62. struct dcp_async_ctx {
  63. /* Common context */
  64. enum dcp_chan chan;
  65. uint32_t fill;
  66. /* SHA Hash-specific context */
  67. struct mutex mutex;
  68. uint32_t alg;
  69. unsigned int hot:1;
  70. /* Crypto-specific context */
  71. struct crypto_skcipher *fallback;
  72. unsigned int key_len;
  73. uint8_t key[AES_KEYSIZE_128];
  74. };
  75. struct dcp_aes_req_ctx {
  76. unsigned int enc:1;
  77. unsigned int ecb:1;
  78. };
  79. struct dcp_sha_req_ctx {
  80. unsigned int init:1;
  81. unsigned int fini:1;
  82. };
  83. /*
  84. * There can even be only one instance of the MXS DCP due to the
  85. * design of Linux Crypto API.
  86. */
  87. static struct dcp *global_sdcp;
  88. /* DCP register layout. */
  89. #define MXS_DCP_CTRL 0x00
  90. #define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
  91. #define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
  92. #define MXS_DCP_STAT 0x10
  93. #define MXS_DCP_STAT_CLR 0x18
  94. #define MXS_DCP_STAT_IRQ_MASK 0xf
  95. #define MXS_DCP_CHANNELCTRL 0x20
  96. #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
  97. #define MXS_DCP_CAPABILITY1 0x40
  98. #define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
  99. #define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
  100. #define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
  101. #define MXS_DCP_CONTEXT 0x50
  102. #define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
  103. #define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
  104. #define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
  105. #define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
  106. /* DMA descriptor bits. */
  107. #define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
  108. #define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
  109. #define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
  110. #define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
  111. #define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
  112. #define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
  113. #define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
  114. #define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
  115. #define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
  116. #define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
  117. #define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
  118. #define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
  119. #define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
  120. #define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
  121. static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
  122. {
  123. struct dcp *sdcp = global_sdcp;
  124. const int chan = actx->chan;
  125. uint32_t stat;
  126. unsigned long ret;
  127. struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
  128. dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc),
  129. DMA_TO_DEVICE);
  130. reinit_completion(&sdcp->completion[chan]);
  131. /* Clear status register. */
  132. writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
  133. /* Load the DMA descriptor. */
  134. writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
  135. /* Increment the semaphore to start the DMA transfer. */
  136. writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
  137. ret = wait_for_completion_timeout(&sdcp->completion[chan],
  138. msecs_to_jiffies(1000));
  139. if (!ret) {
  140. dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n",
  141. chan, readl(sdcp->base + MXS_DCP_STAT));
  142. return -ETIMEDOUT;
  143. }
  144. stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
  145. if (stat & 0xff) {
  146. dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n",
  147. chan, stat);
  148. return -EINVAL;
  149. }
  150. dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
  151. return 0;
  152. }
  153. /*
  154. * Encryption (AES128)
  155. */
  156. static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
  157. struct ablkcipher_request *req, int init)
  158. {
  159. struct dcp *sdcp = global_sdcp;
  160. struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
  161. struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  162. int ret;
  163. dma_addr_t key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key,
  164. 2 * AES_KEYSIZE_128,
  165. DMA_TO_DEVICE);
  166. dma_addr_t src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf,
  167. DCP_BUF_SZ, DMA_TO_DEVICE);
  168. dma_addr_t dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf,
  169. DCP_BUF_SZ, DMA_FROM_DEVICE);
  170. /* Fill in the DMA descriptor. */
  171. desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
  172. MXS_DCP_CONTROL0_INTERRUPT |
  173. MXS_DCP_CONTROL0_ENABLE_CIPHER;
  174. /* Payload contains the key. */
  175. desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
  176. if (rctx->enc)
  177. desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
  178. if (init)
  179. desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
  180. desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
  181. if (rctx->ecb)
  182. desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
  183. else
  184. desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
  185. desc->next_cmd_addr = 0;
  186. desc->source = src_phys;
  187. desc->destination = dst_phys;
  188. desc->size = actx->fill;
  189. desc->payload = key_phys;
  190. desc->status = 0;
  191. ret = mxs_dcp_start_dma(actx);
  192. dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
  193. DMA_TO_DEVICE);
  194. dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
  195. dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
  196. return ret;
  197. }
  198. static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
  199. {
  200. struct dcp *sdcp = global_sdcp;
  201. struct ablkcipher_request *req = ablkcipher_request_cast(arq);
  202. struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
  203. struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  204. struct scatterlist *dst = req->dst;
  205. struct scatterlist *src = req->src;
  206. const int nents = sg_nents(req->src);
  207. const int out_off = DCP_BUF_SZ;
  208. uint8_t *in_buf = sdcp->coh->aes_in_buf;
  209. uint8_t *out_buf = sdcp->coh->aes_out_buf;
  210. uint8_t *out_tmp, *src_buf, *dst_buf = NULL;
  211. uint32_t dst_off = 0;
  212. uint8_t *key = sdcp->coh->aes_key;
  213. int ret = 0;
  214. int split = 0;
  215. unsigned int i, len, clen, rem = 0;
  216. int init = 0;
  217. actx->fill = 0;
  218. /* Copy the key from the temporary location. */
  219. memcpy(key, actx->key, actx->key_len);
  220. if (!rctx->ecb) {
  221. /* Copy the CBC IV just past the key. */
  222. memcpy(key + AES_KEYSIZE_128, req->info, AES_KEYSIZE_128);
  223. /* CBC needs the INIT set. */
  224. init = 1;
  225. } else {
  226. memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
  227. }
  228. for_each_sg(req->src, src, nents, i) {
  229. src_buf = sg_virt(src);
  230. len = sg_dma_len(src);
  231. do {
  232. if (actx->fill + len > out_off)
  233. clen = out_off - actx->fill;
  234. else
  235. clen = len;
  236. memcpy(in_buf + actx->fill, src_buf, clen);
  237. len -= clen;
  238. src_buf += clen;
  239. actx->fill += clen;
  240. /*
  241. * If we filled the buffer or this is the last SG,
  242. * submit the buffer.
  243. */
  244. if (actx->fill == out_off || sg_is_last(src)) {
  245. ret = mxs_dcp_run_aes(actx, req, init);
  246. if (ret)
  247. return ret;
  248. init = 0;
  249. out_tmp = out_buf;
  250. while (dst && actx->fill) {
  251. if (!split) {
  252. dst_buf = sg_virt(dst);
  253. dst_off = 0;
  254. }
  255. rem = min(sg_dma_len(dst) - dst_off,
  256. actx->fill);
  257. memcpy(dst_buf + dst_off, out_tmp, rem);
  258. out_tmp += rem;
  259. dst_off += rem;
  260. actx->fill -= rem;
  261. if (dst_off == sg_dma_len(dst)) {
  262. dst = sg_next(dst);
  263. split = 0;
  264. } else {
  265. split = 1;
  266. }
  267. }
  268. }
  269. } while (len);
  270. }
  271. return ret;
  272. }
  273. static int dcp_chan_thread_aes(void *data)
  274. {
  275. struct dcp *sdcp = global_sdcp;
  276. const int chan = DCP_CHAN_CRYPTO;
  277. struct crypto_async_request *backlog;
  278. struct crypto_async_request *arq;
  279. int ret;
  280. do {
  281. __set_current_state(TASK_INTERRUPTIBLE);
  282. mutex_lock(&sdcp->mutex[chan]);
  283. backlog = crypto_get_backlog(&sdcp->queue[chan]);
  284. arq = crypto_dequeue_request(&sdcp->queue[chan]);
  285. mutex_unlock(&sdcp->mutex[chan]);
  286. if (backlog)
  287. backlog->complete(backlog, -EINPROGRESS);
  288. if (arq) {
  289. ret = mxs_dcp_aes_block_crypt(arq);
  290. arq->complete(arq, ret);
  291. continue;
  292. }
  293. schedule();
  294. } while (!kthread_should_stop());
  295. return 0;
  296. }
  297. static int mxs_dcp_block_fallback(struct ablkcipher_request *req, int enc)
  298. {
  299. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  300. struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  301. SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
  302. int ret;
  303. skcipher_request_set_tfm(subreq, ctx->fallback);
  304. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  305. skcipher_request_set_crypt(subreq, req->src, req->dst,
  306. req->nbytes, req->info);
  307. if (enc)
  308. ret = crypto_skcipher_encrypt(subreq);
  309. else
  310. ret = crypto_skcipher_decrypt(subreq);
  311. skcipher_request_zero(subreq);
  312. return ret;
  313. }
  314. static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb)
  315. {
  316. struct dcp *sdcp = global_sdcp;
  317. struct crypto_async_request *arq = &req->base;
  318. struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
  319. struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  320. int ret;
  321. if (unlikely(actx->key_len != AES_KEYSIZE_128))
  322. return mxs_dcp_block_fallback(req, enc);
  323. rctx->enc = enc;
  324. rctx->ecb = ecb;
  325. actx->chan = DCP_CHAN_CRYPTO;
  326. mutex_lock(&sdcp->mutex[actx->chan]);
  327. ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
  328. mutex_unlock(&sdcp->mutex[actx->chan]);
  329. wake_up_process(sdcp->thread[actx->chan]);
  330. return -EINPROGRESS;
  331. }
  332. static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req)
  333. {
  334. return mxs_dcp_aes_enqueue(req, 0, 1);
  335. }
  336. static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req)
  337. {
  338. return mxs_dcp_aes_enqueue(req, 1, 1);
  339. }
  340. static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req)
  341. {
  342. return mxs_dcp_aes_enqueue(req, 0, 0);
  343. }
  344. static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req)
  345. {
  346. return mxs_dcp_aes_enqueue(req, 1, 0);
  347. }
  348. static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  349. unsigned int len)
  350. {
  351. struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm);
  352. unsigned int ret;
  353. /*
  354. * AES 128 is supposed by the hardware, store key into temporary
  355. * buffer and exit. We must use the temporary buffer here, since
  356. * there can still be an operation in progress.
  357. */
  358. actx->key_len = len;
  359. if (len == AES_KEYSIZE_128) {
  360. memcpy(actx->key, key, len);
  361. return 0;
  362. }
  363. /*
  364. * If the requested AES key size is not supported by the hardware,
  365. * but is supported by in-kernel software implementation, we use
  366. * software fallback.
  367. */
  368. crypto_skcipher_clear_flags(actx->fallback, CRYPTO_TFM_REQ_MASK);
  369. crypto_skcipher_set_flags(actx->fallback,
  370. tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
  371. ret = crypto_skcipher_setkey(actx->fallback, key, len);
  372. if (!ret)
  373. return 0;
  374. tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
  375. tfm->base.crt_flags |= crypto_skcipher_get_flags(actx->fallback) &
  376. CRYPTO_TFM_RES_MASK;
  377. return ret;
  378. }
  379. static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm)
  380. {
  381. const char *name = crypto_tfm_alg_name(tfm);
  382. const uint32_t flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
  383. struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
  384. struct crypto_skcipher *blk;
  385. blk = crypto_alloc_skcipher(name, 0, flags);
  386. if (IS_ERR(blk))
  387. return PTR_ERR(blk);
  388. actx->fallback = blk;
  389. tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx);
  390. return 0;
  391. }
  392. static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm)
  393. {
  394. struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
  395. crypto_free_skcipher(actx->fallback);
  396. }
  397. /*
  398. * Hashing (SHA1/SHA256)
  399. */
  400. static int mxs_dcp_run_sha(struct ahash_request *req)
  401. {
  402. struct dcp *sdcp = global_sdcp;
  403. int ret;
  404. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  405. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  406. struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  407. struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  408. struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
  409. dma_addr_t digest_phys = 0;
  410. dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
  411. DCP_BUF_SZ, DMA_TO_DEVICE);
  412. /* Fill in the DMA descriptor. */
  413. desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
  414. MXS_DCP_CONTROL0_INTERRUPT |
  415. MXS_DCP_CONTROL0_ENABLE_HASH;
  416. if (rctx->init)
  417. desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
  418. desc->control1 = actx->alg;
  419. desc->next_cmd_addr = 0;
  420. desc->source = buf_phys;
  421. desc->destination = 0;
  422. desc->size = actx->fill;
  423. desc->payload = 0;
  424. desc->status = 0;
  425. /* Set HASH_TERM bit for last transfer block. */
  426. if (rctx->fini) {
  427. digest_phys = dma_map_single(sdcp->dev, req->result,
  428. halg->digestsize, DMA_FROM_DEVICE);
  429. desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
  430. desc->payload = digest_phys;
  431. }
  432. ret = mxs_dcp_start_dma(actx);
  433. if (rctx->fini)
  434. dma_unmap_single(sdcp->dev, digest_phys, halg->digestsize,
  435. DMA_FROM_DEVICE);
  436. dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
  437. return ret;
  438. }
  439. static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
  440. {
  441. struct dcp *sdcp = global_sdcp;
  442. struct ahash_request *req = ahash_request_cast(arq);
  443. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  444. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  445. struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  446. struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  447. const int nents = sg_nents(req->src);
  448. uint8_t *in_buf = sdcp->coh->sha_in_buf;
  449. uint8_t *src_buf;
  450. struct scatterlist *src;
  451. unsigned int i, len, clen;
  452. int ret;
  453. int fin = rctx->fini;
  454. if (fin)
  455. rctx->fini = 0;
  456. for_each_sg(req->src, src, nents, i) {
  457. src_buf = sg_virt(src);
  458. len = sg_dma_len(src);
  459. do {
  460. if (actx->fill + len > DCP_BUF_SZ)
  461. clen = DCP_BUF_SZ - actx->fill;
  462. else
  463. clen = len;
  464. memcpy(in_buf + actx->fill, src_buf, clen);
  465. len -= clen;
  466. src_buf += clen;
  467. actx->fill += clen;
  468. /*
  469. * If we filled the buffer and still have some
  470. * more data, submit the buffer.
  471. */
  472. if (len && actx->fill == DCP_BUF_SZ) {
  473. ret = mxs_dcp_run_sha(req);
  474. if (ret)
  475. return ret;
  476. actx->fill = 0;
  477. rctx->init = 0;
  478. }
  479. } while (len);
  480. }
  481. if (fin) {
  482. rctx->fini = 1;
  483. /* Submit whatever is left. */
  484. if (!req->result)
  485. return -EINVAL;
  486. ret = mxs_dcp_run_sha(req);
  487. if (ret)
  488. return ret;
  489. actx->fill = 0;
  490. /* For some reason, the result is flipped. */
  491. for (i = 0; i < halg->digestsize / 2; i++) {
  492. swap(req->result[i],
  493. req->result[halg->digestsize - i - 1]);
  494. }
  495. }
  496. return 0;
  497. }
  498. static int dcp_chan_thread_sha(void *data)
  499. {
  500. struct dcp *sdcp = global_sdcp;
  501. const int chan = DCP_CHAN_HASH_SHA;
  502. struct crypto_async_request *backlog;
  503. struct crypto_async_request *arq;
  504. struct dcp_sha_req_ctx *rctx;
  505. struct ahash_request *req;
  506. int ret, fini;
  507. do {
  508. __set_current_state(TASK_INTERRUPTIBLE);
  509. mutex_lock(&sdcp->mutex[chan]);
  510. backlog = crypto_get_backlog(&sdcp->queue[chan]);
  511. arq = crypto_dequeue_request(&sdcp->queue[chan]);
  512. mutex_unlock(&sdcp->mutex[chan]);
  513. if (backlog)
  514. backlog->complete(backlog, -EINPROGRESS);
  515. if (arq) {
  516. req = ahash_request_cast(arq);
  517. rctx = ahash_request_ctx(req);
  518. ret = dcp_sha_req_to_buf(arq);
  519. fini = rctx->fini;
  520. arq->complete(arq, ret);
  521. if (!fini)
  522. continue;
  523. }
  524. schedule();
  525. } while (!kthread_should_stop());
  526. return 0;
  527. }
  528. static int dcp_sha_init(struct ahash_request *req)
  529. {
  530. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  531. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  532. struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  533. /*
  534. * Start hashing session. The code below only inits the
  535. * hashing session context, nothing more.
  536. */
  537. memset(actx, 0, sizeof(*actx));
  538. if (strcmp(halg->base.cra_name, "sha1") == 0)
  539. actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
  540. else
  541. actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
  542. actx->fill = 0;
  543. actx->hot = 0;
  544. actx->chan = DCP_CHAN_HASH_SHA;
  545. mutex_init(&actx->mutex);
  546. return 0;
  547. }
  548. static int dcp_sha_update_fx(struct ahash_request *req, int fini)
  549. {
  550. struct dcp *sdcp = global_sdcp;
  551. struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  552. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  553. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  554. int ret;
  555. /*
  556. * Ignore requests that have no data in them and are not
  557. * the trailing requests in the stream of requests.
  558. */
  559. if (!req->nbytes && !fini)
  560. return 0;
  561. mutex_lock(&actx->mutex);
  562. rctx->fini = fini;
  563. if (!actx->hot) {
  564. actx->hot = 1;
  565. rctx->init = 1;
  566. }
  567. mutex_lock(&sdcp->mutex[actx->chan]);
  568. ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
  569. mutex_unlock(&sdcp->mutex[actx->chan]);
  570. wake_up_process(sdcp->thread[actx->chan]);
  571. mutex_unlock(&actx->mutex);
  572. return -EINPROGRESS;
  573. }
  574. static int dcp_sha_update(struct ahash_request *req)
  575. {
  576. return dcp_sha_update_fx(req, 0);
  577. }
  578. static int dcp_sha_final(struct ahash_request *req)
  579. {
  580. ahash_request_set_crypt(req, NULL, req->result, 0);
  581. req->nbytes = 0;
  582. return dcp_sha_update_fx(req, 1);
  583. }
  584. static int dcp_sha_finup(struct ahash_request *req)
  585. {
  586. return dcp_sha_update_fx(req, 1);
  587. }
  588. static int dcp_sha_digest(struct ahash_request *req)
  589. {
  590. int ret;
  591. ret = dcp_sha_init(req);
  592. if (ret)
  593. return ret;
  594. return dcp_sha_finup(req);
  595. }
  596. static int dcp_sha_cra_init(struct crypto_tfm *tfm)
  597. {
  598. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  599. sizeof(struct dcp_sha_req_ctx));
  600. return 0;
  601. }
  602. static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
  603. {
  604. }
  605. /* AES 128 ECB and AES 128 CBC */
  606. static struct crypto_alg dcp_aes_algs[] = {
  607. {
  608. .cra_name = "ecb(aes)",
  609. .cra_driver_name = "ecb-aes-dcp",
  610. .cra_priority = 400,
  611. .cra_alignmask = 15,
  612. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  613. CRYPTO_ALG_ASYNC |
  614. CRYPTO_ALG_NEED_FALLBACK,
  615. .cra_init = mxs_dcp_aes_fallback_init,
  616. .cra_exit = mxs_dcp_aes_fallback_exit,
  617. .cra_blocksize = AES_BLOCK_SIZE,
  618. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  619. .cra_type = &crypto_ablkcipher_type,
  620. .cra_module = THIS_MODULE,
  621. .cra_u = {
  622. .ablkcipher = {
  623. .min_keysize = AES_MIN_KEY_SIZE,
  624. .max_keysize = AES_MAX_KEY_SIZE,
  625. .setkey = mxs_dcp_aes_setkey,
  626. .encrypt = mxs_dcp_aes_ecb_encrypt,
  627. .decrypt = mxs_dcp_aes_ecb_decrypt
  628. },
  629. },
  630. }, {
  631. .cra_name = "cbc(aes)",
  632. .cra_driver_name = "cbc-aes-dcp",
  633. .cra_priority = 400,
  634. .cra_alignmask = 15,
  635. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  636. CRYPTO_ALG_ASYNC |
  637. CRYPTO_ALG_NEED_FALLBACK,
  638. .cra_init = mxs_dcp_aes_fallback_init,
  639. .cra_exit = mxs_dcp_aes_fallback_exit,
  640. .cra_blocksize = AES_BLOCK_SIZE,
  641. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  642. .cra_type = &crypto_ablkcipher_type,
  643. .cra_module = THIS_MODULE,
  644. .cra_u = {
  645. .ablkcipher = {
  646. .min_keysize = AES_MIN_KEY_SIZE,
  647. .max_keysize = AES_MAX_KEY_SIZE,
  648. .setkey = mxs_dcp_aes_setkey,
  649. .encrypt = mxs_dcp_aes_cbc_encrypt,
  650. .decrypt = mxs_dcp_aes_cbc_decrypt,
  651. .ivsize = AES_BLOCK_SIZE,
  652. },
  653. },
  654. },
  655. };
  656. /* SHA1 */
  657. static struct ahash_alg dcp_sha1_alg = {
  658. .init = dcp_sha_init,
  659. .update = dcp_sha_update,
  660. .final = dcp_sha_final,
  661. .finup = dcp_sha_finup,
  662. .digest = dcp_sha_digest,
  663. .halg = {
  664. .digestsize = SHA1_DIGEST_SIZE,
  665. .base = {
  666. .cra_name = "sha1",
  667. .cra_driver_name = "sha1-dcp",
  668. .cra_priority = 400,
  669. .cra_alignmask = 63,
  670. .cra_flags = CRYPTO_ALG_ASYNC,
  671. .cra_blocksize = SHA1_BLOCK_SIZE,
  672. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  673. .cra_module = THIS_MODULE,
  674. .cra_init = dcp_sha_cra_init,
  675. .cra_exit = dcp_sha_cra_exit,
  676. },
  677. },
  678. };
  679. /* SHA256 */
  680. static struct ahash_alg dcp_sha256_alg = {
  681. .init = dcp_sha_init,
  682. .update = dcp_sha_update,
  683. .final = dcp_sha_final,
  684. .finup = dcp_sha_finup,
  685. .digest = dcp_sha_digest,
  686. .halg = {
  687. .digestsize = SHA256_DIGEST_SIZE,
  688. .base = {
  689. .cra_name = "sha256",
  690. .cra_driver_name = "sha256-dcp",
  691. .cra_priority = 400,
  692. .cra_alignmask = 63,
  693. .cra_flags = CRYPTO_ALG_ASYNC,
  694. .cra_blocksize = SHA256_BLOCK_SIZE,
  695. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  696. .cra_module = THIS_MODULE,
  697. .cra_init = dcp_sha_cra_init,
  698. .cra_exit = dcp_sha_cra_exit,
  699. },
  700. },
  701. };
  702. static irqreturn_t mxs_dcp_irq(int irq, void *context)
  703. {
  704. struct dcp *sdcp = context;
  705. uint32_t stat;
  706. int i;
  707. stat = readl(sdcp->base + MXS_DCP_STAT);
  708. stat &= MXS_DCP_STAT_IRQ_MASK;
  709. if (!stat)
  710. return IRQ_NONE;
  711. /* Clear the interrupts. */
  712. writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
  713. /* Complete the DMA requests that finished. */
  714. for (i = 0; i < DCP_MAX_CHANS; i++)
  715. if (stat & (1 << i))
  716. complete(&sdcp->completion[i]);
  717. return IRQ_HANDLED;
  718. }
  719. static int mxs_dcp_probe(struct platform_device *pdev)
  720. {
  721. struct device *dev = &pdev->dev;
  722. struct dcp *sdcp = NULL;
  723. int i, ret;
  724. struct resource *iores;
  725. int dcp_vmi_irq, dcp_irq;
  726. if (global_sdcp) {
  727. dev_err(dev, "Only one DCP instance allowed!\n");
  728. return -ENODEV;
  729. }
  730. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  731. dcp_vmi_irq = platform_get_irq(pdev, 0);
  732. if (dcp_vmi_irq < 0)
  733. return dcp_vmi_irq;
  734. dcp_irq = platform_get_irq(pdev, 1);
  735. if (dcp_irq < 0)
  736. return dcp_irq;
  737. sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
  738. if (!sdcp)
  739. return -ENOMEM;
  740. sdcp->dev = dev;
  741. sdcp->base = devm_ioremap_resource(dev, iores);
  742. if (IS_ERR(sdcp->base))
  743. return PTR_ERR(sdcp->base);
  744. ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
  745. "dcp-vmi-irq", sdcp);
  746. if (ret) {
  747. dev_err(dev, "Failed to claim DCP VMI IRQ!\n");
  748. return ret;
  749. }
  750. ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
  751. "dcp-irq", sdcp);
  752. if (ret) {
  753. dev_err(dev, "Failed to claim DCP IRQ!\n");
  754. return ret;
  755. }
  756. /* Allocate coherent helper block. */
  757. sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
  758. GFP_KERNEL);
  759. if (!sdcp->coh)
  760. return -ENOMEM;
  761. /* Re-align the structure so it fits the DCP constraints. */
  762. sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
  763. /* Restart the DCP block. */
  764. ret = stmp_reset_block(sdcp->base);
  765. if (ret)
  766. return ret;
  767. /* Initialize control register. */
  768. writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
  769. MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
  770. sdcp->base + MXS_DCP_CTRL);
  771. /* Enable all DCP DMA channels. */
  772. writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
  773. sdcp->base + MXS_DCP_CHANNELCTRL);
  774. /*
  775. * We do not enable context switching. Give the context buffer a
  776. * pointer to an illegal address so if context switching is
  777. * inadvertantly enabled, the DCP will return an error instead of
  778. * trashing good memory. The DCP DMA cannot access ROM, so any ROM
  779. * address will do.
  780. */
  781. writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
  782. for (i = 0; i < DCP_MAX_CHANS; i++)
  783. writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
  784. writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
  785. global_sdcp = sdcp;
  786. platform_set_drvdata(pdev, sdcp);
  787. for (i = 0; i < DCP_MAX_CHANS; i++) {
  788. mutex_init(&sdcp->mutex[i]);
  789. init_completion(&sdcp->completion[i]);
  790. crypto_init_queue(&sdcp->queue[i], 50);
  791. }
  792. /* Create the SHA and AES handler threads. */
  793. sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
  794. NULL, "mxs_dcp_chan/sha");
  795. if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
  796. dev_err(dev, "Error starting SHA thread!\n");
  797. return PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
  798. }
  799. sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
  800. NULL, "mxs_dcp_chan/aes");
  801. if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
  802. dev_err(dev, "Error starting SHA thread!\n");
  803. ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
  804. goto err_destroy_sha_thread;
  805. }
  806. /* Register the various crypto algorithms. */
  807. sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
  808. if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
  809. ret = crypto_register_algs(dcp_aes_algs,
  810. ARRAY_SIZE(dcp_aes_algs));
  811. if (ret) {
  812. /* Failed to register algorithm. */
  813. dev_err(dev, "Failed to register AES crypto!\n");
  814. goto err_destroy_aes_thread;
  815. }
  816. }
  817. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
  818. ret = crypto_register_ahash(&dcp_sha1_alg);
  819. if (ret) {
  820. dev_err(dev, "Failed to register %s hash!\n",
  821. dcp_sha1_alg.halg.base.cra_name);
  822. goto err_unregister_aes;
  823. }
  824. }
  825. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
  826. ret = crypto_register_ahash(&dcp_sha256_alg);
  827. if (ret) {
  828. dev_err(dev, "Failed to register %s hash!\n",
  829. dcp_sha256_alg.halg.base.cra_name);
  830. goto err_unregister_sha1;
  831. }
  832. }
  833. return 0;
  834. err_unregister_sha1:
  835. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
  836. crypto_unregister_ahash(&dcp_sha1_alg);
  837. err_unregister_aes:
  838. if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
  839. crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
  840. err_destroy_aes_thread:
  841. kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
  842. err_destroy_sha_thread:
  843. kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
  844. return ret;
  845. }
  846. static int mxs_dcp_remove(struct platform_device *pdev)
  847. {
  848. struct dcp *sdcp = platform_get_drvdata(pdev);
  849. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
  850. crypto_unregister_ahash(&dcp_sha256_alg);
  851. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
  852. crypto_unregister_ahash(&dcp_sha1_alg);
  853. if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
  854. crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
  855. kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
  856. kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
  857. platform_set_drvdata(pdev, NULL);
  858. global_sdcp = NULL;
  859. return 0;
  860. }
  861. static const struct of_device_id mxs_dcp_dt_ids[] = {
  862. { .compatible = "fsl,imx23-dcp", .data = NULL, },
  863. { .compatible = "fsl,imx28-dcp", .data = NULL, },
  864. { /* sentinel */ }
  865. };
  866. MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
  867. static struct platform_driver mxs_dcp_driver = {
  868. .probe = mxs_dcp_probe,
  869. .remove = mxs_dcp_remove,
  870. .driver = {
  871. .name = "mxs-dcp",
  872. .of_match_table = mxs_dcp_dt_ids,
  873. },
  874. };
  875. module_platform_driver(mxs_dcp_driver);
  876. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  877. MODULE_DESCRIPTION("Freescale MXS DCP Driver");
  878. MODULE_LICENSE("GPL");
  879. MODULE_ALIAS("platform:mxs-dcp");