ptdma-dmaengine.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Passthrough DMA device driver
  4. * -- Based on the CCP driver
  5. *
  6. * Copyright (C) 2016,2021 Advanced Micro Devices, Inc.
  7. *
  8. * Author: Sanjay R Mehta <[email protected]>
  9. * Author: Gary R Hook <[email protected]>
  10. */
  11. #include "ptdma.h"
  12. #include "../dmaengine.h"
  13. #include "../virt-dma.h"
  14. static inline struct pt_dma_chan *to_pt_chan(struct dma_chan *dma_chan)
  15. {
  16. return container_of(dma_chan, struct pt_dma_chan, vc.chan);
  17. }
  18. static inline struct pt_dma_desc *to_pt_desc(struct virt_dma_desc *vd)
  19. {
  20. return container_of(vd, struct pt_dma_desc, vd);
  21. }
  22. static void pt_free_chan_resources(struct dma_chan *dma_chan)
  23. {
  24. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  25. vchan_free_chan_resources(&chan->vc);
  26. }
  27. static void pt_synchronize(struct dma_chan *dma_chan)
  28. {
  29. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  30. vchan_synchronize(&chan->vc);
  31. }
  32. static void pt_do_cleanup(struct virt_dma_desc *vd)
  33. {
  34. struct pt_dma_desc *desc = to_pt_desc(vd);
  35. struct pt_device *pt = desc->pt;
  36. kmem_cache_free(pt->dma_desc_cache, desc);
  37. }
  38. static int pt_dma_start_desc(struct pt_dma_desc *desc)
  39. {
  40. struct pt_passthru_engine *pt_engine;
  41. struct pt_device *pt;
  42. struct pt_cmd *pt_cmd;
  43. struct pt_cmd_queue *cmd_q;
  44. desc->issued_to_hw = 1;
  45. pt_cmd = &desc->pt_cmd;
  46. pt = pt_cmd->pt;
  47. cmd_q = &pt->cmd_q;
  48. pt_engine = &pt_cmd->passthru;
  49. pt->tdata.cmd = pt_cmd;
  50. /* Execute the command */
  51. pt_cmd->ret = pt_core_perform_passthru(cmd_q, pt_engine);
  52. return 0;
  53. }
  54. static struct pt_dma_desc *pt_next_dma_desc(struct pt_dma_chan *chan)
  55. {
  56. /* Get the next DMA descriptor on the active list */
  57. struct virt_dma_desc *vd = vchan_next_desc(&chan->vc);
  58. return vd ? to_pt_desc(vd) : NULL;
  59. }
  60. static struct pt_dma_desc *pt_handle_active_desc(struct pt_dma_chan *chan,
  61. struct pt_dma_desc *desc)
  62. {
  63. struct dma_async_tx_descriptor *tx_desc;
  64. struct virt_dma_desc *vd;
  65. unsigned long flags;
  66. /* Loop over descriptors until one is found with commands */
  67. do {
  68. if (desc) {
  69. if (!desc->issued_to_hw) {
  70. /* No errors, keep going */
  71. if (desc->status != DMA_ERROR)
  72. return desc;
  73. }
  74. tx_desc = &desc->vd.tx;
  75. vd = &desc->vd;
  76. } else {
  77. tx_desc = NULL;
  78. }
  79. spin_lock_irqsave(&chan->vc.lock, flags);
  80. if (desc) {
  81. if (desc->status != DMA_COMPLETE) {
  82. if (desc->status != DMA_ERROR)
  83. desc->status = DMA_COMPLETE;
  84. dma_cookie_complete(tx_desc);
  85. dma_descriptor_unmap(tx_desc);
  86. list_del(&desc->vd.node);
  87. } else {
  88. /* Don't handle it twice */
  89. tx_desc = NULL;
  90. }
  91. }
  92. desc = pt_next_dma_desc(chan);
  93. spin_unlock_irqrestore(&chan->vc.lock, flags);
  94. if (tx_desc) {
  95. dmaengine_desc_get_callback_invoke(tx_desc, NULL);
  96. dma_run_dependencies(tx_desc);
  97. vchan_vdesc_fini(vd);
  98. }
  99. } while (desc);
  100. return NULL;
  101. }
  102. static void pt_cmd_callback(void *data, int err)
  103. {
  104. struct pt_dma_desc *desc = data;
  105. struct dma_chan *dma_chan;
  106. struct pt_dma_chan *chan;
  107. int ret;
  108. if (err == -EINPROGRESS)
  109. return;
  110. dma_chan = desc->vd.tx.chan;
  111. chan = to_pt_chan(dma_chan);
  112. if (err)
  113. desc->status = DMA_ERROR;
  114. while (true) {
  115. /* Check for DMA descriptor completion */
  116. desc = pt_handle_active_desc(chan, desc);
  117. /* Don't submit cmd if no descriptor or DMA is paused */
  118. if (!desc)
  119. break;
  120. ret = pt_dma_start_desc(desc);
  121. if (!ret)
  122. break;
  123. desc->status = DMA_ERROR;
  124. }
  125. }
  126. static struct pt_dma_desc *pt_alloc_dma_desc(struct pt_dma_chan *chan,
  127. unsigned long flags)
  128. {
  129. struct pt_dma_desc *desc;
  130. desc = kmem_cache_zalloc(chan->pt->dma_desc_cache, GFP_NOWAIT);
  131. if (!desc)
  132. return NULL;
  133. vchan_tx_prep(&chan->vc, &desc->vd, flags);
  134. desc->pt = chan->pt;
  135. desc->pt->cmd_q.int_en = !!(flags & DMA_PREP_INTERRUPT);
  136. desc->issued_to_hw = 0;
  137. desc->status = DMA_IN_PROGRESS;
  138. return desc;
  139. }
  140. static struct pt_dma_desc *pt_create_desc(struct dma_chan *dma_chan,
  141. dma_addr_t dst,
  142. dma_addr_t src,
  143. unsigned int len,
  144. unsigned long flags)
  145. {
  146. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  147. struct pt_passthru_engine *pt_engine;
  148. struct pt_dma_desc *desc;
  149. struct pt_cmd *pt_cmd;
  150. desc = pt_alloc_dma_desc(chan, flags);
  151. if (!desc)
  152. return NULL;
  153. pt_cmd = &desc->pt_cmd;
  154. pt_cmd->pt = chan->pt;
  155. pt_engine = &pt_cmd->passthru;
  156. pt_cmd->engine = PT_ENGINE_PASSTHRU;
  157. pt_engine->src_dma = src;
  158. pt_engine->dst_dma = dst;
  159. pt_engine->src_len = len;
  160. pt_cmd->pt_cmd_callback = pt_cmd_callback;
  161. pt_cmd->data = desc;
  162. desc->len = len;
  163. return desc;
  164. }
  165. static struct dma_async_tx_descriptor *
  166. pt_prep_dma_memcpy(struct dma_chan *dma_chan, dma_addr_t dst,
  167. dma_addr_t src, size_t len, unsigned long flags)
  168. {
  169. struct pt_dma_desc *desc;
  170. desc = pt_create_desc(dma_chan, dst, src, len, flags);
  171. if (!desc)
  172. return NULL;
  173. return &desc->vd.tx;
  174. }
  175. static struct dma_async_tx_descriptor *
  176. pt_prep_dma_interrupt(struct dma_chan *dma_chan, unsigned long flags)
  177. {
  178. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  179. struct pt_dma_desc *desc;
  180. desc = pt_alloc_dma_desc(chan, flags);
  181. if (!desc)
  182. return NULL;
  183. return &desc->vd.tx;
  184. }
  185. static void pt_issue_pending(struct dma_chan *dma_chan)
  186. {
  187. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  188. struct pt_dma_desc *desc;
  189. unsigned long flags;
  190. bool engine_is_idle = true;
  191. spin_lock_irqsave(&chan->vc.lock, flags);
  192. desc = pt_next_dma_desc(chan);
  193. if (desc)
  194. engine_is_idle = false;
  195. vchan_issue_pending(&chan->vc);
  196. desc = pt_next_dma_desc(chan);
  197. spin_unlock_irqrestore(&chan->vc.lock, flags);
  198. /* If there was nothing active, start processing */
  199. if (engine_is_idle && desc)
  200. pt_cmd_callback(desc, 0);
  201. }
  202. static enum dma_status
  203. pt_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  204. struct dma_tx_state *txstate)
  205. {
  206. struct pt_device *pt = to_pt_chan(c)->pt;
  207. struct pt_cmd_queue *cmd_q = &pt->cmd_q;
  208. pt_check_status_trans(pt, cmd_q);
  209. return dma_cookie_status(c, cookie, txstate);
  210. }
  211. static int pt_pause(struct dma_chan *dma_chan)
  212. {
  213. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  214. unsigned long flags;
  215. spin_lock_irqsave(&chan->vc.lock, flags);
  216. pt_stop_queue(&chan->pt->cmd_q);
  217. spin_unlock_irqrestore(&chan->vc.lock, flags);
  218. return 0;
  219. }
  220. static int pt_resume(struct dma_chan *dma_chan)
  221. {
  222. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  223. struct pt_dma_desc *desc = NULL;
  224. unsigned long flags;
  225. spin_lock_irqsave(&chan->vc.lock, flags);
  226. pt_start_queue(&chan->pt->cmd_q);
  227. desc = pt_next_dma_desc(chan);
  228. spin_unlock_irqrestore(&chan->vc.lock, flags);
  229. /* If there was something active, re-start */
  230. if (desc)
  231. pt_cmd_callback(desc, 0);
  232. return 0;
  233. }
  234. static int pt_terminate_all(struct dma_chan *dma_chan)
  235. {
  236. struct pt_dma_chan *chan = to_pt_chan(dma_chan);
  237. unsigned long flags;
  238. struct pt_cmd_queue *cmd_q = &chan->pt->cmd_q;
  239. LIST_HEAD(head);
  240. iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010);
  241. spin_lock_irqsave(&chan->vc.lock, flags);
  242. vchan_get_all_descriptors(&chan->vc, &head);
  243. spin_unlock_irqrestore(&chan->vc.lock, flags);
  244. vchan_dma_desc_free_list(&chan->vc, &head);
  245. vchan_free_chan_resources(&chan->vc);
  246. return 0;
  247. }
  248. int pt_dmaengine_register(struct pt_device *pt)
  249. {
  250. struct pt_dma_chan *chan;
  251. struct dma_device *dma_dev = &pt->dma_dev;
  252. char *cmd_cache_name;
  253. char *desc_cache_name;
  254. int ret;
  255. pt->pt_dma_chan = devm_kzalloc(pt->dev, sizeof(*pt->pt_dma_chan),
  256. GFP_KERNEL);
  257. if (!pt->pt_dma_chan)
  258. return -ENOMEM;
  259. cmd_cache_name = devm_kasprintf(pt->dev, GFP_KERNEL,
  260. "%s-dmaengine-cmd-cache",
  261. dev_name(pt->dev));
  262. if (!cmd_cache_name)
  263. return -ENOMEM;
  264. desc_cache_name = devm_kasprintf(pt->dev, GFP_KERNEL,
  265. "%s-dmaengine-desc-cache",
  266. dev_name(pt->dev));
  267. if (!desc_cache_name) {
  268. ret = -ENOMEM;
  269. goto err_cache;
  270. }
  271. pt->dma_desc_cache = kmem_cache_create(desc_cache_name,
  272. sizeof(struct pt_dma_desc), 0,
  273. SLAB_HWCACHE_ALIGN, NULL);
  274. if (!pt->dma_desc_cache) {
  275. ret = -ENOMEM;
  276. goto err_cache;
  277. }
  278. dma_dev->dev = pt->dev;
  279. dma_dev->src_addr_widths = DMA_SLAVE_BUSWIDTH_64_BYTES;
  280. dma_dev->dst_addr_widths = DMA_SLAVE_BUSWIDTH_64_BYTES;
  281. dma_dev->directions = DMA_MEM_TO_MEM;
  282. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  283. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  284. dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask);
  285. /*
  286. * PTDMA is intended to be used with the AMD NTB devices, hence
  287. * marking it as DMA_PRIVATE.
  288. */
  289. dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
  290. INIT_LIST_HEAD(&dma_dev->channels);
  291. chan = pt->pt_dma_chan;
  292. chan->pt = pt;
  293. /* Set base and prep routines */
  294. dma_dev->device_free_chan_resources = pt_free_chan_resources;
  295. dma_dev->device_prep_dma_memcpy = pt_prep_dma_memcpy;
  296. dma_dev->device_prep_dma_interrupt = pt_prep_dma_interrupt;
  297. dma_dev->device_issue_pending = pt_issue_pending;
  298. dma_dev->device_tx_status = pt_tx_status;
  299. dma_dev->device_pause = pt_pause;
  300. dma_dev->device_resume = pt_resume;
  301. dma_dev->device_terminate_all = pt_terminate_all;
  302. dma_dev->device_synchronize = pt_synchronize;
  303. chan->vc.desc_free = pt_do_cleanup;
  304. vchan_init(&chan->vc, dma_dev);
  305. dma_set_mask_and_coherent(pt->dev, DMA_BIT_MASK(64));
  306. ret = dma_async_device_register(dma_dev);
  307. if (ret)
  308. goto err_reg;
  309. return 0;
  310. err_reg:
  311. kmem_cache_destroy(pt->dma_desc_cache);
  312. err_cache:
  313. kmem_cache_destroy(pt->dma_cmd_cache);
  314. return ret;
  315. }
  316. void pt_dmaengine_unregister(struct pt_device *pt)
  317. {
  318. struct dma_device *dma_dev = &pt->dma_dev;
  319. dma_async_device_unregister(dma_dev);
  320. kmem_cache_destroy(pt->dma_desc_cache);
  321. kmem_cache_destroy(pt->dma_cmd_cache);
  322. }