mxs-dcp.c 28 KB

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