dma.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
  3. #include <linux/init.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/pci.h>
  7. #include <linux/device.h>
  8. #include <linux/io-64-nonatomic-lo-hi.h>
  9. #include <linux/dmaengine.h>
  10. #include <uapi/linux/idxd.h>
  11. #include "../dmaengine.h"
  12. #include "registers.h"
  13. #include "idxd.h"
  14. static inline struct idxd_wq *to_idxd_wq(struct dma_chan *c)
  15. {
  16. struct idxd_dma_chan *idxd_chan;
  17. idxd_chan = container_of(c, struct idxd_dma_chan, chan);
  18. return idxd_chan->wq;
  19. }
  20. void idxd_dma_complete_txd(struct idxd_desc *desc,
  21. enum idxd_complete_type comp_type,
  22. bool free_desc)
  23. {
  24. struct idxd_device *idxd = desc->wq->idxd;
  25. struct dma_async_tx_descriptor *tx;
  26. struct dmaengine_result res;
  27. int complete = 1;
  28. if (desc->completion->status == DSA_COMP_SUCCESS) {
  29. res.result = DMA_TRANS_NOERROR;
  30. } else if (desc->completion->status) {
  31. if (idxd->request_int_handles && comp_type != IDXD_COMPLETE_ABORT &&
  32. desc->completion->status == DSA_COMP_INT_HANDLE_INVAL &&
  33. idxd_queue_int_handle_resubmit(desc))
  34. return;
  35. res.result = DMA_TRANS_WRITE_FAILED;
  36. } else if (comp_type == IDXD_COMPLETE_ABORT) {
  37. res.result = DMA_TRANS_ABORTED;
  38. } else {
  39. complete = 0;
  40. }
  41. tx = &desc->txd;
  42. if (complete && tx->cookie) {
  43. dma_cookie_complete(tx);
  44. dma_descriptor_unmap(tx);
  45. dmaengine_desc_get_callback_invoke(tx, &res);
  46. tx->callback = NULL;
  47. tx->callback_result = NULL;
  48. }
  49. if (free_desc)
  50. idxd_free_desc(desc->wq, desc);
  51. }
  52. static void op_flag_setup(unsigned long flags, u32 *desc_flags)
  53. {
  54. *desc_flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR;
  55. if (flags & DMA_PREP_INTERRUPT)
  56. *desc_flags |= IDXD_OP_FLAG_RCI;
  57. }
  58. static inline void set_completion_address(struct idxd_desc *desc,
  59. u64 *compl_addr)
  60. {
  61. *compl_addr = desc->compl_dma;
  62. }
  63. static inline void idxd_prep_desc_common(struct idxd_wq *wq,
  64. struct dsa_hw_desc *hw, char opcode,
  65. u64 addr_f1, u64 addr_f2, u64 len,
  66. u64 compl, u32 flags)
  67. {
  68. hw->flags = flags;
  69. hw->opcode = opcode;
  70. hw->src_addr = addr_f1;
  71. hw->dst_addr = addr_f2;
  72. hw->xfer_size = len;
  73. /*
  74. * For dedicated WQ, this field is ignored and HW will use the WQCFG.priv
  75. * field instead. This field should be set to 1 for kernel descriptors.
  76. */
  77. hw->priv = 1;
  78. hw->completion_addr = compl;
  79. }
  80. static struct dma_async_tx_descriptor *
  81. idxd_dma_prep_interrupt(struct dma_chan *c, unsigned long flags)
  82. {
  83. struct idxd_wq *wq = to_idxd_wq(c);
  84. u32 desc_flags;
  85. struct idxd_desc *desc;
  86. if (wq->state != IDXD_WQ_ENABLED)
  87. return NULL;
  88. op_flag_setup(flags, &desc_flags);
  89. desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
  90. if (IS_ERR(desc))
  91. return NULL;
  92. idxd_prep_desc_common(wq, desc->hw, DSA_OPCODE_NOOP,
  93. 0, 0, 0, desc->compl_dma, desc_flags);
  94. desc->txd.flags = flags;
  95. return &desc->txd;
  96. }
  97. static struct dma_async_tx_descriptor *
  98. idxd_dma_submit_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
  99. dma_addr_t dma_src, size_t len, unsigned long flags)
  100. {
  101. struct idxd_wq *wq = to_idxd_wq(c);
  102. u32 desc_flags;
  103. struct idxd_device *idxd = wq->idxd;
  104. struct idxd_desc *desc;
  105. if (wq->state != IDXD_WQ_ENABLED)
  106. return NULL;
  107. if (len > idxd->max_xfer_bytes)
  108. return NULL;
  109. op_flag_setup(flags, &desc_flags);
  110. desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
  111. if (IS_ERR(desc))
  112. return NULL;
  113. idxd_prep_desc_common(wq, desc->hw, DSA_OPCODE_MEMMOVE,
  114. dma_src, dma_dest, len, desc->compl_dma,
  115. desc_flags);
  116. desc->txd.flags = flags;
  117. return &desc->txd;
  118. }
  119. static int idxd_dma_alloc_chan_resources(struct dma_chan *chan)
  120. {
  121. struct idxd_wq *wq = to_idxd_wq(chan);
  122. struct device *dev = &wq->idxd->pdev->dev;
  123. idxd_wq_get(wq);
  124. dev_dbg(dev, "%s: client_count: %d\n", __func__,
  125. idxd_wq_refcount(wq));
  126. return 0;
  127. }
  128. static void idxd_dma_free_chan_resources(struct dma_chan *chan)
  129. {
  130. struct idxd_wq *wq = to_idxd_wq(chan);
  131. struct device *dev = &wq->idxd->pdev->dev;
  132. idxd_wq_put(wq);
  133. dev_dbg(dev, "%s: client_count: %d\n", __func__,
  134. idxd_wq_refcount(wq));
  135. }
  136. static enum dma_status idxd_dma_tx_status(struct dma_chan *dma_chan,
  137. dma_cookie_t cookie,
  138. struct dma_tx_state *txstate)
  139. {
  140. return DMA_OUT_OF_ORDER;
  141. }
  142. /*
  143. * issue_pending() does not need to do anything since tx_submit() does the job
  144. * already.
  145. */
  146. static void idxd_dma_issue_pending(struct dma_chan *dma_chan)
  147. {
  148. }
  149. static dma_cookie_t idxd_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  150. {
  151. struct dma_chan *c = tx->chan;
  152. struct idxd_wq *wq = to_idxd_wq(c);
  153. dma_cookie_t cookie;
  154. int rc;
  155. struct idxd_desc *desc = container_of(tx, struct idxd_desc, txd);
  156. cookie = dma_cookie_assign(tx);
  157. rc = idxd_submit_desc(wq, desc);
  158. if (rc < 0) {
  159. idxd_free_desc(wq, desc);
  160. return rc;
  161. }
  162. return cookie;
  163. }
  164. static void idxd_dma_release(struct dma_device *device)
  165. {
  166. struct idxd_dma_dev *idxd_dma = container_of(device, struct idxd_dma_dev, dma);
  167. kfree(idxd_dma);
  168. }
  169. int idxd_register_dma_device(struct idxd_device *idxd)
  170. {
  171. struct idxd_dma_dev *idxd_dma;
  172. struct dma_device *dma;
  173. struct device *dev = &idxd->pdev->dev;
  174. int rc;
  175. idxd_dma = kzalloc_node(sizeof(*idxd_dma), GFP_KERNEL, dev_to_node(dev));
  176. if (!idxd_dma)
  177. return -ENOMEM;
  178. dma = &idxd_dma->dma;
  179. INIT_LIST_HEAD(&dma->channels);
  180. dma->dev = dev;
  181. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  182. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  183. dma_cap_set(DMA_COMPLETION_NO_ORDER, dma->cap_mask);
  184. dma->device_release = idxd_dma_release;
  185. dma->device_prep_dma_interrupt = idxd_dma_prep_interrupt;
  186. if (idxd->hw.opcap.bits[0] & IDXD_OPCAP_MEMMOVE) {
  187. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  188. dma->device_prep_dma_memcpy = idxd_dma_submit_memcpy;
  189. }
  190. dma->device_tx_status = idxd_dma_tx_status;
  191. dma->device_issue_pending = idxd_dma_issue_pending;
  192. dma->device_alloc_chan_resources = idxd_dma_alloc_chan_resources;
  193. dma->device_free_chan_resources = idxd_dma_free_chan_resources;
  194. rc = dma_async_device_register(dma);
  195. if (rc < 0) {
  196. kfree(idxd_dma);
  197. return rc;
  198. }
  199. idxd_dma->idxd = idxd;
  200. /*
  201. * This pointer is protected by the refs taken by the dma_chan. It will remain valid
  202. * as long as there are outstanding channels.
  203. */
  204. idxd->idxd_dma = idxd_dma;
  205. return 0;
  206. }
  207. void idxd_unregister_dma_device(struct idxd_device *idxd)
  208. {
  209. dma_async_device_unregister(&idxd->idxd_dma->dma);
  210. }
  211. static int idxd_register_dma_channel(struct idxd_wq *wq)
  212. {
  213. struct idxd_device *idxd = wq->idxd;
  214. struct dma_device *dma = &idxd->idxd_dma->dma;
  215. struct device *dev = &idxd->pdev->dev;
  216. struct idxd_dma_chan *idxd_chan;
  217. struct dma_chan *chan;
  218. int rc, i;
  219. idxd_chan = kzalloc_node(sizeof(*idxd_chan), GFP_KERNEL, dev_to_node(dev));
  220. if (!idxd_chan)
  221. return -ENOMEM;
  222. chan = &idxd_chan->chan;
  223. chan->device = dma;
  224. list_add_tail(&chan->device_node, &dma->channels);
  225. for (i = 0; i < wq->num_descs; i++) {
  226. struct idxd_desc *desc = wq->descs[i];
  227. dma_async_tx_descriptor_init(&desc->txd, chan);
  228. desc->txd.tx_submit = idxd_dma_tx_submit;
  229. }
  230. rc = dma_async_device_channel_register(dma, chan);
  231. if (rc < 0) {
  232. kfree(idxd_chan);
  233. return rc;
  234. }
  235. wq->idxd_chan = idxd_chan;
  236. idxd_chan->wq = wq;
  237. get_device(wq_confdev(wq));
  238. return 0;
  239. }
  240. static void idxd_unregister_dma_channel(struct idxd_wq *wq)
  241. {
  242. struct idxd_dma_chan *idxd_chan = wq->idxd_chan;
  243. struct dma_chan *chan = &idxd_chan->chan;
  244. struct idxd_dma_dev *idxd_dma = wq->idxd->idxd_dma;
  245. dma_async_device_channel_unregister(&idxd_dma->dma, chan);
  246. list_del(&chan->device_node);
  247. kfree(wq->idxd_chan);
  248. wq->idxd_chan = NULL;
  249. put_device(wq_confdev(wq));
  250. }
  251. static int idxd_dmaengine_drv_probe(struct idxd_dev *idxd_dev)
  252. {
  253. struct device *dev = &idxd_dev->conf_dev;
  254. struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
  255. struct idxd_device *idxd = wq->idxd;
  256. int rc;
  257. if (idxd->state != IDXD_DEV_ENABLED)
  258. return -ENXIO;
  259. mutex_lock(&wq->wq_lock);
  260. wq->type = IDXD_WQT_KERNEL;
  261. rc = drv_enable_wq(wq);
  262. if (rc < 0) {
  263. dev_dbg(dev, "Enable wq %d failed: %d\n", wq->id, rc);
  264. rc = -ENXIO;
  265. goto err;
  266. }
  267. rc = idxd_register_dma_channel(wq);
  268. if (rc < 0) {
  269. idxd->cmd_status = IDXD_SCMD_DMA_CHAN_ERR;
  270. dev_dbg(dev, "Failed to register dma channel\n");
  271. goto err_dma;
  272. }
  273. idxd->cmd_status = 0;
  274. mutex_unlock(&wq->wq_lock);
  275. return 0;
  276. err_dma:
  277. drv_disable_wq(wq);
  278. err:
  279. wq->type = IDXD_WQT_NONE;
  280. mutex_unlock(&wq->wq_lock);
  281. return rc;
  282. }
  283. static void idxd_dmaengine_drv_remove(struct idxd_dev *idxd_dev)
  284. {
  285. struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
  286. mutex_lock(&wq->wq_lock);
  287. __idxd_wq_quiesce(wq);
  288. idxd_unregister_dma_channel(wq);
  289. drv_disable_wq(wq);
  290. mutex_unlock(&wq->wq_lock);
  291. }
  292. static enum idxd_dev_type dev_types[] = {
  293. IDXD_DEV_WQ,
  294. IDXD_DEV_NONE,
  295. };
  296. struct idxd_device_driver idxd_dmaengine_drv = {
  297. .probe = idxd_dmaengine_drv_probe,
  298. .remove = idxd_dmaengine_drv_remove,
  299. .name = "dmaengine",
  300. .type = dev_types,
  301. };
  302. EXPORT_SYMBOL_GPL(idxd_dmaengine_drv);