mxs-dcp.c 27 KB

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