xref: /linux/drivers/dma/dw-edma/dw-edma-core.c (revision 66498c75b4f8017f62d720d9b59675bdf3abce91)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018-2019 Synopsys, Inc. and/or its affiliates.
4  * Synopsys DesignWare eDMA core driver
5  *
6  * Author: Gustavo Pimentel <gustavo.pimentel@synopsys.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/dmaengine.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/dma/edma.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/string_choices.h>
20 
21 #include "dw-edma-core.h"
22 #include "dw-edma-v0-core.h"
23 #include "dw-hdma-v0-core.h"
24 #include "../dmaengine.h"
25 #include "../virt-dma.h"
26 
27 static inline
vd2dw_edma_desc(struct virt_dma_desc * vd)28 struct dw_edma_desc *vd2dw_edma_desc(struct virt_dma_desc *vd)
29 {
30 	return container_of(vd, struct dw_edma_desc, vd);
31 }
32 
33 enum dw_edma_irq_event {
34 	DW_EDMA_IRQ_DONE	= BIT(0),
35 	DW_EDMA_IRQ_ABORT	= BIT(1),
36 };
37 
38 static inline
dw_edma_get_pci_address(struct dw_edma_chan * chan,phys_addr_t cpu_addr)39 u64 dw_edma_get_pci_address(struct dw_edma_chan *chan, phys_addr_t cpu_addr)
40 {
41 	struct dw_edma_chip *chip = chan->dw->chip;
42 
43 	if (chip->ops->pci_address)
44 		return chip->ops->pci_address(chip->dev, cpu_addr);
45 
46 	return cpu_addr;
47 }
48 
49 static struct dw_edma_desc *
dw_edma_alloc_desc(struct dw_edma_chan * chan,size_t nburst)50 dw_edma_alloc_desc(struct dw_edma_chan *chan, size_t nburst)
51 {
52 	struct dw_edma_desc *desc;
53 
54 	desc = kzalloc_flex(*desc, burst, nburst, GFP_NOWAIT);
55 	if (unlikely(!desc))
56 		return NULL;
57 
58 	desc->chan = chan;
59 	desc->nburst = nburst;
60 	desc->cb = true;
61 
62 	return desc;
63 }
64 
vchan_free_desc(struct virt_dma_desc * vdesc)65 static void vchan_free_desc(struct virt_dma_desc *vdesc)
66 {
67 	kfree(vd2dw_edma_desc(vdesc));
68 }
69 
dw_edma_core_start(struct dw_edma_desc * desc,bool first)70 static void dw_edma_core_start(struct dw_edma_desc *desc, bool first)
71 {
72 	struct dw_edma_chan *chan = desc->chan;
73 	size_t i = 0;
74 
75 	if (chan->non_ll) {
76 		chan->dw->core->non_ll_start(chan, &desc->burst[desc->start_burst]);
77 		desc->done_burst = desc->start_burst;
78 		desc->start_burst += 1;
79 		return;
80 	}
81 
82 	for (i = 0; i + desc->start_burst < desc->nburst; i++) {
83 		u32 idx = i + desc->start_burst;
84 
85 		if (i == chan->ll_max)
86 			break;
87 
88 		dw_edma_core_ll_data(chan, &desc->burst[idx],
89 				     i, desc->cb,
90 				     idx == desc->nburst - 1 || i == chan->ll_max - 1);
91 	}
92 
93 	desc->done_burst = desc->start_burst;
94 	desc->start_burst += i;
95 
96 	dw_edma_core_ll_link(chan, i, desc->cb, chan->ll_region.paddr);
97 
98 	if (first)
99 		dw_edma_core_ch_enable(chan);
100 
101 	dw_edma_core_ch_doorbell(chan);
102 }
103 
dw_edma_start_transfer(struct dw_edma_chan * chan)104 static int dw_edma_start_transfer(struct dw_edma_chan *chan)
105 {
106 	struct dw_edma_desc *desc;
107 	struct virt_dma_desc *vd;
108 
109 	vd = vchan_next_desc(&chan->vc);
110 	if (!vd)
111 		return 0;
112 
113 	desc = vd2dw_edma_desc(vd);
114 	if (!desc)
115 		return 0;
116 
117 	dw_edma_core_start(desc, !desc->start_burst);
118 
119 	desc->cb = !desc->cb;
120 
121 	return 1;
122 }
123 
dw_edma_terminate_vdesc(struct virt_dma_desc * vd)124 static void dw_edma_terminate_vdesc(struct virt_dma_desc *vd)
125 {
126 	list_del(&vd->node);
127 	dma_cookie_complete(&vd->tx);
128 	vchan_terminate_vdesc(vd);
129 }
130 
dw_edma_terminate_vdesc_list(struct list_head * head)131 static void dw_edma_terminate_vdesc_list(struct list_head *head)
132 {
133 	struct virt_dma_desc *vd, *_vd;
134 
135 	list_for_each_entry_safe(vd, _vd, head, node)
136 		dw_edma_terminate_vdesc(vd);
137 }
138 
139 /* Must be called with vc.lock held. */
dw_edma_terminate_all_descs(struct dw_edma_chan * chan)140 static void dw_edma_terminate_all_descs(struct dw_edma_chan *chan)
141 {
142 	/*
143 	 * This order must not be reversed. Cookies are assigned when
144 	 * descriptors are submitted, so desc_issued contains older cookies
145 	 * than desc_submitted. Completing desc_submitted first could move
146 	 * chan->vc.chan.completed_cookie backwards when desc_issued is
147 	 * terminated afterwards.
148 	 */
149 	dw_edma_terminate_vdesc_list(&chan->vc.desc_issued);
150 	dw_edma_terminate_vdesc_list(&chan->vc.desc_submitted);
151 }
152 
dw_edma_device_caps(struct dma_chan * dchan,struct dma_slave_caps * caps)153 static void dw_edma_device_caps(struct dma_chan *dchan,
154 				struct dma_slave_caps *caps)
155 {
156 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
157 
158 	if (chan->dw->chip->flags & DW_EDMA_CHIP_LOCAL) {
159 		if (chan->dir == EDMA_DIR_READ)
160 			caps->directions = BIT(DMA_DEV_TO_MEM);
161 		else
162 			caps->directions = BIT(DMA_MEM_TO_DEV);
163 	} else {
164 		if (chan->dir == EDMA_DIR_WRITE)
165 			caps->directions = BIT(DMA_DEV_TO_MEM);
166 		else
167 			caps->directions = BIT(DMA_MEM_TO_DEV);
168 	}
169 }
170 
171 static enum dw_edma_ch_irq_mode
dw_edma_get_default_irq_mode(struct dw_edma_chan * chan)172 dw_edma_get_default_irq_mode(struct dw_edma_chan *chan)
173 {
174 	struct dw_edma_chip *chip = chan->dw->chip;
175 
176 	return chip->flags & DW_EDMA_CHIP_LOCAL ? DW_EDMA_CH_IRQ_LOCAL :
177 						  DW_EDMA_CH_IRQ_REMOTE;
178 }
179 
dw_edma_device_config(struct dma_chan * dchan,struct dma_slave_config * config)180 static int dw_edma_device_config(struct dma_chan *dchan,
181 				 struct dma_slave_config *config)
182 {
183 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
184 	bool cfg_non_ll;
185 	int non_ll = 0;
186 
187 	chan->non_ll = false;
188 	if (chan->dw->chip->mf == EDMA_MF_HDMA_NATIVE) {
189 		if (config->peripheral_config &&
190 		    config->peripheral_size != sizeof(int)) {
191 			dev_err(dchan->device->dev,
192 				"config param peripheral size mismatch\n");
193 			return -EINVAL;
194 		}
195 
196 		/*
197 		 * When there is no valid LLP base address available then the
198 		 * default DMA ops will use the non-LL mode.
199 		 *
200 		 * Cases where LL mode is enabled and client wants to use the
201 		 * non-LL mode then also client can do so via providing the
202 		 * peripheral_config param.
203 		 */
204 		cfg_non_ll = chan->dw->chip->cfg_non_ll;
205 		if (config->peripheral_config) {
206 			non_ll = *(int *)config->peripheral_config;
207 
208 			if (cfg_non_ll && !non_ll) {
209 				dev_err(dchan->device->dev, "invalid configuration\n");
210 				return -EINVAL;
211 			}
212 		}
213 
214 		if (cfg_non_ll || non_ll)
215 			chan->non_ll = true;
216 	} else if (config->peripheral_config) {
217 		dev_err(dchan->device->dev,
218 			"peripheral config param applicable only for HDMA\n");
219 		return -EINVAL;
220 	}
221 
222 	memcpy(&chan->config, config, sizeof(*config));
223 	chan->configured = true;
224 
225 	return 0;
226 }
227 
228 static struct dma_slave_config *
dw_edma_device_get_config(struct dma_chan * dchan,struct dma_slave_config * config)229 dw_edma_device_get_config(struct dma_chan *dchan,
230 			  struct dma_slave_config *config)
231 {
232 	struct dw_edma_chan *chan;
233 
234 	if (config)
235 		return config;
236 
237 	chan = dchan2dw_edma_chan(dchan);
238 
239 	return &chan->config;
240 }
241 
dw_edma_device_pause(struct dma_chan * dchan)242 static int dw_edma_device_pause(struct dma_chan *dchan)
243 {
244 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
245 	int err = 0;
246 
247 	guard(spinlock_irqsave)(&chan->vc.lock);
248 
249 	if (!chan->configured)
250 		err = -EPERM;
251 	else if (chan->status != EDMA_ST_BUSY)
252 		err = -EPERM;
253 	else if (chan->request != EDMA_REQ_NONE)
254 		err = -EPERM;
255 	else
256 		chan->request = EDMA_REQ_PAUSE;
257 
258 	return err;
259 }
260 
dw_edma_device_resume(struct dma_chan * dchan)261 static int dw_edma_device_resume(struct dma_chan *dchan)
262 {
263 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
264 	int err = 0;
265 
266 	guard(spinlock_irqsave)(&chan->vc.lock);
267 
268 	if (!chan->configured) {
269 		err = -EPERM;
270 	} else if (chan->status != EDMA_ST_PAUSE) {
271 		err = -EPERM;
272 	} else if (chan->request != EDMA_REQ_NONE) {
273 		err = -EPERM;
274 	} else {
275 		chan->status = EDMA_ST_BUSY;
276 		if (!dw_edma_start_transfer(chan))
277 			chan->status = EDMA_ST_IDLE;
278 	}
279 
280 	return err;
281 }
282 
dw_edma_device_terminate_all(struct dma_chan * dchan)283 static int dw_edma_device_terminate_all(struct dma_chan *dchan)
284 {
285 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
286 	int err = 0;
287 
288 	guard(spinlock_irqsave)(&chan->vc.lock);
289 
290 	if (!chan->configured) {
291 		dw_edma_terminate_all_descs(chan);
292 	} else if (chan->status == EDMA_ST_PAUSE) {
293 		dw_edma_terminate_all_descs(chan);
294 		chan->status = EDMA_ST_IDLE;
295 	} else if (chan->status == EDMA_ST_IDLE) {
296 		dw_edma_terminate_all_descs(chan);
297 	} else if (dw_edma_core_ch_status(chan) == DMA_COMPLETE) {
298 		/*
299 		 * The channel is in a false BUSY state, probably didn't
300 		 * receive or lost an interrupt
301 		 */
302 		dw_edma_terminate_all_descs(chan);
303 		chan->status = EDMA_ST_IDLE;
304 	} else if (chan->request > EDMA_REQ_PAUSE) {
305 		err = -EPERM;
306 	} else {
307 		chan->request = EDMA_REQ_STOP;
308 	}
309 	if (chan->status == EDMA_ST_IDLE)
310 		chan->request = EDMA_REQ_NONE;
311 
312 	return err;
313 }
314 
dw_edma_device_issue_pending(struct dma_chan * dchan)315 static void dw_edma_device_issue_pending(struct dma_chan *dchan)
316 {
317 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
318 	unsigned long flags;
319 
320 	spin_lock_irqsave(&chan->vc.lock, flags);
321 	if (chan->configured && vchan_issue_pending(&chan->vc) &&
322 	    chan->request == EDMA_REQ_NONE &&
323 	    chan->status == EDMA_ST_IDLE) {
324 		chan->status = EDMA_ST_BUSY;
325 		dw_edma_start_transfer(chan);
326 	}
327 	spin_unlock_irqrestore(&chan->vc.lock, flags);
328 }
329 
330 static enum dma_status
dw_edma_device_tx_status(struct dma_chan * dchan,dma_cookie_t cookie,struct dma_tx_state * txstate)331 dw_edma_device_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
332 			 struct dma_tx_state *txstate)
333 {
334 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
335 	struct dw_edma_desc *desc;
336 	struct virt_dma_desc *vd;
337 	unsigned long flags;
338 	enum dma_status ret;
339 	u32 residue = 0;
340 
341 	ret = dma_cookie_status(dchan, cookie, txstate);
342 	if (ret == DMA_COMPLETE)
343 		return ret;
344 
345 	if (ret == DMA_IN_PROGRESS && chan->status == EDMA_ST_PAUSE)
346 		ret = DMA_PAUSED;
347 
348 	if (!txstate)
349 		goto ret_residue;
350 
351 	spin_lock_irqsave(&chan->vc.lock, flags);
352 	vd = vchan_find_desc(&chan->vc, cookie);
353 	if (vd) {
354 		desc = vd2dw_edma_desc(vd);
355 
356 		residue = desc->alloc_sz;
357 		if (desc && desc->done_burst)
358 			residue -= desc->burst[desc->done_burst - 1].xfer_sz;
359 	}
360 	spin_unlock_irqrestore(&chan->vc.lock, flags);
361 
362 ret_residue:
363 	dma_set_residue(txstate, residue);
364 
365 	return ret;
366 }
367 
368 static struct dma_async_tx_descriptor *
dw_edma_device_transfer(struct dw_edma_transfer * xfer,struct dma_slave_config * config)369 dw_edma_device_transfer(struct dw_edma_transfer *xfer,
370 			struct dma_slave_config *config)
371 {
372 	struct dw_edma_chan *chan = dchan2dw_edma_chan(xfer->dchan);
373 	enum dma_transfer_direction dir = xfer->direction;
374 	struct scatterlist *sg = NULL;
375 	struct dw_edma_burst *burst;
376 	struct dw_edma_desc *desc;
377 	u64 src_addr, dst_addr;
378 	size_t fsz = 0;
379 	size_t cnt = 0;
380 	u32 i;
381 
382 	if (!chan->configured)
383 		return NULL;
384 
385 	/*
386 	 * Local Root Port/End-point              Remote End-point
387 	 * +-----------------------+ PCIe bus +----------------------+
388 	 * |                       |    +-+   |                      |
389 	 * |    DEV_TO_MEM   Rx Ch <----+ +---+ Tx Ch  DEV_TO_MEM    |
390 	 * |                       |    | |   |                      |
391 	 * |    MEM_TO_DEV   Tx Ch +----+ +---> Rx Ch  MEM_TO_DEV    |
392 	 * |                       |    +-+   |                      |
393 	 * +-----------------------+          +----------------------+
394 	 *
395 	 * 1. Normal logic:
396 	 * If eDMA is embedded into the DW PCIe RP/EP and controlled from the
397 	 * CPU/Application side, the Rx channel (EDMA_DIR_READ) will be used
398 	 * for the device read operations (DEV_TO_MEM) and the Tx channel
399 	 * (EDMA_DIR_WRITE) - for the write operations (MEM_TO_DEV).
400 	 *
401 	 * 2. Inverted logic:
402 	 * If eDMA is embedded into a Remote PCIe EP and is controlled by the
403 	 * MWr/MRd TLPs sent from the CPU's PCIe host controller, the Tx
404 	 * channel (EDMA_DIR_WRITE) will be used for the device read operations
405 	 * (DEV_TO_MEM) and the Rx channel (EDMA_DIR_READ) - for the write
406 	 * operations (MEM_TO_DEV).
407 	 *
408 	 * It is the client driver responsibility to choose a proper channel
409 	 * for the DMA transfers.
410 	 */
411 	if (chan->dw->chip->flags & DW_EDMA_CHIP_LOCAL) {
412 		if ((chan->dir == EDMA_DIR_READ && dir != DMA_DEV_TO_MEM) ||
413 		    (chan->dir == EDMA_DIR_WRITE && dir != DMA_MEM_TO_DEV))
414 			return NULL;
415 	} else {
416 		if ((chan->dir == EDMA_DIR_WRITE && dir != DMA_DEV_TO_MEM) ||
417 		    (chan->dir == EDMA_DIR_READ && dir != DMA_MEM_TO_DEV))
418 			return NULL;
419 	}
420 
421 	if (xfer->type == EDMA_XFER_CYCLIC) {
422 		if (!xfer->xfer.cyclic.len || !xfer->xfer.cyclic.cnt)
423 			return NULL;
424 	} else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
425 		if (xfer->xfer.sg.len < 1)
426 			return NULL;
427 	} else if (xfer->type == EDMA_XFER_INTERLEAVED) {
428 		if (!xfer->xfer.il->numf || xfer->xfer.il->frame_size < 1)
429 			return NULL;
430 		if (!xfer->xfer.il->src_inc || !xfer->xfer.il->dst_inc)
431 			return NULL;
432 	} else {
433 		return NULL;
434 	}
435 
436 	if (xfer->type == EDMA_XFER_INTERLEAVED) {
437 		src_addr = xfer->xfer.il->src_start;
438 		dst_addr = xfer->xfer.il->dst_start;
439 	} else {
440 		src_addr = config->src_addr;
441 		dst_addr = config->dst_addr;
442 	}
443 
444 	if (dir == DMA_DEV_TO_MEM)
445 		src_addr = dw_edma_get_pci_address(chan, (phys_addr_t)src_addr);
446 	else
447 		dst_addr = dw_edma_get_pci_address(chan, (phys_addr_t)dst_addr);
448 
449 	if (xfer->type == EDMA_XFER_CYCLIC) {
450 		cnt = xfer->xfer.cyclic.cnt;
451 	} else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
452 		cnt = xfer->xfer.sg.len;
453 		sg = xfer->xfer.sg.sgl;
454 	} else if (xfer->type == EDMA_XFER_INTERLEAVED) {
455 		cnt = xfer->xfer.il->numf * xfer->xfer.il->frame_size;
456 		fsz = xfer->xfer.il->frame_size;
457 	}
458 
459 	desc = dw_edma_alloc_desc(chan, cnt);
460 	if (unlikely(!desc))
461 		return NULL;
462 
463 	for (i = 0; i < cnt; i++) {
464 		if (xfer->type == EDMA_XFER_SCATTER_GATHER && !sg)
465 			break;
466 
467 		burst = desc->burst + i;
468 
469 		if (xfer->type == EDMA_XFER_CYCLIC)
470 			burst->sz = xfer->xfer.cyclic.len;
471 		else if (xfer->type == EDMA_XFER_SCATTER_GATHER)
472 			burst->sz = sg_dma_len(sg);
473 		else if (xfer->type == EDMA_XFER_INTERLEAVED)
474 			burst->sz = xfer->xfer.il->sgl[i % fsz].size;
475 
476 		desc->alloc_sz += burst->sz;
477 		burst->xfer_sz = desc->alloc_sz;
478 
479 		if (dir == DMA_DEV_TO_MEM) {
480 			burst->sar = src_addr;
481 			if (xfer->type == EDMA_XFER_CYCLIC) {
482 				burst->dar = xfer->xfer.cyclic.paddr;
483 			} else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
484 				src_addr += sg_dma_len(sg);
485 				burst->dar = sg_dma_address(sg);
486 				/* Unlike the typical assumption by other
487 				 * drivers/IPs the peripheral memory isn't
488 				 * a FIFO memory, in this case, it's a
489 				 * linear memory and that why the source
490 				 * and destination addresses are increased
491 				 * by the same portion (data length)
492 				 */
493 			} else if (xfer->type == EDMA_XFER_INTERLEAVED) {
494 				burst->dar = dst_addr;
495 			}
496 		} else {
497 			burst->dar = dst_addr;
498 			if (xfer->type == EDMA_XFER_CYCLIC) {
499 				burst->sar = xfer->xfer.cyclic.paddr;
500 			} else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
501 				dst_addr += sg_dma_len(sg);
502 				burst->sar = sg_dma_address(sg);
503 				/* Unlike the typical assumption by other
504 				 * drivers/IPs the peripheral memory isn't
505 				 * a FIFO memory, in this case, it's a
506 				 * linear memory and that why the source
507 				 * and destination addresses are increased
508 				 * by the same portion (data length)
509 				 */
510 			}  else if (xfer->type == EDMA_XFER_INTERLEAVED) {
511 				burst->sar = src_addr;
512 			}
513 		}
514 
515 		if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
516 			sg = sg_next(sg);
517 		} else if (xfer->type == EDMA_XFER_INTERLEAVED) {
518 			struct dma_interleaved_template *il = xfer->xfer.il;
519 			struct data_chunk *dc = &il->sgl[i % fsz];
520 
521 			src_addr += burst->sz;
522 			if (il->src_sgl)
523 				src_addr += dmaengine_get_src_icg(il, dc);
524 
525 			dst_addr += burst->sz;
526 			if (il->dst_sgl)
527 				dst_addr += dmaengine_get_dst_icg(il, dc);
528 		}
529 	}
530 
531 	return vchan_tx_prep(&chan->vc, &desc->vd, xfer->flags);
532 }
533 
534 static struct dma_async_tx_descriptor *
dw_edma_device_prep_config_sg(struct dma_chan * dchan,struct scatterlist * sgl,unsigned int len,enum dma_transfer_direction direction,unsigned long flags,struct dma_slave_config * config)535 dw_edma_device_prep_config_sg(struct dma_chan *dchan, struct scatterlist *sgl,
536 			      unsigned int len,
537 			      enum dma_transfer_direction direction,
538 			      unsigned long flags,
539 			      struct dma_slave_config *config)
540 {
541 	struct dw_edma_transfer xfer;
542 
543 	xfer.dchan = dchan;
544 	xfer.direction = direction;
545 	xfer.xfer.sg.sgl = sgl;
546 	xfer.xfer.sg.len = len;
547 	xfer.flags = flags;
548 	xfer.type = EDMA_XFER_SCATTER_GATHER;
549 
550 	if (config && dw_edma_device_config(dchan, config))
551 		return NULL;
552 
553 	return dw_edma_device_transfer(&xfer, dw_edma_device_get_config(dchan, config));
554 }
555 
556 static struct dma_async_tx_descriptor *
dw_edma_device_prep_dma_cyclic(struct dma_chan * dchan,dma_addr_t paddr,size_t len,size_t count,enum dma_transfer_direction direction,unsigned long flags)557 dw_edma_device_prep_dma_cyclic(struct dma_chan *dchan, dma_addr_t paddr,
558 			       size_t len, size_t count,
559 			       enum dma_transfer_direction direction,
560 			       unsigned long flags)
561 {
562 	struct dw_edma_transfer xfer;
563 
564 	xfer.dchan = dchan;
565 	xfer.direction = direction;
566 	xfer.xfer.cyclic.paddr = paddr;
567 	xfer.xfer.cyclic.len = len;
568 	xfer.xfer.cyclic.cnt = count;
569 	xfer.flags = flags;
570 	xfer.type = EDMA_XFER_CYCLIC;
571 
572 	return dw_edma_device_transfer(&xfer, dw_edma_device_get_config(dchan, NULL));
573 }
574 
575 static struct dma_async_tx_descriptor *
dw_edma_device_prep_interleaved_dma(struct dma_chan * dchan,struct dma_interleaved_template * ilt,unsigned long flags)576 dw_edma_device_prep_interleaved_dma(struct dma_chan *dchan,
577 				    struct dma_interleaved_template *ilt,
578 				    unsigned long flags)
579 {
580 	struct dw_edma_transfer xfer;
581 
582 	xfer.dchan = dchan;
583 	xfer.direction = ilt->dir;
584 	xfer.xfer.il = ilt;
585 	xfer.flags = flags;
586 	xfer.type = EDMA_XFER_INTERLEAVED;
587 
588 	return dw_edma_device_transfer(&xfer, dw_edma_device_get_config(dchan, NULL));
589 }
590 
dw_hdma_set_callback_result(struct virt_dma_desc * vd,enum dmaengine_tx_result result)591 static void dw_hdma_set_callback_result(struct virt_dma_desc *vd,
592 					enum dmaengine_tx_result result)
593 {
594 	u32 residue = 0;
595 	struct dw_edma_desc *desc;
596 	struct dmaengine_result *res;
597 
598 	if (!vd->tx.callback_result)
599 		return;
600 
601 	desc = vd2dw_edma_desc(vd);
602 	if (desc) {
603 		residue = desc->alloc_sz;
604 
605 		if (result == DMA_TRANS_NOERROR)
606 			residue -= desc->burst[desc->start_burst - 1].xfer_sz;
607 		else if (desc->done_burst)
608 			residue -= desc->burst[desc->done_burst - 1].xfer_sz;
609 	}
610 
611 	res = &vd->tx_result;
612 	res->result = result;
613 	res->residue = residue;
614 }
615 
dw_edma_done_interrupt(struct dw_edma_chan * chan)616 static void dw_edma_done_interrupt(struct dw_edma_chan *chan)
617 {
618 	struct dw_edma_desc *desc;
619 	struct virt_dma_desc *vd;
620 	unsigned long flags;
621 
622 	spin_lock_irqsave(&chan->vc.lock, flags);
623 	if (chan->status == EDMA_ST_PAUSE) {
624 		spin_unlock_irqrestore(&chan->vc.lock, flags);
625 		return;
626 	}
627 
628 	vd = vchan_next_desc(&chan->vc);
629 	if (vd) {
630 		switch (chan->request) {
631 		case EDMA_REQ_NONE:
632 		case EDMA_REQ_PAUSE:
633 			desc = vd2dw_edma_desc(vd);
634 			if (desc->start_burst >= desc->nburst) {
635 				dw_hdma_set_callback_result(vd,
636 							    DMA_TRANS_NOERROR);
637 				list_del(&vd->node);
638 				vchan_cookie_complete(vd);
639 			}
640 
641 			if (chan->request == EDMA_REQ_PAUSE) {
642 				chan->request = EDMA_REQ_NONE;
643 				chan->status = EDMA_ST_PAUSE;
644 				break;
645 			}
646 
647 			/* Continue transferring if there are remaining chunks or issued requests.
648 			 */
649 			chan->status = dw_edma_start_transfer(chan) ? EDMA_ST_BUSY : EDMA_ST_IDLE;
650 			break;
651 
652 		case EDMA_REQ_STOP:
653 			dw_edma_terminate_all_descs(chan);
654 			chan->request = EDMA_REQ_NONE;
655 			chan->status = EDMA_ST_IDLE;
656 			break;
657 
658 		default:
659 			break;
660 		}
661 	}
662 	spin_unlock_irqrestore(&chan->vc.lock, flags);
663 }
664 
dw_edma_abort_interrupt(struct dw_edma_chan * chan)665 static void dw_edma_abort_interrupt(struct dw_edma_chan *chan)
666 {
667 	struct virt_dma_desc *vd;
668 	unsigned long flags;
669 
670 	spin_lock_irqsave(&chan->vc.lock, flags);
671 	vd = vchan_next_desc(&chan->vc);
672 	if (vd && chan->request == EDMA_REQ_STOP) {
673 		dw_edma_terminate_all_descs(chan);
674 	} else if (vd) {
675 		dw_hdma_set_callback_result(vd, DMA_TRANS_ABORTED);
676 		list_del(&vd->node);
677 		vchan_cookie_complete(vd);
678 	}
679 	chan->request = EDMA_REQ_NONE;
680 	chan->status = EDMA_ST_IDLE;
681 	spin_unlock_irqrestore(&chan->vc.lock, flags);
682 }
683 
dw_edma_irq_work(struct work_struct * work)684 static void dw_edma_irq_work(struct work_struct *work)
685 {
686 	struct dw_edma_chan *chan = container_of(work, struct dw_edma_chan,
687 						 irq_work);
688 	unsigned int events;
689 
690 	do {
691 		events = atomic_xchg(&chan->irq_pending, 0);
692 
693 		if (events & DW_EDMA_IRQ_DONE)
694 			dw_edma_done_interrupt(chan);
695 		if (events & DW_EDMA_IRQ_ABORT)
696 			dw_edma_abort_interrupt(chan);
697 	} while (atomic_read(&chan->irq_pending));
698 }
699 
dw_edma_queue_irq_work(struct dw_edma_chan * chan,enum dw_edma_irq_event event)700 static void dw_edma_queue_irq_work(struct dw_edma_chan *chan,
701 				   enum dw_edma_irq_event event)
702 {
703 	atomic_or(event, &chan->irq_pending);
704 	queue_work(chan->dw->wq, &chan->irq_work);
705 }
706 
dw_edma_done_interrupt_deferred(struct dw_edma_chan * chan)707 static void dw_edma_done_interrupt_deferred(struct dw_edma_chan *chan)
708 {
709 	dw_edma_queue_irq_work(chan, DW_EDMA_IRQ_DONE);
710 }
711 
dw_edma_abort_interrupt_deferred(struct dw_edma_chan * chan)712 static void dw_edma_abort_interrupt_deferred(struct dw_edma_chan *chan)
713 {
714 	dw_edma_queue_irq_work(chan, DW_EDMA_IRQ_ABORT);
715 }
716 
dw_edma_emul_irq_ack(struct irq_data * d)717 static void dw_edma_emul_irq_ack(struct irq_data *d)
718 {
719 	struct dw_edma *dw = irq_data_get_irq_chip_data(d);
720 
721 	dw_edma_core_ack_emulated_irq(dw);
722 }
723 
724 /*
725  * irq_chip implementation for interrupt-emulation doorbells.
726  *
727  * The emulated source has no mask/unmask mechanism. With handle_level_irq(),
728  * the flow is therefore:
729  *   1) .irq_ack() deasserts the source
730  *   2) registered handlers (if any) are dispatched
731  * Since deassertion is already done in .irq_ack(), handlers do not need to take
732  * care of it, hence IRQCHIP_ONESHOT_SAFE.
733  */
734 static struct irq_chip dw_edma_emul_irqchip = {
735 	.name		= "dw-edma-emul",
736 	.irq_ack	= dw_edma_emul_irq_ack,
737 	.flags		= IRQCHIP_ONESHOT_SAFE | IRQCHIP_SKIP_SET_WAKE,
738 };
739 
dw_edma_emul_irq_alloc(struct dw_edma * dw)740 static int dw_edma_emul_irq_alloc(struct dw_edma *dw)
741 {
742 	struct dw_edma_chip *chip = dw->chip;
743 	int virq;
744 
745 	chip->db_irq = 0;
746 	chip->db_offset = ~0;
747 
748 	if (chip->flags & DW_EDMA_CHIP_PARTIAL)
749 		return 0;
750 
751 	/*
752 	 * Only meaningful when the core provides the deassert sequence
753 	 * for interrupt emulation.
754 	 */
755 	if (!dw->core->ack_emulated_irq)
756 		return 0;
757 
758 	/*
759 	 * Allocate a single, requestable Linux virtual IRQ number.
760 	 * Use >= 1 so that 0 can remain a "not available" sentinel.
761 	 */
762 	virq = irq_alloc_desc(NUMA_NO_NODE);
763 	if (virq < 0)
764 		return virq;
765 
766 	irq_set_chip_and_handler(virq, &dw_edma_emul_irqchip, handle_level_irq);
767 	irq_set_status_flags(virq, IRQ_LEVEL);
768 	irq_set_chip_data(virq, dw);
769 	irq_set_noprobe(virq);
770 
771 	chip->db_irq = virq;
772 	chip->db_offset = dw_edma_core_db_offset(dw);
773 
774 	return 0;
775 }
776 
dw_edma_emul_irq_free(struct dw_edma * dw)777 static void dw_edma_emul_irq_free(struct dw_edma *dw)
778 {
779 	struct dw_edma_chip *chip = dw->chip;
780 
781 	if (!chip)
782 		return;
783 	if (chip->db_irq <= 0)
784 		return;
785 
786 	irq_free_descs(chip->db_irq, 1);
787 	chip->db_irq = 0;
788 	chip->db_offset = ~0;
789 }
790 
dw_edma_interrupt_emulated(void * data)791 static inline irqreturn_t dw_edma_interrupt_emulated(void *data)
792 {
793 	struct dw_edma_irq *dw_irq = data;
794 	struct dw_edma *dw = dw_irq->dw;
795 	int db_irq = dw->chip->db_irq;
796 
797 	if (db_irq > 0) {
798 		/*
799 		 * Interrupt emulation may assert the IRQ line without updating the
800 		 * normal DONE/ABORT status bits. With a shared IRQ handler we
801 		 * cannot reliably detect such events by status registers alone, so
802 		 * always perform the core-specific deassert sequence.
803 		 */
804 		generic_handle_irq(db_irq);
805 		return IRQ_HANDLED;
806 	}
807 	return IRQ_NONE;
808 }
809 
dw_edma_interrupt_write_inner(int irq,void * data)810 static inline irqreturn_t dw_edma_interrupt_write_inner(int irq, void *data)
811 {
812 	struct dw_edma_irq *dw_irq = data;
813 
814 	return dw_edma_core_handle_int(dw_irq, EDMA_DIR_WRITE,
815 				       dw_edma_done_interrupt_deferred,
816 				       dw_edma_abort_interrupt_deferred);
817 }
818 
dw_edma_interrupt_read_inner(int irq,void * data)819 static inline irqreturn_t dw_edma_interrupt_read_inner(int irq, void *data)
820 {
821 	struct dw_edma_irq *dw_irq = data;
822 
823 	return dw_edma_core_handle_int(dw_irq, EDMA_DIR_READ,
824 				       dw_edma_done_interrupt_deferred,
825 				       dw_edma_abort_interrupt_deferred);
826 }
827 
dw_edma_interrupt_write(int irq,void * data)828 static inline irqreturn_t dw_edma_interrupt_write(int irq, void *data)
829 {
830 	irqreturn_t ret = IRQ_NONE;
831 
832 	ret |= dw_edma_interrupt_write_inner(irq, data);
833 	ret |= dw_edma_interrupt_emulated(data);
834 
835 	return ret;
836 }
837 
dw_edma_interrupt_read(int irq,void * data)838 static inline irqreturn_t dw_edma_interrupt_read(int irq, void *data)
839 {
840 	irqreturn_t ret = IRQ_NONE;
841 
842 	ret |= dw_edma_interrupt_read_inner(irq, data);
843 	ret |= dw_edma_interrupt_emulated(data);
844 
845 	return ret;
846 }
847 
dw_edma_interrupt_common(int irq,void * data)848 static inline irqreturn_t dw_edma_interrupt_common(int irq, void *data)
849 {
850 	irqreturn_t ret = IRQ_NONE;
851 
852 	ret |= dw_edma_interrupt_write_inner(irq, data);
853 	ret |= dw_edma_interrupt_read_inner(irq, data);
854 	ret |= dw_edma_interrupt_emulated(data);
855 
856 	return ret;
857 }
858 
dw_edma_alloc_chan_resources(struct dma_chan * dchan)859 static int dw_edma_alloc_chan_resources(struct dma_chan *dchan)
860 {
861 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
862 
863 	if (chan->status != EDMA_ST_IDLE)
864 		return -EBUSY;
865 
866 	return 0;
867 }
868 
dw_edma_wait_termination(struct dma_chan * dchan)869 static void dw_edma_wait_termination(struct dma_chan *dchan)
870 {
871 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
872 	unsigned long timeout = jiffies + msecs_to_jiffies(5000);
873 	bool stopping;
874 
875 	/*
876 	 * A STOP may be deferred to a later interrupt while the channel is still
877 	 * running. Wait until that handler completes the termination.
878 	 */
879 	while (time_before(jiffies, timeout)) {
880 		scoped_guard(spinlock_irqsave, &chan->vc.lock)
881 			stopping = chan->request == EDMA_REQ_STOP;
882 
883 		if (!stopping)
884 			return;
885 
886 		fsleep(1000);
887 	}
888 
889 	dev_warn(chan->dw->chip->dev,
890 		 "timeout waiting for channel termination\n");
891 }
892 
dw_edma_device_synchronize(struct dma_chan * dchan)893 static void dw_edma_device_synchronize(struct dma_chan *dchan)
894 {
895 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
896 
897 	dw_edma_wait_termination(dchan);
898 	cancel_work_sync(&chan->irq_work);
899 	atomic_set(&chan->irq_pending, 0);
900 	vchan_synchronize(&chan->vc);
901 }
902 
dw_edma_free_chan_resources(struct dma_chan * dchan)903 static void dw_edma_free_chan_resources(struct dma_chan *dchan)
904 {
905 	struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
906 
907 	dw_edma_device_terminate_all(dchan);
908 	dw_edma_device_synchronize(dchan);
909 
910 	scoped_guard(spinlock_irqsave, &chan->vc.lock)
911 		chan->configured = false;
912 
913 	vchan_free_chan_resources(&chan->vc);
914 }
915 
dw_edma_channel_setup(struct dw_edma * dw,u32 wr_alloc,u32 rd_alloc)916 static int dw_edma_channel_setup(struct dw_edma *dw, u32 wr_alloc, u32 rd_alloc)
917 {
918 	struct dw_edma_chip *chip = dw->chip;
919 	struct device *dev = chip->dev;
920 	struct dw_edma_chan *chan;
921 	struct dw_edma_irq *irq;
922 	struct dma_device *dma;
923 	u32 i, ch_cnt;
924 	u32 pos;
925 
926 	ch_cnt = dw->wr_ch_cnt + dw->rd_ch_cnt;
927 	dma = &dw->dma;
928 
929 	INIT_LIST_HEAD(&dma->channels);
930 
931 	for (i = 0; i < ch_cnt; i++) {
932 		chan = &dw->chan[i];
933 
934 		chan->dw = dw;
935 		chan->func_no = chip->func_no;
936 
937 		if (i < dw->wr_ch_cnt) {
938 			chan->id = i;
939 			chan->dir = EDMA_DIR_WRITE;
940 		} else {
941 			chan->id = i - dw->wr_ch_cnt;
942 			chan->dir = EDMA_DIR_READ;
943 		}
944 
945 		chan->configured = false;
946 		chan->request = EDMA_REQ_NONE;
947 		chan->status = EDMA_ST_IDLE;
948 		chan->irq_mode = dw_edma_get_default_irq_mode(chan);
949 		INIT_WORK(&chan->irq_work, dw_edma_irq_work);
950 		atomic_set(&chan->irq_pending, 0);
951 
952 		if (chan->dir == EDMA_DIR_WRITE)
953 			chan->ll_region = chip->ll_region_wr[chan->id];
954 		else
955 			chan->ll_region = chip->ll_region_rd[chan->id];
956 
957 		chan->ll_max = chan->ll_region.sz / EDMA_LL_SZ - 1;
958 
959 		dev_vdbg(dev, "L. List:\tChannel %s[%u] max_cnt=%u\n",
960 			 str_write_read(chan->dir == EDMA_DIR_WRITE),
961 			 chan->id, chan->ll_max);
962 
963 		if (dw->nr_irqs == 1)
964 			pos = 0;
965 		else if (chan->dir == EDMA_DIR_WRITE)
966 			pos = chan->id % wr_alloc;
967 		else
968 			pos = wr_alloc + chan->id % rd_alloc;
969 
970 		irq = &dw->irq[pos];
971 
972 		if (chan->dir == EDMA_DIR_WRITE)
973 			bitmap_set(irq->wr_mask, chan->id, 1);
974 		else
975 			bitmap_set(irq->rd_mask, chan->id, 1);
976 
977 		memcpy(&chan->msi, &irq->msi, sizeof(chan->msi));
978 
979 		dev_vdbg(dev, "MSI:\t\tChannel %s[%u] addr=0x%.8x%.8x, data=0x%.8x\n",
980 			 str_write_read(chan->dir == EDMA_DIR_WRITE),
981 			 chan->id,
982 			 chan->msi.address_hi, chan->msi.address_lo,
983 			 chan->msi.data);
984 
985 		chan->vc.desc_free = vchan_free_desc;
986 		chan->vc.chan.private = chan->dir == EDMA_DIR_WRITE ?
987 					&dw->chip->dt_region_wr[chan->id] :
988 					&dw->chip->dt_region_rd[chan->id];
989 
990 		vchan_init(&chan->vc, dma);
991 
992 		dw_edma_core_ch_config(chan);
993 	}
994 
995 	/* Set DMA channel capabilities */
996 	dma_cap_zero(dma->cap_mask);
997 	dma_cap_set(DMA_SLAVE, dma->cap_mask);
998 	dma_cap_set(DMA_CYCLIC, dma->cap_mask);
999 	dma_cap_set(DMA_PRIVATE, dma->cap_mask);
1000 	dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
1001 	dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1002 	dma->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1003 	dma->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1004 	dma->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
1005 
1006 	/* Set DMA channel callbacks */
1007 	dma->dev = chip->dev;
1008 	dma->device_alloc_chan_resources = dw_edma_alloc_chan_resources;
1009 	dma->device_free_chan_resources = dw_edma_free_chan_resources;
1010 	dma->device_caps = dw_edma_device_caps;
1011 	dma->device_config = dw_edma_device_config;
1012 	dma->device_pause = dw_edma_device_pause;
1013 	dma->device_resume = dw_edma_device_resume;
1014 	dma->device_terminate_all = dw_edma_device_terminate_all;
1015 	dma->device_synchronize = dw_edma_device_synchronize;
1016 	dma->device_issue_pending = dw_edma_device_issue_pending;
1017 	dma->device_tx_status = dw_edma_device_tx_status;
1018 	dma->device_prep_config_sg = dw_edma_device_prep_config_sg;
1019 	dma->device_prep_dma_cyclic = dw_edma_device_prep_dma_cyclic;
1020 	dma->device_prep_interleaved_dma = dw_edma_device_prep_interleaved_dma;
1021 
1022 	dma_set_max_seg_size(dma->dev, U32_MAX);
1023 
1024 	/* Register DMA device */
1025 	return dma_async_device_register(dma);
1026 }
1027 
dw_edma_dec_irq_alloc(int * nr_irqs,u32 * alloc,u16 cnt)1028 static inline void dw_edma_dec_irq_alloc(int *nr_irqs, u32 *alloc, u16 cnt)
1029 {
1030 	if (*nr_irqs && *alloc < cnt) {
1031 		(*alloc)++;
1032 		(*nr_irqs)--;
1033 	}
1034 }
1035 
dw_edma_irq_request(struct dw_edma * dw,u32 * wr_alloc,u32 * rd_alloc)1036 static int dw_edma_irq_request(struct dw_edma *dw,
1037 			       u32 *wr_alloc, u32 *rd_alloc)
1038 {
1039 	struct dw_edma_chip *chip = dw->chip;
1040 	struct device *dev = dw->chip->dev;
1041 	struct msi_desc *msi_desc;
1042 	int i, err = 0;
1043 	u32 ch_cnt;
1044 	int irq;
1045 
1046 	ch_cnt = dw->wr_ch_cnt + dw->rd_ch_cnt;
1047 
1048 	if (chip->nr_irqs < 1 || !chip->ops->irq_vector)
1049 		return -EINVAL;
1050 
1051 	dw->irq = devm_kcalloc(dev, chip->nr_irqs, sizeof(*dw->irq), GFP_KERNEL);
1052 	if (!dw->irq)
1053 		return -ENOMEM;
1054 
1055 	if (chip->nr_irqs == 1) {
1056 		/* Common IRQ shared among all channels */
1057 		irq = chip->ops->irq_vector(dev, 0);
1058 		dw->irq[0].dw = dw;
1059 		err = request_irq(irq, dw_edma_interrupt_common,
1060 				  IRQF_SHARED, dw->name, &dw->irq[0]);
1061 		if (err) {
1062 			dw->nr_irqs = 0;
1063 			return err;
1064 		}
1065 
1066 		if (irq_get_msi_desc(irq))
1067 			get_cached_msi_msg(irq, &dw->irq[0].msi);
1068 
1069 		dw->nr_irqs = 1;
1070 	} else {
1071 		/* Distribute IRQs equally among all channels */
1072 		int tmp = chip->nr_irqs;
1073 
1074 		while (tmp && (*wr_alloc + *rd_alloc) < ch_cnt) {
1075 			dw_edma_dec_irq_alloc(&tmp, wr_alloc, dw->wr_ch_cnt);
1076 			dw_edma_dec_irq_alloc(&tmp, rd_alloc, dw->rd_ch_cnt);
1077 		}
1078 
1079 		for (i = 0; i < (*wr_alloc + *rd_alloc); i++) {
1080 			irq = chip->ops->irq_vector(dev, i);
1081 			dw->irq[i].dw = dw;
1082 			err = request_irq(irq,
1083 					  i < *wr_alloc ?
1084 						dw_edma_interrupt_write :
1085 						dw_edma_interrupt_read,
1086 					  IRQF_SHARED, dw->name,
1087 					  &dw->irq[i]);
1088 			if (err)
1089 				goto err_irq_free;
1090 			msi_desc = irq_get_msi_desc(irq);
1091 			if (msi_desc) {
1092 				get_cached_msi_msg(irq, &dw->irq[i].msi);
1093 				if (!msi_desc->pci.msi_attrib.is_msix)
1094 					dw->irq[i].msi.data = dw->irq[0].msi.data + i;
1095 			}
1096 		}
1097 
1098 		dw->nr_irqs = i;
1099 	}
1100 
1101 	return 0;
1102 
1103 err_irq_free:
1104 	for  (i--; i >= 0; i--) {
1105 		irq = chip->ops->irq_vector(dev, i);
1106 		free_irq(irq, &dw->irq[i]);
1107 	}
1108 
1109 	return err;
1110 }
1111 
dw_edma_check_partial(struct dw_edma_chip * chip,u16 hw_wr_ch_cnt,u16 hw_rd_ch_cnt)1112 static int dw_edma_check_partial(struct dw_edma_chip *chip,
1113 				 u16 hw_wr_ch_cnt, u16 hw_rd_ch_cnt)
1114 {
1115 	if (!(chip->flags & DW_EDMA_CHIP_PARTIAL))
1116 		return 0;
1117 
1118 	if (chip->mf != EDMA_MF_EDMA_UNROLL &&
1119 	    chip->mf != EDMA_MF_HDMA_COMPAT)
1120 		return 0;
1121 
1122 	/*
1123 	 * Direction-wide registers are shared by all channels in that
1124 	 * direction, so a direction must have a single owner.
1125 	 */
1126 	if ((chip->ll_wr_cnt && chip->ll_wr_cnt != hw_wr_ch_cnt) ||
1127 	    (chip->ll_rd_cnt && chip->ll_rd_cnt != hw_rd_ch_cnt))
1128 		return -EOPNOTSUPP;
1129 
1130 	return 0;
1131 }
1132 
dw_edma_probe(struct dw_edma_chip * chip)1133 int dw_edma_probe(struct dw_edma_chip *chip)
1134 {
1135 	struct device *dev;
1136 	struct dw_edma *dw;
1137 	u16 hw_wr_ch_cnt;
1138 	u16 hw_rd_ch_cnt;
1139 	u32 wr_alloc = 0;
1140 	u32 rd_alloc = 0;
1141 	u16 max_wr_cnt;
1142 	u16 max_rd_cnt;
1143 	int i, err;
1144 
1145 	if (!chip)
1146 		return -EINVAL;
1147 
1148 	dev = chip->dev;
1149 	if (!dev || !chip->ops)
1150 		return -EINVAL;
1151 
1152 	if (chip->flags & DW_EDMA_CHIP_PARTIAL) {
1153 		switch (chip->mf) {
1154 		case EDMA_MF_EDMA_UNROLL:
1155 		case EDMA_MF_HDMA_COMPAT:
1156 		case EDMA_MF_HDMA_NATIVE:
1157 			break;
1158 		default:
1159 			return -EOPNOTSUPP;
1160 		}
1161 	}
1162 
1163 	dw = devm_kzalloc(dev, sizeof(*dw), GFP_KERNEL);
1164 	if (!dw)
1165 		return -ENOMEM;
1166 
1167 	dw->chip = chip;
1168 
1169 	if (dw->chip->mf == EDMA_MF_HDMA_NATIVE) {
1170 		dw_hdma_v0_core_register(dw);
1171 		max_wr_cnt = HDMA_MAX_WR_CH;
1172 		max_rd_cnt = HDMA_MAX_RD_CH;
1173 	} else {
1174 		dw_edma_v0_core_register(dw);
1175 		max_wr_cnt = EDMA_MAX_WR_CH;
1176 		max_rd_cnt = EDMA_MAX_RD_CH;
1177 	}
1178 
1179 	raw_spin_lock_init(&dw->lock);
1180 
1181 	/*
1182 	 * chip->ll_*_cnt describes the channels exposed by this instance. Keep
1183 	 * the usable hardware counts separate for partial ownership checks.
1184 	 */
1185 	hw_wr_ch_cnt = min(dw_edma_core_ch_count(dw, EDMA_DIR_WRITE),
1186 			   max_wr_cnt);
1187 	hw_rd_ch_cnt = min(dw_edma_core_ch_count(dw, EDMA_DIR_READ),
1188 			   max_rd_cnt);
1189 
1190 	err = dw_edma_check_partial(chip, hw_wr_ch_cnt, hw_rd_ch_cnt);
1191 	if (err)
1192 		return err;
1193 
1194 	dw->wr_ch_cnt = min(chip->ll_wr_cnt, hw_wr_ch_cnt);
1195 	dw->rd_ch_cnt = min(chip->ll_rd_cnt, hw_rd_ch_cnt);
1196 
1197 	if (!dw->wr_ch_cnt && !dw->rd_ch_cnt)
1198 		return -EINVAL;
1199 
1200 	dev_vdbg(dev, "Channels:\twrite=%d, read=%d\n",
1201 		 dw->wr_ch_cnt, dw->rd_ch_cnt);
1202 
1203 	/* Allocate channels */
1204 	dw->chan = devm_kcalloc(dev, dw->wr_ch_cnt + dw->rd_ch_cnt,
1205 				sizeof(*dw->chan), GFP_KERNEL);
1206 	if (!dw->chan)
1207 		return -ENOMEM;
1208 
1209 	snprintf(dw->name, sizeof(dw->name), "dw-edma-core:%s",
1210 		 dev_name(chip->dev));
1211 
1212 	if (chip->flags & DW_EDMA_CHIP_PARTIAL) {
1213 		/*
1214 		 * Do not reset the shared controller, but drain stale state
1215 		 * from resources represented by this instance.
1216 		 */
1217 		err = dw_edma_core_quiesce(dw);
1218 		if (err)
1219 			return err;
1220 	} else {
1221 		/* Disable eDMA only when this instance owns the controller. */
1222 		dw_edma_core_off(dw);
1223 	}
1224 
1225 	/*
1226 	 * Deferred IRQ works are queued from the hard IRQ handlers, so the
1227 	 * workqueue must exist before any IRQ is requested.
1228 	 */
1229 	dw->wq = alloc_workqueue("dw-edma:%s", WQ_UNBOUND | WQ_HIGHPRI, 0,
1230 				 dev_name(chip->dev));
1231 	if (!dw->wq)
1232 		return -ENOMEM;
1233 
1234 	/* Request IRQs */
1235 	err = dw_edma_irq_request(dw, &wr_alloc, &rd_alloc);
1236 	if (err) {
1237 		destroy_workqueue(dw->wq);
1238 		return err;
1239 	}
1240 
1241 	/* Allocate a dedicated virtual IRQ for interrupt-emulation doorbells */
1242 	err = dw_edma_emul_irq_alloc(dw);
1243 	if (err)
1244 		dev_warn(dev, "Failed to allocate emulation IRQ: %d\n", err);
1245 
1246 	/* Setup write/read channels */
1247 	err = dw_edma_channel_setup(dw, wr_alloc, rd_alloc);
1248 	if (err)
1249 		goto err_irq_free;
1250 
1251 	/* Turn debugfs on */
1252 	dw_edma_core_debugfs_on(dw);
1253 
1254 	chip->dw = dw;
1255 
1256 	return 0;
1257 
1258 err_irq_free:
1259 	for (i = (dw->nr_irqs - 1); i >= 0; i--)
1260 		free_irq(chip->ops->irq_vector(dev, i), &dw->irq[i]);
1261 	dw_edma_emul_irq_free(dw);
1262 	destroy_workqueue(dw->wq);
1263 
1264 	return err;
1265 }
1266 EXPORT_SYMBOL_GPL(dw_edma_probe);
1267 
dw_edma_remove(struct dw_edma_chip * chip)1268 int dw_edma_remove(struct dw_edma_chip *chip)
1269 {
1270 	struct dw_edma_chan *chan, *_chan;
1271 	struct device *dev = chip->dev;
1272 	struct dw_edma *dw = chip->dw;
1273 	int i, err = 0;
1274 
1275 	/* Skip removal if no private data found */
1276 	if (!dw)
1277 		return -ENODEV;
1278 
1279 	if (chip->flags & DW_EDMA_CHIP_PARTIAL)
1280 		err = dw_edma_core_quiesce(dw);
1281 	else
1282 		dw_edma_core_off(dw);
1283 
1284 	/* Free irqs */
1285 	for (i = (dw->nr_irqs - 1); i >= 0; i--)
1286 		free_irq(chip->ops->irq_vector(dev, i), &dw->irq[i]);
1287 	dw_edma_emul_irq_free(dw);
1288 
1289 	for (i = 0; i < dw->wr_ch_cnt + dw->rd_ch_cnt; i++)
1290 		cancel_work_sync(&dw->chan[i].irq_work);
1291 
1292 	destroy_workqueue(dw->wq);
1293 
1294 	/* Deregister eDMA device */
1295 	dma_async_device_unregister(&dw->dma);
1296 	list_for_each_entry_safe(chan, _chan, &dw->dma.channels,
1297 				 vc.chan.device_node) {
1298 		tasklet_kill(&chan->vc.task);
1299 		list_del(&chan->vc.chan.device_node);
1300 	}
1301 
1302 	return err;
1303 }
1304 EXPORT_SYMBOL_GPL(dw_edma_remove);
1305 
1306 MODULE_LICENSE("GPL v2");
1307 MODULE_DESCRIPTION("Synopsys DesignWare eDMA controller core driver");
1308 MODULE_AUTHOR("Gustavo Pimentel <gustavo.pimentel@synopsys.com>");
1309