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
to_idxd_wq(struct dma_chan * c)15 static inline struct idxd_wq *to_idxd_wq(struct dma_chan *c)
16 {
17 struct idxd_dma_chan *idxd_chan;
18
19 idxd_chan = container_of(c, struct idxd_dma_chan, chan);
20 return idxd_chan->wq;
21 }
22
idxd_dma_complete_txd(struct idxd_desc * desc,enum idxd_complete_type comp_type)23 void idxd_dma_complete_txd(struct idxd_desc *desc,
24 enum idxd_complete_type comp_type)
25 {
26 struct dma_async_tx_descriptor *tx;
27 struct dmaengine_result res;
28 int complete = 1;
29
30 if (desc->completion->status == DSA_COMP_SUCCESS)
31 res.result = DMA_TRANS_NOERROR;
32 else if (desc->completion->status)
33 res.result = DMA_TRANS_WRITE_FAILED;
34 else if (comp_type == IDXD_COMPLETE_ABORT)
35 res.result = DMA_TRANS_ABORTED;
36 else
37 complete = 0;
38
39 tx = &desc->txd;
40 if (complete && tx->cookie) {
41 dma_cookie_complete(tx);
42 dma_descriptor_unmap(tx);
43 dmaengine_desc_get_callback_invoke(tx, &res);
44 tx->callback = NULL;
45 tx->callback_result = NULL;
46 }
47 }
48
op_flag_setup(unsigned long flags,u32 * desc_flags)49 static void op_flag_setup(unsigned long flags, u32 *desc_flags)
50 {
51 *desc_flags = IDXD_OP_FLAG_CRAV | IDXD_OP_FLAG_RCR;
52 if (flags & DMA_PREP_INTERRUPT)
53 *desc_flags |= IDXD_OP_FLAG_RCI;
54 }
55
set_completion_address(struct idxd_desc * desc,u64 * compl_addr)56 static inline void set_completion_address(struct idxd_desc *desc,
57 u64 *compl_addr)
58 {
59 *compl_addr = desc->compl_dma;
60 }
61
idxd_prep_desc_common(struct idxd_wq * wq,struct dsa_hw_desc * hw,char opcode,u64 addr_f1,u64 addr_f2,u64 len,u64 compl,u32 flags)62 static inline void idxd_prep_desc_common(struct idxd_wq *wq,
63 struct dsa_hw_desc *hw, char opcode,
64 u64 addr_f1, u64 addr_f2, u64 len,
65 u64 compl, u32 flags)
66 {
67 hw->flags = flags;
68 hw->opcode = opcode;
69 hw->src_addr = addr_f1;
70 hw->dst_addr = addr_f2;
71 hw->xfer_size = len;
72 /*
73 * For dedicated WQ, this field is ignored and HW will use the WQCFG.priv
74 * field instead. This field should be set to 1 for kernel descriptors.
75 */
76 hw->priv = 1;
77 hw->completion_addr = compl;
78 }
79
80 static struct dma_async_tx_descriptor *
idxd_dma_prep_interrupt(struct dma_chan * c,unsigned long flags)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
87 if (wq->state != IDXD_WQ_ENABLED)
88 return NULL;
89
90 op_flag_setup(flags, &desc_flags);
91 desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
92 if (IS_ERR(desc))
93 return NULL;
94
95 idxd_prep_desc_common(wq, desc->hw, DSA_OPCODE_NOOP,
96 0, 0, 0, desc->compl_dma, desc_flags);
97 desc->txd.flags = flags;
98 return &desc->txd;
99 }
100
101 static struct dma_async_tx_descriptor *
idxd_dma_submit_memcpy(struct dma_chan * c,dma_addr_t dma_dest,dma_addr_t dma_src,size_t len,unsigned long flags)102 idxd_dma_submit_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
103 dma_addr_t dma_src, size_t len, unsigned long flags)
104 {
105 struct idxd_wq *wq = to_idxd_wq(c);
106 u32 desc_flags;
107 struct idxd_device *idxd = wq->idxd;
108 struct idxd_desc *desc;
109
110 if (wq->state != IDXD_WQ_ENABLED)
111 return NULL;
112
113 if (len > idxd->max_xfer_bytes)
114 return NULL;
115
116 op_flag_setup(flags, &desc_flags);
117 desc = idxd_alloc_desc(wq, IDXD_OP_BLOCK);
118 if (IS_ERR(desc))
119 return NULL;
120
121 idxd_prep_desc_common(wq, desc->hw, DSA_OPCODE_MEMMOVE,
122 dma_src, dma_dest, len, desc->compl_dma,
123 desc_flags);
124
125 desc->txd.flags = flags;
126
127 return &desc->txd;
128 }
129
idxd_dma_alloc_chan_resources(struct dma_chan * chan)130 static int idxd_dma_alloc_chan_resources(struct dma_chan *chan)
131 {
132 struct idxd_wq *wq = to_idxd_wq(chan);
133 struct device *dev = &wq->idxd->pdev->dev;
134
135 idxd_wq_get(wq);
136 dev_dbg(dev, "%s: client_count: %d\n", __func__,
137 idxd_wq_refcount(wq));
138 return 0;
139 }
140
idxd_dma_free_chan_resources(struct dma_chan * chan)141 static void idxd_dma_free_chan_resources(struct dma_chan *chan)
142 {
143 struct idxd_wq *wq = to_idxd_wq(chan);
144 struct device *dev = &wq->idxd->pdev->dev;
145
146 idxd_wq_put(wq);
147 dev_dbg(dev, "%s: client_count: %d\n", __func__,
148 idxd_wq_refcount(wq));
149 }
150
idxd_dma_tx_status(struct dma_chan * dma_chan,dma_cookie_t cookie,struct dma_tx_state * txstate)151 static enum dma_status idxd_dma_tx_status(struct dma_chan *dma_chan,
152 dma_cookie_t cookie,
153 struct dma_tx_state *txstate)
154 {
155 return DMA_OUT_OF_ORDER;
156 }
157
158 /*
159 * issue_pending() does not need to do anything since tx_submit() does the job
160 * already.
161 */
idxd_dma_issue_pending(struct dma_chan * dma_chan)162 static void idxd_dma_issue_pending(struct dma_chan *dma_chan)
163 {
164 }
165
idxd_dma_tx_submit(struct dma_async_tx_descriptor * tx)166 static dma_cookie_t idxd_dma_tx_submit(struct dma_async_tx_descriptor *tx)
167 {
168 struct dma_chan *c = tx->chan;
169 struct idxd_wq *wq = to_idxd_wq(c);
170 dma_cookie_t cookie;
171 int rc;
172 struct idxd_desc *desc = container_of(tx, struct idxd_desc, txd);
173
174 cookie = dma_cookie_assign(tx);
175
176 rc = idxd_submit_desc(wq, desc);
177 if (rc < 0)
178 return rc;
179
180 return cookie;
181 }
182
idxd_dma_release(struct dma_device * device)183 static void idxd_dma_release(struct dma_device *device)
184 {
185 struct idxd_dma_dev *idxd_dma = container_of(device, struct idxd_dma_dev, dma);
186
187 kfree(idxd_dma);
188 }
189
idxd_register_dma_device(struct idxd_device * idxd)190 int idxd_register_dma_device(struct idxd_device *idxd)
191 {
192 struct idxd_dma_dev *idxd_dma;
193 struct dma_device *dma;
194 struct device *dev = &idxd->pdev->dev;
195 int rc;
196
197 idxd_dma = kzalloc_node(sizeof(*idxd_dma), GFP_KERNEL, dev_to_node(dev));
198 if (!idxd_dma)
199 return -ENOMEM;
200
201 dma = &idxd_dma->dma;
202 INIT_LIST_HEAD(&dma->channels);
203 dma->dev = dev;
204
205 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
206 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
207 dma_cap_set(DMA_COMPLETION_NO_ORDER, dma->cap_mask);
208 dma->device_release = idxd_dma_release;
209
210 dma->device_prep_dma_interrupt = idxd_dma_prep_interrupt;
211 if (idxd->hw.opcap.bits[0] & IDXD_OPCAP_MEMMOVE) {
212 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
213 dma->device_prep_dma_memcpy = idxd_dma_submit_memcpy;
214 }
215
216 dma->device_tx_status = idxd_dma_tx_status;
217 dma->device_issue_pending = idxd_dma_issue_pending;
218 dma->device_alloc_chan_resources = idxd_dma_alloc_chan_resources;
219 dma->device_free_chan_resources = idxd_dma_free_chan_resources;
220
221 rc = dma_async_device_register(dma);
222 if (rc < 0) {
223 kfree(idxd_dma);
224 return rc;
225 }
226
227 idxd_dma->idxd = idxd;
228 /*
229 * This pointer is protected by the refs taken by the dma_chan. It will remain valid
230 * as long as there are outstanding channels.
231 */
232 idxd->idxd_dma = idxd_dma;
233 return 0;
234 }
235
idxd_unregister_dma_device(struct idxd_device * idxd)236 void idxd_unregister_dma_device(struct idxd_device *idxd)
237 {
238 dma_async_device_unregister(&idxd->idxd_dma->dma);
239 }
240
idxd_register_dma_channel(struct idxd_wq * wq)241 int idxd_register_dma_channel(struct idxd_wq *wq)
242 {
243 struct idxd_device *idxd = wq->idxd;
244 struct dma_device *dma = &idxd->idxd_dma->dma;
245 struct device *dev = &idxd->pdev->dev;
246 struct idxd_dma_chan *idxd_chan;
247 struct dma_chan *chan;
248 int rc, i;
249
250 idxd_chan = kzalloc_node(sizeof(*idxd_chan), GFP_KERNEL, dev_to_node(dev));
251 if (!idxd_chan)
252 return -ENOMEM;
253
254 chan = &idxd_chan->chan;
255 chan->device = dma;
256 list_add_tail(&chan->device_node, &dma->channels);
257
258 for (i = 0; i < wq->num_descs; i++) {
259 struct idxd_desc *desc = wq->descs[i];
260
261 dma_async_tx_descriptor_init(&desc->txd, chan);
262 desc->txd.tx_submit = idxd_dma_tx_submit;
263 }
264
265 rc = dma_async_device_channel_register(dma, chan);
266 if (rc < 0) {
267 kfree(idxd_chan);
268 return rc;
269 }
270
271 wq->idxd_chan = idxd_chan;
272 idxd_chan->wq = wq;
273 get_device(wq_confdev(wq));
274
275 return 0;
276 }
277
idxd_unregister_dma_channel(struct idxd_wq * wq)278 void idxd_unregister_dma_channel(struct idxd_wq *wq)
279 {
280 struct idxd_dma_chan *idxd_chan = wq->idxd_chan;
281 struct dma_chan *chan = &idxd_chan->chan;
282 struct idxd_dma_dev *idxd_dma = wq->idxd->idxd_dma;
283
284 dma_async_device_channel_unregister(&idxd_dma->dma, chan);
285 list_del(&chan->device_node);
286 kfree(wq->idxd_chan);
287 wq->idxd_chan = NULL;
288 put_device(wq_confdev(wq));
289 }
290
idxd_dmaengine_drv_probe(struct idxd_dev * idxd_dev)291 static int idxd_dmaengine_drv_probe(struct idxd_dev *idxd_dev)
292 {
293 struct device *dev = &idxd_dev->conf_dev;
294 struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
295 struct idxd_device *idxd = wq->idxd;
296 int rc;
297
298 if (idxd->state != IDXD_DEV_ENABLED)
299 return -ENXIO;
300
301 mutex_lock(&wq->wq_lock);
302 wq->type = IDXD_WQT_KERNEL;
303 rc = __drv_enable_wq(wq);
304 if (rc < 0) {
305 dev_dbg(dev, "Enable wq %d failed: %d\n", wq->id, rc);
306 rc = -ENXIO;
307 goto err;
308 }
309
310 rc = idxd_wq_alloc_resources(wq);
311 if (rc < 0) {
312 idxd->cmd_status = IDXD_SCMD_WQ_RES_ALLOC_ERR;
313 dev_dbg(dev, "WQ resource alloc failed\n");
314 goto err_res_alloc;
315 }
316
317 rc = idxd_wq_init_percpu_ref(wq);
318 if (rc < 0) {
319 idxd->cmd_status = IDXD_SCMD_PERCPU_ERR;
320 dev_dbg(dev, "percpu_ref setup failed\n");
321 goto err_ref;
322 }
323
324 rc = idxd_register_dma_channel(wq);
325 if (rc < 0) {
326 idxd->cmd_status = IDXD_SCMD_DMA_CHAN_ERR;
327 dev_dbg(dev, "Failed to register dma channel\n");
328 goto err_dma;
329 }
330
331 idxd->cmd_status = 0;
332 mutex_unlock(&wq->wq_lock);
333 return 0;
334
335 err_dma:
336 idxd_wq_quiesce(wq);
337 percpu_ref_exit(&wq->wq_active);
338 err_ref:
339 idxd_wq_free_resources(wq);
340 err_res_alloc:
341 __drv_disable_wq(wq);
342 err:
343 wq->type = IDXD_WQT_NONE;
344 mutex_unlock(&wq->wq_lock);
345 return rc;
346 }
347
idxd_dmaengine_drv_remove(struct idxd_dev * idxd_dev)348 static void idxd_dmaengine_drv_remove(struct idxd_dev *idxd_dev)
349 {
350 struct idxd_wq *wq = idxd_dev_to_wq(idxd_dev);
351
352 mutex_lock(&wq->wq_lock);
353 idxd_wq_quiesce(wq);
354 idxd_unregister_dma_channel(wq);
355 idxd_wq_free_resources(wq);
356 __drv_disable_wq(wq);
357 percpu_ref_exit(&wq->wq_active);
358 mutex_unlock(&wq->wq_lock);
359 }
360
361 static enum idxd_dev_type dev_types[] = {
362 IDXD_DEV_WQ,
363 IDXD_DEV_NONE,
364 };
365
366 struct idxd_device_driver idxd_dmaengine_drv = {
367 .probe = idxd_dmaengine_drv_probe,
368 .remove = idxd_dmaengine_drv_remove,
369 .name = "dmaengine",
370 .type = dev_types,
371 };
372 EXPORT_SYMBOL_GPL(idxd_dmaengine_drv);
373