ccp-dmaengine.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Gary R Hook <gary.hook@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/mutex.h>
  17. #include <linux/ccp.h>
  18. #include "ccp-dev.h"
  19. #include "../../dma/dmaengine.h"
  20. #define CCP_DMA_WIDTH(_mask) \
  21. ({ \
  22. u64 mask = _mask + 1; \
  23. (mask == 0) ? 64 : fls64(mask); \
  24. })
  25. /* The CCP as a DMA provider can be configured for public or private
  26. * channels. Default is specified in the vdata for the device (PCI ID).
  27. * This module parameter will override for all channels on all devices:
  28. * dma_chan_attr = 0x2 to force all channels public
  29. * = 0x1 to force all channels private
  30. * = 0x0 to defer to the vdata setting
  31. * = any other value: warning, revert to 0x0
  32. */
  33. static unsigned int dma_chan_attr = CCP_DMA_DFLT;
  34. module_param(dma_chan_attr, uint, 0444);
  35. MODULE_PARM_DESC(dma_chan_attr, "Set DMA channel visibility: 0 (default) = device defaults, 1 = make private, 2 = make public");
  36. static unsigned int ccp_get_dma_chan_attr(struct ccp_device *ccp)
  37. {
  38. switch (dma_chan_attr) {
  39. case CCP_DMA_DFLT:
  40. return ccp->vdata->dma_chan_attr;
  41. case CCP_DMA_PRIV:
  42. return DMA_PRIVATE;
  43. case CCP_DMA_PUB:
  44. return 0;
  45. default:
  46. dev_info_once(ccp->dev, "Invalid value for dma_chan_attr: %d\n",
  47. dma_chan_attr);
  48. return ccp->vdata->dma_chan_attr;
  49. }
  50. }
  51. static void ccp_free_cmd_resources(struct ccp_device *ccp,
  52. struct list_head *list)
  53. {
  54. struct ccp_dma_cmd *cmd, *ctmp;
  55. list_for_each_entry_safe(cmd, ctmp, list, entry) {
  56. list_del(&cmd->entry);
  57. kmem_cache_free(ccp->dma_cmd_cache, cmd);
  58. }
  59. }
  60. static void ccp_free_desc_resources(struct ccp_device *ccp,
  61. struct list_head *list)
  62. {
  63. struct ccp_dma_desc *desc, *dtmp;
  64. list_for_each_entry_safe(desc, dtmp, list, entry) {
  65. ccp_free_cmd_resources(ccp, &desc->active);
  66. ccp_free_cmd_resources(ccp, &desc->pending);
  67. list_del(&desc->entry);
  68. kmem_cache_free(ccp->dma_desc_cache, desc);
  69. }
  70. }
  71. static void ccp_free_chan_resources(struct dma_chan *dma_chan)
  72. {
  73. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  74. dma_chan);
  75. unsigned long flags;
  76. dev_dbg(chan->ccp->dev, "%s - chan=%p\n", __func__, chan);
  77. spin_lock_irqsave(&chan->lock, flags);
  78. ccp_free_desc_resources(chan->ccp, &chan->complete);
  79. ccp_free_desc_resources(chan->ccp, &chan->active);
  80. ccp_free_desc_resources(chan->ccp, &chan->pending);
  81. ccp_free_desc_resources(chan->ccp, &chan->created);
  82. spin_unlock_irqrestore(&chan->lock, flags);
  83. }
  84. static void ccp_cleanup_desc_resources(struct ccp_device *ccp,
  85. struct list_head *list)
  86. {
  87. struct ccp_dma_desc *desc, *dtmp;
  88. list_for_each_entry_safe_reverse(desc, dtmp, list, entry) {
  89. if (!async_tx_test_ack(&desc->tx_desc))
  90. continue;
  91. dev_dbg(ccp->dev, "%s - desc=%p\n", __func__, desc);
  92. ccp_free_cmd_resources(ccp, &desc->active);
  93. ccp_free_cmd_resources(ccp, &desc->pending);
  94. list_del(&desc->entry);
  95. kmem_cache_free(ccp->dma_desc_cache, desc);
  96. }
  97. }
  98. static void ccp_do_cleanup(unsigned long data)
  99. {
  100. struct ccp_dma_chan *chan = (struct ccp_dma_chan *)data;
  101. unsigned long flags;
  102. dev_dbg(chan->ccp->dev, "%s - chan=%s\n", __func__,
  103. dma_chan_name(&chan->dma_chan));
  104. spin_lock_irqsave(&chan->lock, flags);
  105. ccp_cleanup_desc_resources(chan->ccp, &chan->complete);
  106. spin_unlock_irqrestore(&chan->lock, flags);
  107. }
  108. static int ccp_issue_next_cmd(struct ccp_dma_desc *desc)
  109. {
  110. struct ccp_dma_cmd *cmd;
  111. int ret;
  112. cmd = list_first_entry(&desc->pending, struct ccp_dma_cmd, entry);
  113. list_move(&cmd->entry, &desc->active);
  114. dev_dbg(desc->ccp->dev, "%s - tx %d, cmd=%p\n", __func__,
  115. desc->tx_desc.cookie, cmd);
  116. ret = ccp_enqueue_cmd(&cmd->ccp_cmd);
  117. if (!ret || (ret == -EINPROGRESS) || (ret == -EBUSY))
  118. return 0;
  119. dev_dbg(desc->ccp->dev, "%s - error: ret=%d, tx %d, cmd=%p\n", __func__,
  120. ret, desc->tx_desc.cookie, cmd);
  121. return ret;
  122. }
  123. static void ccp_free_active_cmd(struct ccp_dma_desc *desc)
  124. {
  125. struct ccp_dma_cmd *cmd;
  126. cmd = list_first_entry_or_null(&desc->active, struct ccp_dma_cmd,
  127. entry);
  128. if (!cmd)
  129. return;
  130. dev_dbg(desc->ccp->dev, "%s - freeing tx %d cmd=%p\n",
  131. __func__, desc->tx_desc.cookie, cmd);
  132. list_del(&cmd->entry);
  133. kmem_cache_free(desc->ccp->dma_cmd_cache, cmd);
  134. }
  135. static struct ccp_dma_desc *__ccp_next_dma_desc(struct ccp_dma_chan *chan,
  136. struct ccp_dma_desc *desc)
  137. {
  138. /* Move current DMA descriptor to the complete list */
  139. if (desc)
  140. list_move(&desc->entry, &chan->complete);
  141. /* Get the next DMA descriptor on the active list */
  142. desc = list_first_entry_or_null(&chan->active, struct ccp_dma_desc,
  143. entry);
  144. return desc;
  145. }
  146. static struct ccp_dma_desc *ccp_handle_active_desc(struct ccp_dma_chan *chan,
  147. struct ccp_dma_desc *desc)
  148. {
  149. struct dma_async_tx_descriptor *tx_desc;
  150. unsigned long flags;
  151. /* Loop over descriptors until one is found with commands */
  152. do {
  153. if (desc) {
  154. /* Remove the DMA command from the list and free it */
  155. ccp_free_active_cmd(desc);
  156. if (!list_empty(&desc->pending)) {
  157. /* No errors, keep going */
  158. if (desc->status != DMA_ERROR)
  159. return desc;
  160. /* Error, free remaining commands and move on */
  161. ccp_free_cmd_resources(desc->ccp,
  162. &desc->pending);
  163. }
  164. tx_desc = &desc->tx_desc;
  165. } else {
  166. tx_desc = NULL;
  167. }
  168. spin_lock_irqsave(&chan->lock, flags);
  169. if (desc) {
  170. if (desc->status != DMA_ERROR)
  171. desc->status = DMA_COMPLETE;
  172. dev_dbg(desc->ccp->dev,
  173. "%s - tx %d complete, status=%u\n", __func__,
  174. desc->tx_desc.cookie, desc->status);
  175. dma_cookie_complete(tx_desc);
  176. dma_descriptor_unmap(tx_desc);
  177. }
  178. desc = __ccp_next_dma_desc(chan, desc);
  179. spin_unlock_irqrestore(&chan->lock, flags);
  180. if (tx_desc) {
  181. dmaengine_desc_get_callback_invoke(tx_desc, NULL);
  182. dma_run_dependencies(tx_desc);
  183. }
  184. } while (desc);
  185. return NULL;
  186. }
  187. static struct ccp_dma_desc *__ccp_pending_to_active(struct ccp_dma_chan *chan)
  188. {
  189. struct ccp_dma_desc *desc;
  190. if (list_empty(&chan->pending))
  191. return NULL;
  192. desc = list_empty(&chan->active)
  193. ? list_first_entry(&chan->pending, struct ccp_dma_desc, entry)
  194. : NULL;
  195. list_splice_tail_init(&chan->pending, &chan->active);
  196. return desc;
  197. }
  198. static void ccp_cmd_callback(void *data, int err)
  199. {
  200. struct ccp_dma_desc *desc = data;
  201. struct ccp_dma_chan *chan;
  202. int ret;
  203. if (err == -EINPROGRESS)
  204. return;
  205. chan = container_of(desc->tx_desc.chan, struct ccp_dma_chan,
  206. dma_chan);
  207. dev_dbg(chan->ccp->dev, "%s - tx %d callback, err=%d\n",
  208. __func__, desc->tx_desc.cookie, err);
  209. if (err)
  210. desc->status = DMA_ERROR;
  211. while (true) {
  212. /* Check for DMA descriptor completion */
  213. desc = ccp_handle_active_desc(chan, desc);
  214. /* Don't submit cmd if no descriptor or DMA is paused */
  215. if (!desc || (chan->status == DMA_PAUSED))
  216. break;
  217. ret = ccp_issue_next_cmd(desc);
  218. if (!ret)
  219. break;
  220. desc->status = DMA_ERROR;
  221. }
  222. tasklet_schedule(&chan->cleanup_tasklet);
  223. }
  224. static dma_cookie_t ccp_tx_submit(struct dma_async_tx_descriptor *tx_desc)
  225. {
  226. struct ccp_dma_desc *desc = container_of(tx_desc, struct ccp_dma_desc,
  227. tx_desc);
  228. struct ccp_dma_chan *chan;
  229. dma_cookie_t cookie;
  230. unsigned long flags;
  231. chan = container_of(tx_desc->chan, struct ccp_dma_chan, dma_chan);
  232. spin_lock_irqsave(&chan->lock, flags);
  233. cookie = dma_cookie_assign(tx_desc);
  234. list_del(&desc->entry);
  235. list_add_tail(&desc->entry, &chan->pending);
  236. spin_unlock_irqrestore(&chan->lock, flags);
  237. dev_dbg(chan->ccp->dev, "%s - added tx descriptor %d to pending list\n",
  238. __func__, cookie);
  239. return cookie;
  240. }
  241. static struct ccp_dma_cmd *ccp_alloc_dma_cmd(struct ccp_dma_chan *chan)
  242. {
  243. struct ccp_dma_cmd *cmd;
  244. cmd = kmem_cache_alloc(chan->ccp->dma_cmd_cache, GFP_NOWAIT);
  245. if (cmd)
  246. memset(cmd, 0, sizeof(*cmd));
  247. return cmd;
  248. }
  249. static struct ccp_dma_desc *ccp_alloc_dma_desc(struct ccp_dma_chan *chan,
  250. unsigned long flags)
  251. {
  252. struct ccp_dma_desc *desc;
  253. desc = kmem_cache_zalloc(chan->ccp->dma_desc_cache, GFP_NOWAIT);
  254. if (!desc)
  255. return NULL;
  256. dma_async_tx_descriptor_init(&desc->tx_desc, &chan->dma_chan);
  257. desc->tx_desc.flags = flags;
  258. desc->tx_desc.tx_submit = ccp_tx_submit;
  259. desc->ccp = chan->ccp;
  260. INIT_LIST_HEAD(&desc->entry);
  261. INIT_LIST_HEAD(&desc->pending);
  262. INIT_LIST_HEAD(&desc->active);
  263. desc->status = DMA_IN_PROGRESS;
  264. return desc;
  265. }
  266. static struct ccp_dma_desc *ccp_create_desc(struct dma_chan *dma_chan,
  267. struct scatterlist *dst_sg,
  268. unsigned int dst_nents,
  269. struct scatterlist *src_sg,
  270. unsigned int src_nents,
  271. unsigned long flags)
  272. {
  273. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  274. dma_chan);
  275. struct ccp_device *ccp = chan->ccp;
  276. struct ccp_dma_desc *desc;
  277. struct ccp_dma_cmd *cmd;
  278. struct ccp_cmd *ccp_cmd;
  279. struct ccp_passthru_nomap_engine *ccp_pt;
  280. unsigned int src_offset, src_len;
  281. unsigned int dst_offset, dst_len;
  282. unsigned int len;
  283. unsigned long sflags;
  284. size_t total_len;
  285. if (!dst_sg || !src_sg)
  286. return NULL;
  287. if (!dst_nents || !src_nents)
  288. return NULL;
  289. desc = ccp_alloc_dma_desc(chan, flags);
  290. if (!desc)
  291. return NULL;
  292. total_len = 0;
  293. src_len = sg_dma_len(src_sg);
  294. src_offset = 0;
  295. dst_len = sg_dma_len(dst_sg);
  296. dst_offset = 0;
  297. while (true) {
  298. if (!src_len) {
  299. src_nents--;
  300. if (!src_nents)
  301. break;
  302. src_sg = sg_next(src_sg);
  303. if (!src_sg)
  304. break;
  305. src_len = sg_dma_len(src_sg);
  306. src_offset = 0;
  307. continue;
  308. }
  309. if (!dst_len) {
  310. dst_nents--;
  311. if (!dst_nents)
  312. break;
  313. dst_sg = sg_next(dst_sg);
  314. if (!dst_sg)
  315. break;
  316. dst_len = sg_dma_len(dst_sg);
  317. dst_offset = 0;
  318. continue;
  319. }
  320. len = min(dst_len, src_len);
  321. cmd = ccp_alloc_dma_cmd(chan);
  322. if (!cmd)
  323. goto err;
  324. ccp_cmd = &cmd->ccp_cmd;
  325. ccp_cmd->ccp = chan->ccp;
  326. ccp_pt = &ccp_cmd->u.passthru_nomap;
  327. ccp_cmd->flags = CCP_CMD_MAY_BACKLOG;
  328. ccp_cmd->flags |= CCP_CMD_PASSTHRU_NO_DMA_MAP;
  329. ccp_cmd->engine = CCP_ENGINE_PASSTHRU;
  330. ccp_pt->bit_mod = CCP_PASSTHRU_BITWISE_NOOP;
  331. ccp_pt->byte_swap = CCP_PASSTHRU_BYTESWAP_NOOP;
  332. ccp_pt->src_dma = sg_dma_address(src_sg) + src_offset;
  333. ccp_pt->dst_dma = sg_dma_address(dst_sg) + dst_offset;
  334. ccp_pt->src_len = len;
  335. ccp_pt->final = 1;
  336. ccp_cmd->callback = ccp_cmd_callback;
  337. ccp_cmd->data = desc;
  338. list_add_tail(&cmd->entry, &desc->pending);
  339. dev_dbg(ccp->dev,
  340. "%s - cmd=%p, src=%pad, dst=%pad, len=%llu\n", __func__,
  341. cmd, &ccp_pt->src_dma,
  342. &ccp_pt->dst_dma, ccp_pt->src_len);
  343. total_len += len;
  344. src_len -= len;
  345. src_offset += len;
  346. dst_len -= len;
  347. dst_offset += len;
  348. }
  349. desc->len = total_len;
  350. if (list_empty(&desc->pending))
  351. goto err;
  352. dev_dbg(ccp->dev, "%s - desc=%p\n", __func__, desc);
  353. spin_lock_irqsave(&chan->lock, sflags);
  354. list_add_tail(&desc->entry, &chan->created);
  355. spin_unlock_irqrestore(&chan->lock, sflags);
  356. return desc;
  357. err:
  358. ccp_free_cmd_resources(ccp, &desc->pending);
  359. kmem_cache_free(ccp->dma_desc_cache, desc);
  360. return NULL;
  361. }
  362. static struct dma_async_tx_descriptor *ccp_prep_dma_memcpy(
  363. struct dma_chan *dma_chan, dma_addr_t dst, dma_addr_t src, size_t len,
  364. unsigned long flags)
  365. {
  366. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  367. dma_chan);
  368. struct ccp_dma_desc *desc;
  369. struct scatterlist dst_sg, src_sg;
  370. dev_dbg(chan->ccp->dev,
  371. "%s - src=%pad, dst=%pad, len=%zu, flags=%#lx\n",
  372. __func__, &src, &dst, len, flags);
  373. sg_init_table(&dst_sg, 1);
  374. sg_dma_address(&dst_sg) = dst;
  375. sg_dma_len(&dst_sg) = len;
  376. sg_init_table(&src_sg, 1);
  377. sg_dma_address(&src_sg) = src;
  378. sg_dma_len(&src_sg) = len;
  379. desc = ccp_create_desc(dma_chan, &dst_sg, 1, &src_sg, 1, flags);
  380. if (!desc)
  381. return NULL;
  382. return &desc->tx_desc;
  383. }
  384. static struct dma_async_tx_descriptor *ccp_prep_dma_interrupt(
  385. struct dma_chan *dma_chan, unsigned long flags)
  386. {
  387. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  388. dma_chan);
  389. struct ccp_dma_desc *desc;
  390. desc = ccp_alloc_dma_desc(chan, flags);
  391. if (!desc)
  392. return NULL;
  393. return &desc->tx_desc;
  394. }
  395. static void ccp_issue_pending(struct dma_chan *dma_chan)
  396. {
  397. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  398. dma_chan);
  399. struct ccp_dma_desc *desc;
  400. unsigned long flags;
  401. dev_dbg(chan->ccp->dev, "%s\n", __func__);
  402. spin_lock_irqsave(&chan->lock, flags);
  403. desc = __ccp_pending_to_active(chan);
  404. spin_unlock_irqrestore(&chan->lock, flags);
  405. /* If there was nothing active, start processing */
  406. if (desc)
  407. ccp_cmd_callback(desc, 0);
  408. }
  409. static enum dma_status ccp_tx_status(struct dma_chan *dma_chan,
  410. dma_cookie_t cookie,
  411. struct dma_tx_state *state)
  412. {
  413. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  414. dma_chan);
  415. struct ccp_dma_desc *desc;
  416. enum dma_status ret;
  417. unsigned long flags;
  418. if (chan->status == DMA_PAUSED) {
  419. ret = DMA_PAUSED;
  420. goto out;
  421. }
  422. ret = dma_cookie_status(dma_chan, cookie, state);
  423. if (ret == DMA_COMPLETE) {
  424. spin_lock_irqsave(&chan->lock, flags);
  425. /* Get status from complete chain, if still there */
  426. list_for_each_entry(desc, &chan->complete, entry) {
  427. if (desc->tx_desc.cookie != cookie)
  428. continue;
  429. ret = desc->status;
  430. break;
  431. }
  432. spin_unlock_irqrestore(&chan->lock, flags);
  433. }
  434. out:
  435. dev_dbg(chan->ccp->dev, "%s - %u\n", __func__, ret);
  436. return ret;
  437. }
  438. static int ccp_pause(struct dma_chan *dma_chan)
  439. {
  440. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  441. dma_chan);
  442. chan->status = DMA_PAUSED;
  443. /*TODO: Wait for active DMA to complete before returning? */
  444. return 0;
  445. }
  446. static int ccp_resume(struct dma_chan *dma_chan)
  447. {
  448. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  449. dma_chan);
  450. struct ccp_dma_desc *desc;
  451. unsigned long flags;
  452. spin_lock_irqsave(&chan->lock, flags);
  453. desc = list_first_entry_or_null(&chan->active, struct ccp_dma_desc,
  454. entry);
  455. spin_unlock_irqrestore(&chan->lock, flags);
  456. /* Indicate the channel is running again */
  457. chan->status = DMA_IN_PROGRESS;
  458. /* If there was something active, re-start */
  459. if (desc)
  460. ccp_cmd_callback(desc, 0);
  461. return 0;
  462. }
  463. static int ccp_terminate_all(struct dma_chan *dma_chan)
  464. {
  465. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  466. dma_chan);
  467. unsigned long flags;
  468. dev_dbg(chan->ccp->dev, "%s\n", __func__);
  469. /*TODO: Wait for active DMA to complete before continuing */
  470. spin_lock_irqsave(&chan->lock, flags);
  471. /*TODO: Purge the complete list? */
  472. ccp_free_desc_resources(chan->ccp, &chan->active);
  473. ccp_free_desc_resources(chan->ccp, &chan->pending);
  474. ccp_free_desc_resources(chan->ccp, &chan->created);
  475. spin_unlock_irqrestore(&chan->lock, flags);
  476. return 0;
  477. }
  478. int ccp_dmaengine_register(struct ccp_device *ccp)
  479. {
  480. struct ccp_dma_chan *chan;
  481. struct dma_device *dma_dev = &ccp->dma_dev;
  482. struct dma_chan *dma_chan;
  483. char *dma_cmd_cache_name;
  484. char *dma_desc_cache_name;
  485. unsigned int i;
  486. int ret;
  487. ccp->ccp_dma_chan = devm_kcalloc(ccp->dev, ccp->cmd_q_count,
  488. sizeof(*(ccp->ccp_dma_chan)),
  489. GFP_KERNEL);
  490. if (!ccp->ccp_dma_chan)
  491. return -ENOMEM;
  492. dma_cmd_cache_name = devm_kasprintf(ccp->dev, GFP_KERNEL,
  493. "%s-dmaengine-cmd-cache",
  494. ccp->name);
  495. if (!dma_cmd_cache_name)
  496. return -ENOMEM;
  497. ccp->dma_cmd_cache = kmem_cache_create(dma_cmd_cache_name,
  498. sizeof(struct ccp_dma_cmd),
  499. sizeof(void *),
  500. SLAB_HWCACHE_ALIGN, NULL);
  501. if (!ccp->dma_cmd_cache)
  502. return -ENOMEM;
  503. dma_desc_cache_name = devm_kasprintf(ccp->dev, GFP_KERNEL,
  504. "%s-dmaengine-desc-cache",
  505. ccp->name);
  506. if (!dma_desc_cache_name) {
  507. ret = -ENOMEM;
  508. goto err_cache;
  509. }
  510. ccp->dma_desc_cache = kmem_cache_create(dma_desc_cache_name,
  511. sizeof(struct ccp_dma_desc),
  512. sizeof(void *),
  513. SLAB_HWCACHE_ALIGN, NULL);
  514. if (!ccp->dma_desc_cache) {
  515. ret = -ENOMEM;
  516. goto err_cache;
  517. }
  518. dma_dev->dev = ccp->dev;
  519. dma_dev->src_addr_widths = CCP_DMA_WIDTH(dma_get_mask(ccp->dev));
  520. dma_dev->dst_addr_widths = CCP_DMA_WIDTH(dma_get_mask(ccp->dev));
  521. dma_dev->directions = DMA_MEM_TO_MEM;
  522. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  523. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  524. dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask);
  525. /* The DMA channels for this device can be set to public or private,
  526. * and overridden by the module parameter dma_chan_attr.
  527. * Default: according to the value in vdata (dma_chan_attr=0)
  528. * dma_chan_attr=0x1: all channels private (override vdata)
  529. * dma_chan_attr=0x2: all channels public (override vdata)
  530. */
  531. if (ccp_get_dma_chan_attr(ccp) == DMA_PRIVATE)
  532. dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
  533. INIT_LIST_HEAD(&dma_dev->channels);
  534. for (i = 0; i < ccp->cmd_q_count; i++) {
  535. chan = ccp->ccp_dma_chan + i;
  536. dma_chan = &chan->dma_chan;
  537. chan->ccp = ccp;
  538. spin_lock_init(&chan->lock);
  539. INIT_LIST_HEAD(&chan->created);
  540. INIT_LIST_HEAD(&chan->pending);
  541. INIT_LIST_HEAD(&chan->active);
  542. INIT_LIST_HEAD(&chan->complete);
  543. tasklet_init(&chan->cleanup_tasklet, ccp_do_cleanup,
  544. (unsigned long)chan);
  545. dma_chan->device = dma_dev;
  546. dma_cookie_init(dma_chan);
  547. list_add_tail(&dma_chan->device_node, &dma_dev->channels);
  548. }
  549. dma_dev->device_free_chan_resources = ccp_free_chan_resources;
  550. dma_dev->device_prep_dma_memcpy = ccp_prep_dma_memcpy;
  551. dma_dev->device_prep_dma_interrupt = ccp_prep_dma_interrupt;
  552. dma_dev->device_issue_pending = ccp_issue_pending;
  553. dma_dev->device_tx_status = ccp_tx_status;
  554. dma_dev->device_pause = ccp_pause;
  555. dma_dev->device_resume = ccp_resume;
  556. dma_dev->device_terminate_all = ccp_terminate_all;
  557. ret = dma_async_device_register(dma_dev);
  558. if (ret)
  559. goto err_reg;
  560. return 0;
  561. err_reg:
  562. kmem_cache_destroy(ccp->dma_desc_cache);
  563. err_cache:
  564. kmem_cache_destroy(ccp->dma_cmd_cache);
  565. return ret;
  566. }
  567. void ccp_dmaengine_unregister(struct ccp_device *ccp)
  568. {
  569. struct dma_device *dma_dev = &ccp->dma_dev;
  570. dma_async_device_unregister(dma_dev);
  571. kmem_cache_destroy(ccp->dma_desc_cache);
  572. kmem_cache_destroy(ccp->dma_cmd_cache);
  573. }