mxs-dcp.c 28 KB

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