mxs-dcp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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/crypto.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kthread.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/stmp_device.h>
  23. #include <crypto/aes.h>
  24. #include <crypto/sha.h>
  25. #include <crypto/internal/hash.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_ablkcipher *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_tfm *tfm =
  300. crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
  301. struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(
  302. crypto_ablkcipher_reqtfm(req));
  303. int ret;
  304. ablkcipher_request_set_tfm(req, ctx->fallback);
  305. if (enc)
  306. ret = crypto_ablkcipher_encrypt(req);
  307. else
  308. ret = crypto_ablkcipher_decrypt(req);
  309. ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
  310. return ret;
  311. }
  312. static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb)
  313. {
  314. struct dcp *sdcp = global_sdcp;
  315. struct crypto_async_request *arq = &req->base;
  316. struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
  317. struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  318. int ret;
  319. if (unlikely(actx->key_len != AES_KEYSIZE_128))
  320. return mxs_dcp_block_fallback(req, enc);
  321. rctx->enc = enc;
  322. rctx->ecb = ecb;
  323. actx->chan = DCP_CHAN_CRYPTO;
  324. mutex_lock(&sdcp->mutex[actx->chan]);
  325. ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
  326. mutex_unlock(&sdcp->mutex[actx->chan]);
  327. wake_up_process(sdcp->thread[actx->chan]);
  328. return -EINPROGRESS;
  329. }
  330. static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req)
  331. {
  332. return mxs_dcp_aes_enqueue(req, 0, 1);
  333. }
  334. static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req)
  335. {
  336. return mxs_dcp_aes_enqueue(req, 1, 1);
  337. }
  338. static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req)
  339. {
  340. return mxs_dcp_aes_enqueue(req, 0, 0);
  341. }
  342. static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req)
  343. {
  344. return mxs_dcp_aes_enqueue(req, 1, 0);
  345. }
  346. static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  347. unsigned int len)
  348. {
  349. struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm);
  350. unsigned int ret;
  351. /*
  352. * AES 128 is supposed by the hardware, store key into temporary
  353. * buffer and exit. We must use the temporary buffer here, since
  354. * there can still be an operation in progress.
  355. */
  356. actx->key_len = len;
  357. if (len == AES_KEYSIZE_128) {
  358. memcpy(actx->key, key, len);
  359. return 0;
  360. }
  361. /* Check if the key size is supported by kernel at all. */
  362. if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
  363. tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  364. return -EINVAL;
  365. }
  366. /*
  367. * If the requested AES key size is not supported by the hardware,
  368. * but is supported by in-kernel software implementation, we use
  369. * software fallback.
  370. */
  371. actx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
  372. actx->fallback->base.crt_flags |=
  373. tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK;
  374. ret = crypto_ablkcipher_setkey(actx->fallback, key, len);
  375. if (!ret)
  376. return 0;
  377. tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
  378. tfm->base.crt_flags |=
  379. actx->fallback->base.crt_flags & CRYPTO_TFM_RES_MASK;
  380. return ret;
  381. }
  382. static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm)
  383. {
  384. const char *name = crypto_tfm_alg_name(tfm);
  385. const uint32_t flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
  386. struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
  387. struct crypto_ablkcipher *blk;
  388. blk = crypto_alloc_ablkcipher(name, 0, flags);
  389. if (IS_ERR(blk))
  390. return PTR_ERR(blk);
  391. actx->fallback = blk;
  392. tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx);
  393. return 0;
  394. }
  395. static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm)
  396. {
  397. struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
  398. crypto_free_ablkcipher(actx->fallback);
  399. actx->fallback = NULL;
  400. }
  401. /*
  402. * Hashing (SHA1/SHA256)
  403. */
  404. static int mxs_dcp_run_sha(struct ahash_request *req)
  405. {
  406. struct dcp *sdcp = global_sdcp;
  407. int ret;
  408. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  409. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  410. struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  411. struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  412. struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
  413. dma_addr_t digest_phys = 0;
  414. dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
  415. DCP_BUF_SZ, DMA_TO_DEVICE);
  416. /* Fill in the DMA descriptor. */
  417. desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
  418. MXS_DCP_CONTROL0_INTERRUPT |
  419. MXS_DCP_CONTROL0_ENABLE_HASH;
  420. if (rctx->init)
  421. desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
  422. desc->control1 = actx->alg;
  423. desc->next_cmd_addr = 0;
  424. desc->source = buf_phys;
  425. desc->destination = 0;
  426. desc->size = actx->fill;
  427. desc->payload = 0;
  428. desc->status = 0;
  429. /* Set HASH_TERM bit for last transfer block. */
  430. if (rctx->fini) {
  431. digest_phys = dma_map_single(sdcp->dev, req->result,
  432. halg->digestsize, DMA_FROM_DEVICE);
  433. desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
  434. desc->payload = digest_phys;
  435. }
  436. ret = mxs_dcp_start_dma(actx);
  437. if (rctx->fini)
  438. dma_unmap_single(sdcp->dev, digest_phys, halg->digestsize,
  439. DMA_FROM_DEVICE);
  440. dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
  441. return ret;
  442. }
  443. static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
  444. {
  445. struct dcp *sdcp = global_sdcp;
  446. struct ahash_request *req = ahash_request_cast(arq);
  447. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  448. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  449. struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  450. struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  451. const int nents = sg_nents(req->src);
  452. uint8_t *in_buf = sdcp->coh->sha_in_buf;
  453. uint8_t *src_buf;
  454. struct scatterlist *src;
  455. unsigned int i, len, clen;
  456. int ret;
  457. int fin = rctx->fini;
  458. if (fin)
  459. rctx->fini = 0;
  460. for_each_sg(req->src, src, nents, i) {
  461. src_buf = sg_virt(src);
  462. len = sg_dma_len(src);
  463. do {
  464. if (actx->fill + len > DCP_BUF_SZ)
  465. clen = DCP_BUF_SZ - actx->fill;
  466. else
  467. clen = len;
  468. memcpy(in_buf + actx->fill, src_buf, clen);
  469. len -= clen;
  470. src_buf += clen;
  471. actx->fill += clen;
  472. /*
  473. * If we filled the buffer and still have some
  474. * more data, submit the buffer.
  475. */
  476. if (len && actx->fill == DCP_BUF_SZ) {
  477. ret = mxs_dcp_run_sha(req);
  478. if (ret)
  479. return ret;
  480. actx->fill = 0;
  481. rctx->init = 0;
  482. }
  483. } while (len);
  484. }
  485. if (fin) {
  486. rctx->fini = 1;
  487. /* Submit whatever is left. */
  488. if (!req->result)
  489. return -EINVAL;
  490. ret = mxs_dcp_run_sha(req);
  491. if (ret)
  492. return ret;
  493. actx->fill = 0;
  494. /* For some reason, the result is flipped. */
  495. for (i = 0; i < halg->digestsize / 2; i++) {
  496. swap(req->result[i],
  497. req->result[halg->digestsize - i - 1]);
  498. }
  499. }
  500. return 0;
  501. }
  502. static int dcp_chan_thread_sha(void *data)
  503. {
  504. struct dcp *sdcp = global_sdcp;
  505. const int chan = DCP_CHAN_HASH_SHA;
  506. struct crypto_async_request *backlog;
  507. struct crypto_async_request *arq;
  508. struct dcp_sha_req_ctx *rctx;
  509. struct ahash_request *req;
  510. int ret, fini;
  511. do {
  512. __set_current_state(TASK_INTERRUPTIBLE);
  513. mutex_lock(&sdcp->mutex[chan]);
  514. backlog = crypto_get_backlog(&sdcp->queue[chan]);
  515. arq = crypto_dequeue_request(&sdcp->queue[chan]);
  516. mutex_unlock(&sdcp->mutex[chan]);
  517. if (backlog)
  518. backlog->complete(backlog, -EINPROGRESS);
  519. if (arq) {
  520. req = ahash_request_cast(arq);
  521. rctx = ahash_request_ctx(req);
  522. ret = dcp_sha_req_to_buf(arq);
  523. fini = rctx->fini;
  524. arq->complete(arq, ret);
  525. if (!fini)
  526. continue;
  527. }
  528. schedule();
  529. } while (!kthread_should_stop());
  530. return 0;
  531. }
  532. static int dcp_sha_init(struct ahash_request *req)
  533. {
  534. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  535. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  536. struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  537. /*
  538. * Start hashing session. The code below only inits the
  539. * hashing session context, nothing more.
  540. */
  541. memset(actx, 0, sizeof(*actx));
  542. if (strcmp(halg->base.cra_name, "sha1") == 0)
  543. actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
  544. else
  545. actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
  546. actx->fill = 0;
  547. actx->hot = 0;
  548. actx->chan = DCP_CHAN_HASH_SHA;
  549. mutex_init(&actx->mutex);
  550. return 0;
  551. }
  552. static int dcp_sha_update_fx(struct ahash_request *req, int fini)
  553. {
  554. struct dcp *sdcp = global_sdcp;
  555. struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  556. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  557. struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  558. int ret;
  559. /*
  560. * Ignore requests that have no data in them and are not
  561. * the trailing requests in the stream of requests.
  562. */
  563. if (!req->nbytes && !fini)
  564. return 0;
  565. mutex_lock(&actx->mutex);
  566. rctx->fini = fini;
  567. if (!actx->hot) {
  568. actx->hot = 1;
  569. rctx->init = 1;
  570. }
  571. mutex_lock(&sdcp->mutex[actx->chan]);
  572. ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
  573. mutex_unlock(&sdcp->mutex[actx->chan]);
  574. wake_up_process(sdcp->thread[actx->chan]);
  575. mutex_unlock(&actx->mutex);
  576. return -EINPROGRESS;
  577. }
  578. static int dcp_sha_update(struct ahash_request *req)
  579. {
  580. return dcp_sha_update_fx(req, 0);
  581. }
  582. static int dcp_sha_final(struct ahash_request *req)
  583. {
  584. ahash_request_set_crypt(req, NULL, req->result, 0);
  585. req->nbytes = 0;
  586. return dcp_sha_update_fx(req, 1);
  587. }
  588. static int dcp_sha_finup(struct ahash_request *req)
  589. {
  590. return dcp_sha_update_fx(req, 1);
  591. }
  592. static int dcp_sha_digest(struct ahash_request *req)
  593. {
  594. int ret;
  595. ret = dcp_sha_init(req);
  596. if (ret)
  597. return ret;
  598. return dcp_sha_finup(req);
  599. }
  600. static int dcp_sha_cra_init(struct crypto_tfm *tfm)
  601. {
  602. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  603. sizeof(struct dcp_sha_req_ctx));
  604. return 0;
  605. }
  606. static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
  607. {
  608. }
  609. /* AES 128 ECB and AES 128 CBC */
  610. static struct crypto_alg dcp_aes_algs[] = {
  611. {
  612. .cra_name = "ecb(aes)",
  613. .cra_driver_name = "ecb-aes-dcp",
  614. .cra_priority = 400,
  615. .cra_alignmask = 15,
  616. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  617. CRYPTO_ALG_ASYNC |
  618. CRYPTO_ALG_NEED_FALLBACK,
  619. .cra_init = mxs_dcp_aes_fallback_init,
  620. .cra_exit = mxs_dcp_aes_fallback_exit,
  621. .cra_blocksize = AES_BLOCK_SIZE,
  622. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  623. .cra_type = &crypto_ablkcipher_type,
  624. .cra_module = THIS_MODULE,
  625. .cra_u = {
  626. .ablkcipher = {
  627. .min_keysize = AES_MIN_KEY_SIZE,
  628. .max_keysize = AES_MAX_KEY_SIZE,
  629. .setkey = mxs_dcp_aes_setkey,
  630. .encrypt = mxs_dcp_aes_ecb_encrypt,
  631. .decrypt = mxs_dcp_aes_ecb_decrypt
  632. },
  633. },
  634. }, {
  635. .cra_name = "cbc(aes)",
  636. .cra_driver_name = "cbc-aes-dcp",
  637. .cra_priority = 400,
  638. .cra_alignmask = 15,
  639. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  640. CRYPTO_ALG_ASYNC |
  641. CRYPTO_ALG_NEED_FALLBACK,
  642. .cra_init = mxs_dcp_aes_fallback_init,
  643. .cra_exit = mxs_dcp_aes_fallback_exit,
  644. .cra_blocksize = AES_BLOCK_SIZE,
  645. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  646. .cra_type = &crypto_ablkcipher_type,
  647. .cra_module = THIS_MODULE,
  648. .cra_u = {
  649. .ablkcipher = {
  650. .min_keysize = AES_MIN_KEY_SIZE,
  651. .max_keysize = AES_MAX_KEY_SIZE,
  652. .setkey = mxs_dcp_aes_setkey,
  653. .encrypt = mxs_dcp_aes_cbc_encrypt,
  654. .decrypt = mxs_dcp_aes_cbc_decrypt,
  655. .ivsize = AES_BLOCK_SIZE,
  656. },
  657. },
  658. },
  659. };
  660. /* SHA1 */
  661. static struct ahash_alg dcp_sha1_alg = {
  662. .init = dcp_sha_init,
  663. .update = dcp_sha_update,
  664. .final = dcp_sha_final,
  665. .finup = dcp_sha_finup,
  666. .digest = dcp_sha_digest,
  667. .halg = {
  668. .digestsize = SHA1_DIGEST_SIZE,
  669. .base = {
  670. .cra_name = "sha1",
  671. .cra_driver_name = "sha1-dcp",
  672. .cra_priority = 400,
  673. .cra_alignmask = 63,
  674. .cra_flags = CRYPTO_ALG_ASYNC,
  675. .cra_blocksize = SHA1_BLOCK_SIZE,
  676. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  677. .cra_module = THIS_MODULE,
  678. .cra_init = dcp_sha_cra_init,
  679. .cra_exit = dcp_sha_cra_exit,
  680. },
  681. },
  682. };
  683. /* SHA256 */
  684. static struct ahash_alg dcp_sha256_alg = {
  685. .init = dcp_sha_init,
  686. .update = dcp_sha_update,
  687. .final = dcp_sha_final,
  688. .finup = dcp_sha_finup,
  689. .digest = dcp_sha_digest,
  690. .halg = {
  691. .digestsize = SHA256_DIGEST_SIZE,
  692. .base = {
  693. .cra_name = "sha256",
  694. .cra_driver_name = "sha256-dcp",
  695. .cra_priority = 400,
  696. .cra_alignmask = 63,
  697. .cra_flags = CRYPTO_ALG_ASYNC,
  698. .cra_blocksize = SHA256_BLOCK_SIZE,
  699. .cra_ctxsize = sizeof(struct dcp_async_ctx),
  700. .cra_module = THIS_MODULE,
  701. .cra_init = dcp_sha_cra_init,
  702. .cra_exit = dcp_sha_cra_exit,
  703. },
  704. },
  705. };
  706. static irqreturn_t mxs_dcp_irq(int irq, void *context)
  707. {
  708. struct dcp *sdcp = context;
  709. uint32_t stat;
  710. int i;
  711. stat = readl(sdcp->base + MXS_DCP_STAT);
  712. stat &= MXS_DCP_STAT_IRQ_MASK;
  713. if (!stat)
  714. return IRQ_NONE;
  715. /* Clear the interrupts. */
  716. writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
  717. /* Complete the DMA requests that finished. */
  718. for (i = 0; i < DCP_MAX_CHANS; i++)
  719. if (stat & (1 << i))
  720. complete(&sdcp->completion[i]);
  721. return IRQ_HANDLED;
  722. }
  723. static int mxs_dcp_probe(struct platform_device *pdev)
  724. {
  725. struct device *dev = &pdev->dev;
  726. struct dcp *sdcp = NULL;
  727. int i, ret;
  728. struct resource *iores;
  729. int dcp_vmi_irq, dcp_irq;
  730. if (global_sdcp) {
  731. dev_err(dev, "Only one DCP instance allowed!\n");
  732. return -ENODEV;
  733. }
  734. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  735. dcp_vmi_irq = platform_get_irq(pdev, 0);
  736. if (dcp_vmi_irq < 0)
  737. return dcp_vmi_irq;
  738. dcp_irq = platform_get_irq(pdev, 1);
  739. if (dcp_irq < 0)
  740. return dcp_irq;
  741. sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
  742. if (!sdcp)
  743. return -ENOMEM;
  744. sdcp->dev = dev;
  745. sdcp->base = devm_ioremap_resource(dev, iores);
  746. if (IS_ERR(sdcp->base))
  747. return PTR_ERR(sdcp->base);
  748. ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
  749. "dcp-vmi-irq", sdcp);
  750. if (ret) {
  751. dev_err(dev, "Failed to claim DCP VMI IRQ!\n");
  752. return ret;
  753. }
  754. ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
  755. "dcp-irq", sdcp);
  756. if (ret) {
  757. dev_err(dev, "Failed to claim DCP IRQ!\n");
  758. return ret;
  759. }
  760. /* Allocate coherent helper block. */
  761. sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
  762. GFP_KERNEL);
  763. if (!sdcp->coh)
  764. return -ENOMEM;
  765. /* Re-align the structure so it fits the DCP constraints. */
  766. sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
  767. /* Restart the DCP block. */
  768. ret = stmp_reset_block(sdcp->base);
  769. if (ret)
  770. return ret;
  771. /* Initialize control register. */
  772. writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
  773. MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
  774. sdcp->base + MXS_DCP_CTRL);
  775. /* Enable all DCP DMA channels. */
  776. writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
  777. sdcp->base + MXS_DCP_CHANNELCTRL);
  778. /*
  779. * We do not enable context switching. Give the context buffer a
  780. * pointer to an illegal address so if context switching is
  781. * inadvertantly enabled, the DCP will return an error instead of
  782. * trashing good memory. The DCP DMA cannot access ROM, so any ROM
  783. * address will do.
  784. */
  785. writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
  786. for (i = 0; i < DCP_MAX_CHANS; i++)
  787. writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
  788. writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
  789. global_sdcp = sdcp;
  790. platform_set_drvdata(pdev, sdcp);
  791. for (i = 0; i < DCP_MAX_CHANS; i++) {
  792. mutex_init(&sdcp->mutex[i]);
  793. init_completion(&sdcp->completion[i]);
  794. crypto_init_queue(&sdcp->queue[i], 50);
  795. }
  796. /* Create the SHA and AES handler threads. */
  797. sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
  798. NULL, "mxs_dcp_chan/sha");
  799. if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
  800. dev_err(dev, "Error starting SHA thread!\n");
  801. return PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
  802. }
  803. sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
  804. NULL, "mxs_dcp_chan/aes");
  805. if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
  806. dev_err(dev, "Error starting SHA thread!\n");
  807. ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
  808. goto err_destroy_sha_thread;
  809. }
  810. /* Register the various crypto algorithms. */
  811. sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
  812. if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
  813. ret = crypto_register_algs(dcp_aes_algs,
  814. ARRAY_SIZE(dcp_aes_algs));
  815. if (ret) {
  816. /* Failed to register algorithm. */
  817. dev_err(dev, "Failed to register AES crypto!\n");
  818. goto err_destroy_aes_thread;
  819. }
  820. }
  821. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
  822. ret = crypto_register_ahash(&dcp_sha1_alg);
  823. if (ret) {
  824. dev_err(dev, "Failed to register %s hash!\n",
  825. dcp_sha1_alg.halg.base.cra_name);
  826. goto err_unregister_aes;
  827. }
  828. }
  829. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
  830. ret = crypto_register_ahash(&dcp_sha256_alg);
  831. if (ret) {
  832. dev_err(dev, "Failed to register %s hash!\n",
  833. dcp_sha256_alg.halg.base.cra_name);
  834. goto err_unregister_sha1;
  835. }
  836. }
  837. return 0;
  838. err_unregister_sha1:
  839. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
  840. crypto_unregister_ahash(&dcp_sha1_alg);
  841. err_unregister_aes:
  842. if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
  843. crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
  844. err_destroy_aes_thread:
  845. kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
  846. err_destroy_sha_thread:
  847. kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
  848. return ret;
  849. }
  850. static int mxs_dcp_remove(struct platform_device *pdev)
  851. {
  852. struct dcp *sdcp = platform_get_drvdata(pdev);
  853. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
  854. crypto_unregister_ahash(&dcp_sha256_alg);
  855. if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
  856. crypto_unregister_ahash(&dcp_sha1_alg);
  857. if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
  858. crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
  859. kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
  860. kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
  861. platform_set_drvdata(pdev, NULL);
  862. global_sdcp = NULL;
  863. return 0;
  864. }
  865. static const struct of_device_id mxs_dcp_dt_ids[] = {
  866. { .compatible = "fsl,imx23-dcp", .data = NULL, },
  867. { .compatible = "fsl,imx28-dcp", .data = NULL, },
  868. { /* sentinel */ }
  869. };
  870. MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
  871. static struct platform_driver mxs_dcp_driver = {
  872. .probe = mxs_dcp_probe,
  873. .remove = mxs_dcp_remove,
  874. .driver = {
  875. .name = "mxs-dcp",
  876. .of_match_table = mxs_dcp_dt_ids,
  877. },
  878. };
  879. module_platform_driver(mxs_dcp_driver);
  880. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  881. MODULE_DESCRIPTION("Freescale MXS DCP Driver");
  882. MODULE_LICENSE("GPL");
  883. MODULE_ALIAS("platform:mxs-dcp");