xref: /linux/drivers/dma/ti/k3-udma.c (revision 132db93572821ec2fdf81e354cc40f558faf7e4f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com
4  *  Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/delay.h>
9 #include <linux/dmaengine.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmapool.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/of.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_device.h>
22 #include <linux/of_irq.h>
23 #include <linux/workqueue.h>
24 #include <linux/completion.h>
25 #include <linux/soc/ti/k3-ringacc.h>
26 #include <linux/soc/ti/ti_sci_protocol.h>
27 #include <linux/soc/ti/ti_sci_inta_msi.h>
28 #include <linux/dma/ti-cppi5.h>
29 
30 #include "../virt-dma.h"
31 #include "k3-udma.h"
32 #include "k3-psil-priv.h"
33 
34 struct udma_static_tr {
35 	u8 elsize; /* RPSTR0 */
36 	u16 elcnt; /* RPSTR0 */
37 	u16 bstcnt; /* RPSTR1 */
38 };
39 
40 #define K3_UDMA_MAX_RFLOWS		1024
41 #define K3_UDMA_DEFAULT_RING_SIZE	16
42 
43 /* How SRC/DST tag should be updated by UDMA in the descriptor's Word 3 */
44 #define UDMA_RFLOW_SRCTAG_NONE		0
45 #define UDMA_RFLOW_SRCTAG_CFG_TAG	1
46 #define UDMA_RFLOW_SRCTAG_FLOW_ID	2
47 #define UDMA_RFLOW_SRCTAG_SRC_TAG	4
48 
49 #define UDMA_RFLOW_DSTTAG_NONE		0
50 #define UDMA_RFLOW_DSTTAG_CFG_TAG	1
51 #define UDMA_RFLOW_DSTTAG_FLOW_ID	2
52 #define UDMA_RFLOW_DSTTAG_DST_TAG_LO	4
53 #define UDMA_RFLOW_DSTTAG_DST_TAG_HI	5
54 
55 struct udma_chan;
56 
57 enum udma_mmr {
58 	MMR_GCFG = 0,
59 	MMR_RCHANRT,
60 	MMR_TCHANRT,
61 	MMR_LAST,
62 };
63 
64 static const char * const mmr_names[] = { "gcfg", "rchanrt", "tchanrt" };
65 
66 struct udma_tchan {
67 	void __iomem *reg_rt;
68 
69 	int id;
70 	struct k3_ring *t_ring; /* Transmit ring */
71 	struct k3_ring *tc_ring; /* Transmit Completion ring */
72 };
73 
74 struct udma_rflow {
75 	int id;
76 	struct k3_ring *fd_ring; /* Free Descriptor ring */
77 	struct k3_ring *r_ring; /* Receive ring */
78 };
79 
80 struct udma_rchan {
81 	void __iomem *reg_rt;
82 
83 	int id;
84 };
85 
86 #define UDMA_FLAG_PDMA_ACC32		BIT(0)
87 #define UDMA_FLAG_PDMA_BURST		BIT(1)
88 
89 struct udma_match_data {
90 	u32 psil_base;
91 	bool enable_memcpy_support;
92 	u32 flags;
93 	u32 statictr_z_mask;
94 	u32 rchan_oes_offset;
95 
96 	u8 tpl_levels;
97 	u32 level_start_idx[];
98 };
99 
100 struct udma_hwdesc {
101 	size_t cppi5_desc_size;
102 	void *cppi5_desc_vaddr;
103 	dma_addr_t cppi5_desc_paddr;
104 
105 	/* TR descriptor internal pointers */
106 	void *tr_req_base;
107 	struct cppi5_tr_resp_t *tr_resp_base;
108 };
109 
110 struct udma_rx_flush {
111 	struct udma_hwdesc hwdescs[2];
112 
113 	size_t buffer_size;
114 	void *buffer_vaddr;
115 	dma_addr_t buffer_paddr;
116 };
117 
118 struct udma_dev {
119 	struct dma_device ddev;
120 	struct device *dev;
121 	void __iomem *mmrs[MMR_LAST];
122 	const struct udma_match_data *match_data;
123 
124 	size_t desc_align; /* alignment to use for descriptors */
125 
126 	struct udma_tisci_rm tisci_rm;
127 
128 	struct k3_ringacc *ringacc;
129 
130 	struct work_struct purge_work;
131 	struct list_head desc_to_purge;
132 	spinlock_t lock;
133 
134 	struct udma_rx_flush rx_flush;
135 
136 	int tchan_cnt;
137 	int echan_cnt;
138 	int rchan_cnt;
139 	int rflow_cnt;
140 	unsigned long *tchan_map;
141 	unsigned long *rchan_map;
142 	unsigned long *rflow_gp_map;
143 	unsigned long *rflow_gp_map_allocated;
144 	unsigned long *rflow_in_use;
145 
146 	struct udma_tchan *tchans;
147 	struct udma_rchan *rchans;
148 	struct udma_rflow *rflows;
149 
150 	struct udma_chan *channels;
151 	u32 psil_base;
152 	u32 atype;
153 };
154 
155 struct udma_desc {
156 	struct virt_dma_desc vd;
157 
158 	bool terminated;
159 
160 	enum dma_transfer_direction dir;
161 
162 	struct udma_static_tr static_tr;
163 	u32 residue;
164 
165 	unsigned int sglen;
166 	unsigned int desc_idx; /* Only used for cyclic in packet mode */
167 	unsigned int tr_idx;
168 
169 	u32 metadata_size;
170 	void *metadata; /* pointer to provided metadata buffer (EPIP, PSdata) */
171 
172 	unsigned int hwdesc_count;
173 	struct udma_hwdesc hwdesc[];
174 };
175 
176 enum udma_chan_state {
177 	UDMA_CHAN_IS_IDLE = 0, /* not active, no teardown is in progress */
178 	UDMA_CHAN_IS_ACTIVE, /* Normal operation */
179 	UDMA_CHAN_IS_TERMINATING, /* channel is being terminated */
180 };
181 
182 struct udma_tx_drain {
183 	struct delayed_work work;
184 	ktime_t tstamp;
185 	u32 residue;
186 };
187 
188 struct udma_chan_config {
189 	bool pkt_mode; /* TR or packet */
190 	bool needs_epib; /* EPIB is needed for the communication or not */
191 	u32 psd_size; /* size of Protocol Specific Data */
192 	u32 metadata_size; /* (needs_epib ? 16:0) + psd_size */
193 	u32 hdesc_size; /* Size of a packet descriptor in packet mode */
194 	bool notdpkt; /* Suppress sending TDC packet */
195 	int remote_thread_id;
196 	u32 atype;
197 	u32 src_thread;
198 	u32 dst_thread;
199 	enum psil_endpoint_type ep_type;
200 	bool enable_acc32;
201 	bool enable_burst;
202 	enum udma_tp_level channel_tpl; /* Channel Throughput Level */
203 
204 	enum dma_transfer_direction dir;
205 };
206 
207 struct udma_chan {
208 	struct virt_dma_chan vc;
209 	struct dma_slave_config	cfg;
210 	struct udma_dev *ud;
211 	struct udma_desc *desc;
212 	struct udma_desc *terminated_desc;
213 	struct udma_static_tr static_tr;
214 	char *name;
215 
216 	struct udma_tchan *tchan;
217 	struct udma_rchan *rchan;
218 	struct udma_rflow *rflow;
219 
220 	bool psil_paired;
221 
222 	int irq_num_ring;
223 	int irq_num_udma;
224 
225 	bool cyclic;
226 	bool paused;
227 
228 	enum udma_chan_state state;
229 	struct completion teardown_completed;
230 
231 	struct udma_tx_drain tx_drain;
232 
233 	u32 bcnt; /* number of bytes completed since the start of the channel */
234 
235 	/* Channel configuration parameters */
236 	struct udma_chan_config config;
237 
238 	/* dmapool for packet mode descriptors */
239 	bool use_dma_pool;
240 	struct dma_pool *hdesc_pool;
241 
242 	u32 id;
243 };
244 
245 static inline struct udma_dev *to_udma_dev(struct dma_device *d)
246 {
247 	return container_of(d, struct udma_dev, ddev);
248 }
249 
250 static inline struct udma_chan *to_udma_chan(struct dma_chan *c)
251 {
252 	return container_of(c, struct udma_chan, vc.chan);
253 }
254 
255 static inline struct udma_desc *to_udma_desc(struct dma_async_tx_descriptor *t)
256 {
257 	return container_of(t, struct udma_desc, vd.tx);
258 }
259 
260 /* Generic register access functions */
261 static inline u32 udma_read(void __iomem *base, int reg)
262 {
263 	return readl(base + reg);
264 }
265 
266 static inline void udma_write(void __iomem *base, int reg, u32 val)
267 {
268 	writel(val, base + reg);
269 }
270 
271 static inline void udma_update_bits(void __iomem *base, int reg,
272 				    u32 mask, u32 val)
273 {
274 	u32 tmp, orig;
275 
276 	orig = readl(base + reg);
277 	tmp = orig & ~mask;
278 	tmp |= (val & mask);
279 
280 	if (tmp != orig)
281 		writel(tmp, base + reg);
282 }
283 
284 /* TCHANRT */
285 static inline u32 udma_tchanrt_read(struct udma_tchan *tchan, int reg)
286 {
287 	if (!tchan)
288 		return 0;
289 	return udma_read(tchan->reg_rt, reg);
290 }
291 
292 static inline void udma_tchanrt_write(struct udma_tchan *tchan, int reg,
293 				      u32 val)
294 {
295 	if (!tchan)
296 		return;
297 	udma_write(tchan->reg_rt, reg, val);
298 }
299 
300 static inline void udma_tchanrt_update_bits(struct udma_tchan *tchan, int reg,
301 					    u32 mask, u32 val)
302 {
303 	if (!tchan)
304 		return;
305 	udma_update_bits(tchan->reg_rt, reg, mask, val);
306 }
307 
308 /* RCHANRT */
309 static inline u32 udma_rchanrt_read(struct udma_rchan *rchan, int reg)
310 {
311 	if (!rchan)
312 		return 0;
313 	return udma_read(rchan->reg_rt, reg);
314 }
315 
316 static inline void udma_rchanrt_write(struct udma_rchan *rchan, int reg,
317 				      u32 val)
318 {
319 	if (!rchan)
320 		return;
321 	udma_write(rchan->reg_rt, reg, val);
322 }
323 
324 static inline void udma_rchanrt_update_bits(struct udma_rchan *rchan, int reg,
325 					    u32 mask, u32 val)
326 {
327 	if (!rchan)
328 		return;
329 	udma_update_bits(rchan->reg_rt, reg, mask, val);
330 }
331 
332 static int navss_psil_pair(struct udma_dev *ud, u32 src_thread, u32 dst_thread)
333 {
334 	struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
335 
336 	dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
337 	return tisci_rm->tisci_psil_ops->pair(tisci_rm->tisci,
338 					      tisci_rm->tisci_navss_dev_id,
339 					      src_thread, dst_thread);
340 }
341 
342 static int navss_psil_unpair(struct udma_dev *ud, u32 src_thread,
343 			     u32 dst_thread)
344 {
345 	struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
346 
347 	dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
348 	return tisci_rm->tisci_psil_ops->unpair(tisci_rm->tisci,
349 						tisci_rm->tisci_navss_dev_id,
350 						src_thread, dst_thread);
351 }
352 
353 static void udma_reset_uchan(struct udma_chan *uc)
354 {
355 	memset(&uc->config, 0, sizeof(uc->config));
356 	uc->config.remote_thread_id = -1;
357 	uc->state = UDMA_CHAN_IS_IDLE;
358 }
359 
360 static void udma_dump_chan_stdata(struct udma_chan *uc)
361 {
362 	struct device *dev = uc->ud->dev;
363 	u32 offset;
364 	int i;
365 
366 	if (uc->config.dir == DMA_MEM_TO_DEV || uc->config.dir == DMA_MEM_TO_MEM) {
367 		dev_dbg(dev, "TCHAN State data:\n");
368 		for (i = 0; i < 32; i++) {
369 			offset = UDMA_TCHAN_RT_STDATA_REG + i * 4;
370 			dev_dbg(dev, "TRT_STDATA[%02d]: 0x%08x\n", i,
371 				udma_tchanrt_read(uc->tchan, offset));
372 		}
373 	}
374 
375 	if (uc->config.dir == DMA_DEV_TO_MEM || uc->config.dir == DMA_MEM_TO_MEM) {
376 		dev_dbg(dev, "RCHAN State data:\n");
377 		for (i = 0; i < 32; i++) {
378 			offset = UDMA_RCHAN_RT_STDATA_REG + i * 4;
379 			dev_dbg(dev, "RRT_STDATA[%02d]: 0x%08x\n", i,
380 				udma_rchanrt_read(uc->rchan, offset));
381 		}
382 	}
383 }
384 
385 static inline dma_addr_t udma_curr_cppi5_desc_paddr(struct udma_desc *d,
386 						    int idx)
387 {
388 	return d->hwdesc[idx].cppi5_desc_paddr;
389 }
390 
391 static inline void *udma_curr_cppi5_desc_vaddr(struct udma_desc *d, int idx)
392 {
393 	return d->hwdesc[idx].cppi5_desc_vaddr;
394 }
395 
396 static struct udma_desc *udma_udma_desc_from_paddr(struct udma_chan *uc,
397 						   dma_addr_t paddr)
398 {
399 	struct udma_desc *d = uc->terminated_desc;
400 
401 	if (d) {
402 		dma_addr_t desc_paddr = udma_curr_cppi5_desc_paddr(d,
403 								   d->desc_idx);
404 
405 		if (desc_paddr != paddr)
406 			d = NULL;
407 	}
408 
409 	if (!d) {
410 		d = uc->desc;
411 		if (d) {
412 			dma_addr_t desc_paddr = udma_curr_cppi5_desc_paddr(d,
413 								d->desc_idx);
414 
415 			if (desc_paddr != paddr)
416 				d = NULL;
417 		}
418 	}
419 
420 	return d;
421 }
422 
423 static void udma_free_hwdesc(struct udma_chan *uc, struct udma_desc *d)
424 {
425 	if (uc->use_dma_pool) {
426 		int i;
427 
428 		for (i = 0; i < d->hwdesc_count; i++) {
429 			if (!d->hwdesc[i].cppi5_desc_vaddr)
430 				continue;
431 
432 			dma_pool_free(uc->hdesc_pool,
433 				      d->hwdesc[i].cppi5_desc_vaddr,
434 				      d->hwdesc[i].cppi5_desc_paddr);
435 
436 			d->hwdesc[i].cppi5_desc_vaddr = NULL;
437 		}
438 	} else if (d->hwdesc[0].cppi5_desc_vaddr) {
439 		struct udma_dev *ud = uc->ud;
440 
441 		dma_free_coherent(ud->dev, d->hwdesc[0].cppi5_desc_size,
442 				  d->hwdesc[0].cppi5_desc_vaddr,
443 				  d->hwdesc[0].cppi5_desc_paddr);
444 
445 		d->hwdesc[0].cppi5_desc_vaddr = NULL;
446 	}
447 }
448 
449 static void udma_purge_desc_work(struct work_struct *work)
450 {
451 	struct udma_dev *ud = container_of(work, typeof(*ud), purge_work);
452 	struct virt_dma_desc *vd, *_vd;
453 	unsigned long flags;
454 	LIST_HEAD(head);
455 
456 	spin_lock_irqsave(&ud->lock, flags);
457 	list_splice_tail_init(&ud->desc_to_purge, &head);
458 	spin_unlock_irqrestore(&ud->lock, flags);
459 
460 	list_for_each_entry_safe(vd, _vd, &head, node) {
461 		struct udma_chan *uc = to_udma_chan(vd->tx.chan);
462 		struct udma_desc *d = to_udma_desc(&vd->tx);
463 
464 		udma_free_hwdesc(uc, d);
465 		list_del(&vd->node);
466 		kfree(d);
467 	}
468 
469 	/* If more to purge, schedule the work again */
470 	if (!list_empty(&ud->desc_to_purge))
471 		schedule_work(&ud->purge_work);
472 }
473 
474 static void udma_desc_free(struct virt_dma_desc *vd)
475 {
476 	struct udma_dev *ud = to_udma_dev(vd->tx.chan->device);
477 	struct udma_chan *uc = to_udma_chan(vd->tx.chan);
478 	struct udma_desc *d = to_udma_desc(&vd->tx);
479 	unsigned long flags;
480 
481 	if (uc->terminated_desc == d)
482 		uc->terminated_desc = NULL;
483 
484 	if (uc->use_dma_pool) {
485 		udma_free_hwdesc(uc, d);
486 		kfree(d);
487 		return;
488 	}
489 
490 	spin_lock_irqsave(&ud->lock, flags);
491 	list_add_tail(&vd->node, &ud->desc_to_purge);
492 	spin_unlock_irqrestore(&ud->lock, flags);
493 
494 	schedule_work(&ud->purge_work);
495 }
496 
497 static bool udma_is_chan_running(struct udma_chan *uc)
498 {
499 	u32 trt_ctl = 0;
500 	u32 rrt_ctl = 0;
501 
502 	if (uc->tchan)
503 		trt_ctl = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_CTL_REG);
504 	if (uc->rchan)
505 		rrt_ctl = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_CTL_REG);
506 
507 	if (trt_ctl & UDMA_CHAN_RT_CTL_EN || rrt_ctl & UDMA_CHAN_RT_CTL_EN)
508 		return true;
509 
510 	return false;
511 }
512 
513 static bool udma_is_chan_paused(struct udma_chan *uc)
514 {
515 	u32 val, pause_mask;
516 
517 	switch (uc->config.dir) {
518 	case DMA_DEV_TO_MEM:
519 		val = udma_rchanrt_read(uc->rchan,
520 					UDMA_RCHAN_RT_PEER_RT_EN_REG);
521 		pause_mask = UDMA_PEER_RT_EN_PAUSE;
522 		break;
523 	case DMA_MEM_TO_DEV:
524 		val = udma_tchanrt_read(uc->tchan,
525 					UDMA_TCHAN_RT_PEER_RT_EN_REG);
526 		pause_mask = UDMA_PEER_RT_EN_PAUSE;
527 		break;
528 	case DMA_MEM_TO_MEM:
529 		val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_CTL_REG);
530 		pause_mask = UDMA_CHAN_RT_CTL_PAUSE;
531 		break;
532 	default:
533 		return false;
534 	}
535 
536 	if (val & pause_mask)
537 		return true;
538 
539 	return false;
540 }
541 
542 static void udma_sync_for_device(struct udma_chan *uc, int idx)
543 {
544 	struct udma_desc *d = uc->desc;
545 
546 	if (uc->cyclic && uc->config.pkt_mode) {
547 		dma_sync_single_for_device(uc->ud->dev,
548 					   d->hwdesc[idx].cppi5_desc_paddr,
549 					   d->hwdesc[idx].cppi5_desc_size,
550 					   DMA_TO_DEVICE);
551 	} else {
552 		int i;
553 
554 		for (i = 0; i < d->hwdesc_count; i++) {
555 			if (!d->hwdesc[i].cppi5_desc_vaddr)
556 				continue;
557 
558 			dma_sync_single_for_device(uc->ud->dev,
559 						d->hwdesc[i].cppi5_desc_paddr,
560 						d->hwdesc[i].cppi5_desc_size,
561 						DMA_TO_DEVICE);
562 		}
563 	}
564 }
565 
566 static inline dma_addr_t udma_get_rx_flush_hwdesc_paddr(struct udma_chan *uc)
567 {
568 	return uc->ud->rx_flush.hwdescs[uc->config.pkt_mode].cppi5_desc_paddr;
569 }
570 
571 static int udma_push_to_ring(struct udma_chan *uc, int idx)
572 {
573 	struct udma_desc *d = uc->desc;
574 	struct k3_ring *ring = NULL;
575 	dma_addr_t paddr;
576 
577 	switch (uc->config.dir) {
578 	case DMA_DEV_TO_MEM:
579 		ring = uc->rflow->fd_ring;
580 		break;
581 	case DMA_MEM_TO_DEV:
582 	case DMA_MEM_TO_MEM:
583 		ring = uc->tchan->t_ring;
584 		break;
585 	default:
586 		return -EINVAL;
587 	}
588 
589 	/* RX flush packet: idx == -1 is only passed in case of DEV_TO_MEM */
590 	if (idx == -1) {
591 		paddr = udma_get_rx_flush_hwdesc_paddr(uc);
592 	} else {
593 		paddr = udma_curr_cppi5_desc_paddr(d, idx);
594 
595 		wmb(); /* Ensure that writes are not moved over this point */
596 		udma_sync_for_device(uc, idx);
597 	}
598 
599 	return k3_ringacc_ring_push(ring, &paddr);
600 }
601 
602 static bool udma_desc_is_rx_flush(struct udma_chan *uc, dma_addr_t addr)
603 {
604 	if (uc->config.dir != DMA_DEV_TO_MEM)
605 		return false;
606 
607 	if (addr == udma_get_rx_flush_hwdesc_paddr(uc))
608 		return true;
609 
610 	return false;
611 }
612 
613 static int udma_pop_from_ring(struct udma_chan *uc, dma_addr_t *addr)
614 {
615 	struct k3_ring *ring = NULL;
616 	int ret = -ENOENT;
617 
618 	switch (uc->config.dir) {
619 	case DMA_DEV_TO_MEM:
620 		ring = uc->rflow->r_ring;
621 		break;
622 	case DMA_MEM_TO_DEV:
623 	case DMA_MEM_TO_MEM:
624 		ring = uc->tchan->tc_ring;
625 		break;
626 	default:
627 		break;
628 	}
629 
630 	if (ring && k3_ringacc_ring_get_occ(ring)) {
631 		struct udma_desc *d = NULL;
632 
633 		ret = k3_ringacc_ring_pop(ring, addr);
634 		if (ret)
635 			return ret;
636 
637 		/* Teardown completion */
638 		if (cppi5_desc_is_tdcm(*addr))
639 			return ret;
640 
641 		/* Check for flush descriptor */
642 		if (udma_desc_is_rx_flush(uc, *addr))
643 			return -ENOENT;
644 
645 		d = udma_udma_desc_from_paddr(uc, *addr);
646 
647 		if (d)
648 			dma_sync_single_for_cpu(uc->ud->dev, *addr,
649 						d->hwdesc[0].cppi5_desc_size,
650 						DMA_FROM_DEVICE);
651 		rmb(); /* Ensure that reads are not moved before this point */
652 	}
653 
654 	return ret;
655 }
656 
657 static void udma_reset_rings(struct udma_chan *uc)
658 {
659 	struct k3_ring *ring1 = NULL;
660 	struct k3_ring *ring2 = NULL;
661 
662 	switch (uc->config.dir) {
663 	case DMA_DEV_TO_MEM:
664 		if (uc->rchan) {
665 			ring1 = uc->rflow->fd_ring;
666 			ring2 = uc->rflow->r_ring;
667 		}
668 		break;
669 	case DMA_MEM_TO_DEV:
670 	case DMA_MEM_TO_MEM:
671 		if (uc->tchan) {
672 			ring1 = uc->tchan->t_ring;
673 			ring2 = uc->tchan->tc_ring;
674 		}
675 		break;
676 	default:
677 		break;
678 	}
679 
680 	if (ring1)
681 		k3_ringacc_ring_reset_dma(ring1,
682 					  k3_ringacc_ring_get_occ(ring1));
683 	if (ring2)
684 		k3_ringacc_ring_reset(ring2);
685 
686 	/* make sure we are not leaking memory by stalled descriptor */
687 	if (uc->terminated_desc) {
688 		udma_desc_free(&uc->terminated_desc->vd);
689 		uc->terminated_desc = NULL;
690 	}
691 }
692 
693 static void udma_reset_counters(struct udma_chan *uc)
694 {
695 	u32 val;
696 
697 	if (uc->tchan) {
698 		val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_BCNT_REG);
699 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_BCNT_REG, val);
700 
701 		val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_SBCNT_REG);
702 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_SBCNT_REG, val);
703 
704 		val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_PCNT_REG);
705 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PCNT_REG, val);
706 
707 		val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_PEER_BCNT_REG);
708 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_BCNT_REG, val);
709 	}
710 
711 	if (uc->rchan) {
712 		val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_BCNT_REG);
713 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_BCNT_REG, val);
714 
715 		val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_SBCNT_REG);
716 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_SBCNT_REG, val);
717 
718 		val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_PCNT_REG);
719 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PCNT_REG, val);
720 
721 		val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_PEER_BCNT_REG);
722 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_BCNT_REG, val);
723 	}
724 
725 	uc->bcnt = 0;
726 }
727 
728 static int udma_reset_chan(struct udma_chan *uc, bool hard)
729 {
730 	switch (uc->config.dir) {
731 	case DMA_DEV_TO_MEM:
732 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG, 0);
733 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG, 0);
734 		break;
735 	case DMA_MEM_TO_DEV:
736 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG, 0);
737 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_RT_EN_REG, 0);
738 		break;
739 	case DMA_MEM_TO_MEM:
740 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG, 0);
741 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG, 0);
742 		break;
743 	default:
744 		return -EINVAL;
745 	}
746 
747 	/* Reset all counters */
748 	udma_reset_counters(uc);
749 
750 	/* Hard reset: re-initialize the channel to reset */
751 	if (hard) {
752 		struct udma_chan_config ucc_backup;
753 		int ret;
754 
755 		memcpy(&ucc_backup, &uc->config, sizeof(uc->config));
756 		uc->ud->ddev.device_free_chan_resources(&uc->vc.chan);
757 
758 		/* restore the channel configuration */
759 		memcpy(&uc->config, &ucc_backup, sizeof(uc->config));
760 		ret = uc->ud->ddev.device_alloc_chan_resources(&uc->vc.chan);
761 		if (ret)
762 			return ret;
763 
764 		/*
765 		 * Setting forced teardown after forced reset helps recovering
766 		 * the rchan.
767 		 */
768 		if (uc->config.dir == DMA_DEV_TO_MEM)
769 			udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG,
770 					   UDMA_CHAN_RT_CTL_EN |
771 					   UDMA_CHAN_RT_CTL_TDOWN |
772 					   UDMA_CHAN_RT_CTL_FTDOWN);
773 	}
774 	uc->state = UDMA_CHAN_IS_IDLE;
775 
776 	return 0;
777 }
778 
779 static void udma_start_desc(struct udma_chan *uc)
780 {
781 	struct udma_chan_config *ucc = &uc->config;
782 
783 	if (ucc->pkt_mode && (uc->cyclic || ucc->dir == DMA_DEV_TO_MEM)) {
784 		int i;
785 
786 		/* Push all descriptors to ring for packet mode cyclic or RX */
787 		for (i = 0; i < uc->desc->sglen; i++)
788 			udma_push_to_ring(uc, i);
789 	} else {
790 		udma_push_to_ring(uc, 0);
791 	}
792 }
793 
794 static bool udma_chan_needs_reconfiguration(struct udma_chan *uc)
795 {
796 	/* Only PDMAs have staticTR */
797 	if (uc->config.ep_type == PSIL_EP_NATIVE)
798 		return false;
799 
800 	/* Check if the staticTR configuration has changed for TX */
801 	if (memcmp(&uc->static_tr, &uc->desc->static_tr, sizeof(uc->static_tr)))
802 		return true;
803 
804 	return false;
805 }
806 
807 static int udma_start(struct udma_chan *uc)
808 {
809 	struct virt_dma_desc *vd = vchan_next_desc(&uc->vc);
810 
811 	if (!vd) {
812 		uc->desc = NULL;
813 		return -ENOENT;
814 	}
815 
816 	list_del(&vd->node);
817 
818 	uc->desc = to_udma_desc(&vd->tx);
819 
820 	/* Channel is already running and does not need reconfiguration */
821 	if (udma_is_chan_running(uc) && !udma_chan_needs_reconfiguration(uc)) {
822 		udma_start_desc(uc);
823 		goto out;
824 	}
825 
826 	/* Make sure that we clear the teardown bit, if it is set */
827 	udma_reset_chan(uc, false);
828 
829 	/* Push descriptors before we start the channel */
830 	udma_start_desc(uc);
831 
832 	switch (uc->desc->dir) {
833 	case DMA_DEV_TO_MEM:
834 		/* Config remote TR */
835 		if (uc->config.ep_type == PSIL_EP_PDMA_XY) {
836 			u32 val = PDMA_STATIC_TR_Y(uc->desc->static_tr.elcnt) |
837 				  PDMA_STATIC_TR_X(uc->desc->static_tr.elsize);
838 			const struct udma_match_data *match_data =
839 							uc->ud->match_data;
840 
841 			if (uc->config.enable_acc32)
842 				val |= PDMA_STATIC_TR_XY_ACC32;
843 			if (uc->config.enable_burst)
844 				val |= PDMA_STATIC_TR_XY_BURST;
845 
846 			udma_rchanrt_write(uc->rchan,
847 				UDMA_RCHAN_RT_PEER_STATIC_TR_XY_REG, val);
848 
849 			udma_rchanrt_write(uc->rchan,
850 				UDMA_RCHAN_RT_PEER_STATIC_TR_Z_REG,
851 				PDMA_STATIC_TR_Z(uc->desc->static_tr.bstcnt,
852 						 match_data->statictr_z_mask));
853 
854 			/* save the current staticTR configuration */
855 			memcpy(&uc->static_tr, &uc->desc->static_tr,
856 			       sizeof(uc->static_tr));
857 		}
858 
859 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG,
860 				   UDMA_CHAN_RT_CTL_EN);
861 
862 		/* Enable remote */
863 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG,
864 				   UDMA_PEER_RT_EN_ENABLE);
865 
866 		break;
867 	case DMA_MEM_TO_DEV:
868 		/* Config remote TR */
869 		if (uc->config.ep_type == PSIL_EP_PDMA_XY) {
870 			u32 val = PDMA_STATIC_TR_Y(uc->desc->static_tr.elcnt) |
871 				  PDMA_STATIC_TR_X(uc->desc->static_tr.elsize);
872 
873 			if (uc->config.enable_acc32)
874 				val |= PDMA_STATIC_TR_XY_ACC32;
875 			if (uc->config.enable_burst)
876 				val |= PDMA_STATIC_TR_XY_BURST;
877 
878 			udma_tchanrt_write(uc->tchan,
879 				UDMA_TCHAN_RT_PEER_STATIC_TR_XY_REG, val);
880 
881 			/* save the current staticTR configuration */
882 			memcpy(&uc->static_tr, &uc->desc->static_tr,
883 			       sizeof(uc->static_tr));
884 		}
885 
886 		/* Enable remote */
887 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_RT_EN_REG,
888 				   UDMA_PEER_RT_EN_ENABLE);
889 
890 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
891 				   UDMA_CHAN_RT_CTL_EN);
892 
893 		break;
894 	case DMA_MEM_TO_MEM:
895 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG,
896 				   UDMA_CHAN_RT_CTL_EN);
897 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
898 				   UDMA_CHAN_RT_CTL_EN);
899 
900 		break;
901 	default:
902 		return -EINVAL;
903 	}
904 
905 	uc->state = UDMA_CHAN_IS_ACTIVE;
906 out:
907 
908 	return 0;
909 }
910 
911 static int udma_stop(struct udma_chan *uc)
912 {
913 	enum udma_chan_state old_state = uc->state;
914 
915 	uc->state = UDMA_CHAN_IS_TERMINATING;
916 	reinit_completion(&uc->teardown_completed);
917 
918 	switch (uc->config.dir) {
919 	case DMA_DEV_TO_MEM:
920 		if (!uc->cyclic && !uc->desc)
921 			udma_push_to_ring(uc, -1);
922 
923 		udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG,
924 				   UDMA_PEER_RT_EN_ENABLE |
925 				   UDMA_PEER_RT_EN_TEARDOWN);
926 		break;
927 	case DMA_MEM_TO_DEV:
928 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_RT_EN_REG,
929 				   UDMA_PEER_RT_EN_ENABLE |
930 				   UDMA_PEER_RT_EN_FLUSH);
931 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
932 				   UDMA_CHAN_RT_CTL_EN |
933 				   UDMA_CHAN_RT_CTL_TDOWN);
934 		break;
935 	case DMA_MEM_TO_MEM:
936 		udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
937 				   UDMA_CHAN_RT_CTL_EN |
938 				   UDMA_CHAN_RT_CTL_TDOWN);
939 		break;
940 	default:
941 		uc->state = old_state;
942 		complete_all(&uc->teardown_completed);
943 		return -EINVAL;
944 	}
945 
946 	return 0;
947 }
948 
949 static void udma_cyclic_packet_elapsed(struct udma_chan *uc)
950 {
951 	struct udma_desc *d = uc->desc;
952 	struct cppi5_host_desc_t *h_desc;
953 
954 	h_desc = d->hwdesc[d->desc_idx].cppi5_desc_vaddr;
955 	cppi5_hdesc_reset_to_original(h_desc);
956 	udma_push_to_ring(uc, d->desc_idx);
957 	d->desc_idx = (d->desc_idx + 1) % d->sglen;
958 }
959 
960 static inline void udma_fetch_epib(struct udma_chan *uc, struct udma_desc *d)
961 {
962 	struct cppi5_host_desc_t *h_desc = d->hwdesc[0].cppi5_desc_vaddr;
963 
964 	memcpy(d->metadata, h_desc->epib, d->metadata_size);
965 }
966 
967 static bool udma_is_desc_really_done(struct udma_chan *uc, struct udma_desc *d)
968 {
969 	u32 peer_bcnt, bcnt;
970 
971 	/* Only TX towards PDMA is affected */
972 	if (uc->config.ep_type == PSIL_EP_NATIVE ||
973 	    uc->config.dir != DMA_MEM_TO_DEV)
974 		return true;
975 
976 	peer_bcnt = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_PEER_BCNT_REG);
977 	bcnt = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_BCNT_REG);
978 
979 	/* Transfer is incomplete, store current residue and time stamp */
980 	if (peer_bcnt < bcnt) {
981 		uc->tx_drain.residue = bcnt - peer_bcnt;
982 		uc->tx_drain.tstamp = ktime_get();
983 		return false;
984 	}
985 
986 	return true;
987 }
988 
989 static void udma_check_tx_completion(struct work_struct *work)
990 {
991 	struct udma_chan *uc = container_of(work, typeof(*uc),
992 					    tx_drain.work.work);
993 	bool desc_done = true;
994 	u32 residue_diff;
995 	ktime_t time_diff;
996 	unsigned long delay;
997 
998 	while (1) {
999 		if (uc->desc) {
1000 			/* Get previous residue and time stamp */
1001 			residue_diff = uc->tx_drain.residue;
1002 			time_diff = uc->tx_drain.tstamp;
1003 			/*
1004 			 * Get current residue and time stamp or see if
1005 			 * transfer is complete
1006 			 */
1007 			desc_done = udma_is_desc_really_done(uc, uc->desc);
1008 		}
1009 
1010 		if (!desc_done) {
1011 			/*
1012 			 * Find the time delta and residue delta w.r.t
1013 			 * previous poll
1014 			 */
1015 			time_diff = ktime_sub(uc->tx_drain.tstamp,
1016 					      time_diff) + 1;
1017 			residue_diff -= uc->tx_drain.residue;
1018 			if (residue_diff) {
1019 				/*
1020 				 * Try to guess when we should check
1021 				 * next time by calculating rate at
1022 				 * which data is being drained at the
1023 				 * peer device
1024 				 */
1025 				delay = (time_diff / residue_diff) *
1026 					uc->tx_drain.residue;
1027 			} else {
1028 				/* No progress, check again in 1 second  */
1029 				schedule_delayed_work(&uc->tx_drain.work, HZ);
1030 				break;
1031 			}
1032 
1033 			usleep_range(ktime_to_us(delay),
1034 				     ktime_to_us(delay) + 10);
1035 			continue;
1036 		}
1037 
1038 		if (uc->desc) {
1039 			struct udma_desc *d = uc->desc;
1040 
1041 			uc->bcnt += d->residue;
1042 			udma_start(uc);
1043 			vchan_cookie_complete(&d->vd);
1044 			break;
1045 		}
1046 
1047 		break;
1048 	}
1049 }
1050 
1051 static irqreturn_t udma_ring_irq_handler(int irq, void *data)
1052 {
1053 	struct udma_chan *uc = data;
1054 	struct udma_desc *d;
1055 	unsigned long flags;
1056 	dma_addr_t paddr = 0;
1057 
1058 	if (udma_pop_from_ring(uc, &paddr) || !paddr)
1059 		return IRQ_HANDLED;
1060 
1061 	spin_lock_irqsave(&uc->vc.lock, flags);
1062 
1063 	/* Teardown completion message */
1064 	if (cppi5_desc_is_tdcm(paddr)) {
1065 		complete_all(&uc->teardown_completed);
1066 
1067 		if (uc->terminated_desc) {
1068 			udma_desc_free(&uc->terminated_desc->vd);
1069 			uc->terminated_desc = NULL;
1070 		}
1071 
1072 		if (!uc->desc)
1073 			udma_start(uc);
1074 
1075 		goto out;
1076 	}
1077 
1078 	d = udma_udma_desc_from_paddr(uc, paddr);
1079 
1080 	if (d) {
1081 		dma_addr_t desc_paddr = udma_curr_cppi5_desc_paddr(d,
1082 								   d->desc_idx);
1083 		if (desc_paddr != paddr) {
1084 			dev_err(uc->ud->dev, "not matching descriptors!\n");
1085 			goto out;
1086 		}
1087 
1088 		if (d == uc->desc) {
1089 			/* active descriptor */
1090 			if (uc->cyclic) {
1091 				udma_cyclic_packet_elapsed(uc);
1092 				vchan_cyclic_callback(&d->vd);
1093 			} else {
1094 				if (udma_is_desc_really_done(uc, d)) {
1095 					uc->bcnt += d->residue;
1096 					udma_start(uc);
1097 					vchan_cookie_complete(&d->vd);
1098 				} else {
1099 					schedule_delayed_work(&uc->tx_drain.work,
1100 							      0);
1101 				}
1102 			}
1103 		} else {
1104 			/*
1105 			 * terminated descriptor, mark the descriptor as
1106 			 * completed to update the channel's cookie marker
1107 			 */
1108 			dma_cookie_complete(&d->vd.tx);
1109 		}
1110 	}
1111 out:
1112 	spin_unlock_irqrestore(&uc->vc.lock, flags);
1113 
1114 	return IRQ_HANDLED;
1115 }
1116 
1117 static irqreturn_t udma_udma_irq_handler(int irq, void *data)
1118 {
1119 	struct udma_chan *uc = data;
1120 	struct udma_desc *d;
1121 	unsigned long flags;
1122 
1123 	spin_lock_irqsave(&uc->vc.lock, flags);
1124 	d = uc->desc;
1125 	if (d) {
1126 		d->tr_idx = (d->tr_idx + 1) % d->sglen;
1127 
1128 		if (uc->cyclic) {
1129 			vchan_cyclic_callback(&d->vd);
1130 		} else {
1131 			/* TODO: figure out the real amount of data */
1132 			uc->bcnt += d->residue;
1133 			udma_start(uc);
1134 			vchan_cookie_complete(&d->vd);
1135 		}
1136 	}
1137 
1138 	spin_unlock_irqrestore(&uc->vc.lock, flags);
1139 
1140 	return IRQ_HANDLED;
1141 }
1142 
1143 /**
1144  * __udma_alloc_gp_rflow_range - alloc range of GP RX flows
1145  * @ud: UDMA device
1146  * @from: Start the search from this flow id number
1147  * @cnt: Number of consecutive flow ids to allocate
1148  *
1149  * Allocate range of RX flow ids for future use, those flows can be requested
1150  * only using explicit flow id number. if @from is set to -1 it will try to find
1151  * first free range. if @from is positive value it will force allocation only
1152  * of the specified range of flows.
1153  *
1154  * Returns -ENOMEM if can't find free range.
1155  * -EEXIST if requested range is busy.
1156  * -EINVAL if wrong input values passed.
1157  * Returns flow id on success.
1158  */
1159 static int __udma_alloc_gp_rflow_range(struct udma_dev *ud, int from, int cnt)
1160 {
1161 	int start, tmp_from;
1162 	DECLARE_BITMAP(tmp, K3_UDMA_MAX_RFLOWS);
1163 
1164 	tmp_from = from;
1165 	if (tmp_from < 0)
1166 		tmp_from = ud->rchan_cnt;
1167 	/* default flows can't be allocated and accessible only by id */
1168 	if (tmp_from < ud->rchan_cnt)
1169 		return -EINVAL;
1170 
1171 	if (tmp_from + cnt > ud->rflow_cnt)
1172 		return -EINVAL;
1173 
1174 	bitmap_or(tmp, ud->rflow_gp_map, ud->rflow_gp_map_allocated,
1175 		  ud->rflow_cnt);
1176 
1177 	start = bitmap_find_next_zero_area(tmp,
1178 					   ud->rflow_cnt,
1179 					   tmp_from, cnt, 0);
1180 	if (start >= ud->rflow_cnt)
1181 		return -ENOMEM;
1182 
1183 	if (from >= 0 && start != from)
1184 		return -EEXIST;
1185 
1186 	bitmap_set(ud->rflow_gp_map_allocated, start, cnt);
1187 	return start;
1188 }
1189 
1190 static int __udma_free_gp_rflow_range(struct udma_dev *ud, int from, int cnt)
1191 {
1192 	if (from < ud->rchan_cnt)
1193 		return -EINVAL;
1194 	if (from + cnt > ud->rflow_cnt)
1195 		return -EINVAL;
1196 
1197 	bitmap_clear(ud->rflow_gp_map_allocated, from, cnt);
1198 	return 0;
1199 }
1200 
1201 static struct udma_rflow *__udma_get_rflow(struct udma_dev *ud, int id)
1202 {
1203 	/*
1204 	 * Attempt to request rflow by ID can be made for any rflow
1205 	 * if not in use with assumption that caller knows what's doing.
1206 	 * TI-SCI FW will perform additional permission check ant way, it's
1207 	 * safe
1208 	 */
1209 
1210 	if (id < 0 || id >= ud->rflow_cnt)
1211 		return ERR_PTR(-ENOENT);
1212 
1213 	if (test_bit(id, ud->rflow_in_use))
1214 		return ERR_PTR(-ENOENT);
1215 
1216 	/* GP rflow has to be allocated first */
1217 	if (!test_bit(id, ud->rflow_gp_map) &&
1218 	    !test_bit(id, ud->rflow_gp_map_allocated))
1219 		return ERR_PTR(-EINVAL);
1220 
1221 	dev_dbg(ud->dev, "get rflow%d\n", id);
1222 	set_bit(id, ud->rflow_in_use);
1223 	return &ud->rflows[id];
1224 }
1225 
1226 static void __udma_put_rflow(struct udma_dev *ud, struct udma_rflow *rflow)
1227 {
1228 	if (!test_bit(rflow->id, ud->rflow_in_use)) {
1229 		dev_err(ud->dev, "attempt to put unused rflow%d\n", rflow->id);
1230 		return;
1231 	}
1232 
1233 	dev_dbg(ud->dev, "put rflow%d\n", rflow->id);
1234 	clear_bit(rflow->id, ud->rflow_in_use);
1235 }
1236 
1237 #define UDMA_RESERVE_RESOURCE(res)					\
1238 static struct udma_##res *__udma_reserve_##res(struct udma_dev *ud,	\
1239 					       enum udma_tp_level tpl,	\
1240 					       int id)			\
1241 {									\
1242 	if (id >= 0) {							\
1243 		if (test_bit(id, ud->res##_map)) {			\
1244 			dev_err(ud->dev, "res##%d is in use\n", id);	\
1245 			return ERR_PTR(-ENOENT);			\
1246 		}							\
1247 	} else {							\
1248 		int start;						\
1249 									\
1250 		if (tpl >= ud->match_data->tpl_levels)			\
1251 			tpl = ud->match_data->tpl_levels - 1;		\
1252 									\
1253 		start = ud->match_data->level_start_idx[tpl];		\
1254 									\
1255 		id = find_next_zero_bit(ud->res##_map, ud->res##_cnt,	\
1256 					start);				\
1257 		if (id == ud->res##_cnt) {				\
1258 			return ERR_PTR(-ENOENT);			\
1259 		}							\
1260 	}								\
1261 									\
1262 	set_bit(id, ud->res##_map);					\
1263 	return &ud->res##s[id];						\
1264 }
1265 
1266 UDMA_RESERVE_RESOURCE(tchan);
1267 UDMA_RESERVE_RESOURCE(rchan);
1268 
1269 static int udma_get_tchan(struct udma_chan *uc)
1270 {
1271 	struct udma_dev *ud = uc->ud;
1272 
1273 	if (uc->tchan) {
1274 		dev_dbg(ud->dev, "chan%d: already have tchan%d allocated\n",
1275 			uc->id, uc->tchan->id);
1276 		return 0;
1277 	}
1278 
1279 	uc->tchan = __udma_reserve_tchan(ud, uc->config.channel_tpl, -1);
1280 
1281 	return PTR_ERR_OR_ZERO(uc->tchan);
1282 }
1283 
1284 static int udma_get_rchan(struct udma_chan *uc)
1285 {
1286 	struct udma_dev *ud = uc->ud;
1287 
1288 	if (uc->rchan) {
1289 		dev_dbg(ud->dev, "chan%d: already have rchan%d allocated\n",
1290 			uc->id, uc->rchan->id);
1291 		return 0;
1292 	}
1293 
1294 	uc->rchan = __udma_reserve_rchan(ud, uc->config.channel_tpl, -1);
1295 
1296 	return PTR_ERR_OR_ZERO(uc->rchan);
1297 }
1298 
1299 static int udma_get_chan_pair(struct udma_chan *uc)
1300 {
1301 	struct udma_dev *ud = uc->ud;
1302 	const struct udma_match_data *match_data = ud->match_data;
1303 	int chan_id, end;
1304 
1305 	if ((uc->tchan && uc->rchan) && uc->tchan->id == uc->rchan->id) {
1306 		dev_info(ud->dev, "chan%d: already have %d pair allocated\n",
1307 			 uc->id, uc->tchan->id);
1308 		return 0;
1309 	}
1310 
1311 	if (uc->tchan) {
1312 		dev_err(ud->dev, "chan%d: already have tchan%d allocated\n",
1313 			uc->id, uc->tchan->id);
1314 		return -EBUSY;
1315 	} else if (uc->rchan) {
1316 		dev_err(ud->dev, "chan%d: already have rchan%d allocated\n",
1317 			uc->id, uc->rchan->id);
1318 		return -EBUSY;
1319 	}
1320 
1321 	/* Can be optimized, but let's have it like this for now */
1322 	end = min(ud->tchan_cnt, ud->rchan_cnt);
1323 	/* Try to use the highest TPL channel pair for MEM_TO_MEM channels */
1324 	chan_id = match_data->level_start_idx[match_data->tpl_levels - 1];
1325 	for (; chan_id < end; chan_id++) {
1326 		if (!test_bit(chan_id, ud->tchan_map) &&
1327 		    !test_bit(chan_id, ud->rchan_map))
1328 			break;
1329 	}
1330 
1331 	if (chan_id == end)
1332 		return -ENOENT;
1333 
1334 	set_bit(chan_id, ud->tchan_map);
1335 	set_bit(chan_id, ud->rchan_map);
1336 	uc->tchan = &ud->tchans[chan_id];
1337 	uc->rchan = &ud->rchans[chan_id];
1338 
1339 	return 0;
1340 }
1341 
1342 static int udma_get_rflow(struct udma_chan *uc, int flow_id)
1343 {
1344 	struct udma_dev *ud = uc->ud;
1345 
1346 	if (!uc->rchan) {
1347 		dev_err(ud->dev, "chan%d: does not have rchan??\n", uc->id);
1348 		return -EINVAL;
1349 	}
1350 
1351 	if (uc->rflow) {
1352 		dev_dbg(ud->dev, "chan%d: already have rflow%d allocated\n",
1353 			uc->id, uc->rflow->id);
1354 		return 0;
1355 	}
1356 
1357 	uc->rflow = __udma_get_rflow(ud, flow_id);
1358 
1359 	return PTR_ERR_OR_ZERO(uc->rflow);
1360 }
1361 
1362 static void udma_put_rchan(struct udma_chan *uc)
1363 {
1364 	struct udma_dev *ud = uc->ud;
1365 
1366 	if (uc->rchan) {
1367 		dev_dbg(ud->dev, "chan%d: put rchan%d\n", uc->id,
1368 			uc->rchan->id);
1369 		clear_bit(uc->rchan->id, ud->rchan_map);
1370 		uc->rchan = NULL;
1371 	}
1372 }
1373 
1374 static void udma_put_tchan(struct udma_chan *uc)
1375 {
1376 	struct udma_dev *ud = uc->ud;
1377 
1378 	if (uc->tchan) {
1379 		dev_dbg(ud->dev, "chan%d: put tchan%d\n", uc->id,
1380 			uc->tchan->id);
1381 		clear_bit(uc->tchan->id, ud->tchan_map);
1382 		uc->tchan = NULL;
1383 	}
1384 }
1385 
1386 static void udma_put_rflow(struct udma_chan *uc)
1387 {
1388 	struct udma_dev *ud = uc->ud;
1389 
1390 	if (uc->rflow) {
1391 		dev_dbg(ud->dev, "chan%d: put rflow%d\n", uc->id,
1392 			uc->rflow->id);
1393 		__udma_put_rflow(ud, uc->rflow);
1394 		uc->rflow = NULL;
1395 	}
1396 }
1397 
1398 static void udma_free_tx_resources(struct udma_chan *uc)
1399 {
1400 	if (!uc->tchan)
1401 		return;
1402 
1403 	k3_ringacc_ring_free(uc->tchan->t_ring);
1404 	k3_ringacc_ring_free(uc->tchan->tc_ring);
1405 	uc->tchan->t_ring = NULL;
1406 	uc->tchan->tc_ring = NULL;
1407 
1408 	udma_put_tchan(uc);
1409 }
1410 
1411 static int udma_alloc_tx_resources(struct udma_chan *uc)
1412 {
1413 	struct k3_ring_cfg ring_cfg;
1414 	struct udma_dev *ud = uc->ud;
1415 	int ret;
1416 
1417 	ret = udma_get_tchan(uc);
1418 	if (ret)
1419 		return ret;
1420 
1421 	uc->tchan->t_ring = k3_ringacc_request_ring(ud->ringacc,
1422 						    uc->tchan->id, 0);
1423 	if (!uc->tchan->t_ring) {
1424 		ret = -EBUSY;
1425 		goto err_tx_ring;
1426 	}
1427 
1428 	uc->tchan->tc_ring = k3_ringacc_request_ring(ud->ringacc, -1, 0);
1429 	if (!uc->tchan->tc_ring) {
1430 		ret = -EBUSY;
1431 		goto err_txc_ring;
1432 	}
1433 
1434 	memset(&ring_cfg, 0, sizeof(ring_cfg));
1435 	ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1436 	ring_cfg.elm_size = K3_RINGACC_RING_ELSIZE_8;
1437 	ring_cfg.mode = K3_RINGACC_RING_MODE_MESSAGE;
1438 
1439 	ret = k3_ringacc_ring_cfg(uc->tchan->t_ring, &ring_cfg);
1440 	ret |= k3_ringacc_ring_cfg(uc->tchan->tc_ring, &ring_cfg);
1441 
1442 	if (ret)
1443 		goto err_ringcfg;
1444 
1445 	return 0;
1446 
1447 err_ringcfg:
1448 	k3_ringacc_ring_free(uc->tchan->tc_ring);
1449 	uc->tchan->tc_ring = NULL;
1450 err_txc_ring:
1451 	k3_ringacc_ring_free(uc->tchan->t_ring);
1452 	uc->tchan->t_ring = NULL;
1453 err_tx_ring:
1454 	udma_put_tchan(uc);
1455 
1456 	return ret;
1457 }
1458 
1459 static void udma_free_rx_resources(struct udma_chan *uc)
1460 {
1461 	if (!uc->rchan)
1462 		return;
1463 
1464 	if (uc->rflow) {
1465 		struct udma_rflow *rflow = uc->rflow;
1466 
1467 		k3_ringacc_ring_free(rflow->fd_ring);
1468 		k3_ringacc_ring_free(rflow->r_ring);
1469 		rflow->fd_ring = NULL;
1470 		rflow->r_ring = NULL;
1471 
1472 		udma_put_rflow(uc);
1473 	}
1474 
1475 	udma_put_rchan(uc);
1476 }
1477 
1478 static int udma_alloc_rx_resources(struct udma_chan *uc)
1479 {
1480 	struct udma_dev *ud = uc->ud;
1481 	struct k3_ring_cfg ring_cfg;
1482 	struct udma_rflow *rflow;
1483 	int fd_ring_id;
1484 	int ret;
1485 
1486 	ret = udma_get_rchan(uc);
1487 	if (ret)
1488 		return ret;
1489 
1490 	/* For MEM_TO_MEM we don't need rflow or rings */
1491 	if (uc->config.dir == DMA_MEM_TO_MEM)
1492 		return 0;
1493 
1494 	ret = udma_get_rflow(uc, uc->rchan->id);
1495 	if (ret) {
1496 		ret = -EBUSY;
1497 		goto err_rflow;
1498 	}
1499 
1500 	rflow = uc->rflow;
1501 	fd_ring_id = ud->tchan_cnt + ud->echan_cnt + uc->rchan->id;
1502 	rflow->fd_ring = k3_ringacc_request_ring(ud->ringacc, fd_ring_id, 0);
1503 	if (!rflow->fd_ring) {
1504 		ret = -EBUSY;
1505 		goto err_rx_ring;
1506 	}
1507 
1508 	rflow->r_ring = k3_ringacc_request_ring(ud->ringacc, -1, 0);
1509 	if (!rflow->r_ring) {
1510 		ret = -EBUSY;
1511 		goto err_rxc_ring;
1512 	}
1513 
1514 	memset(&ring_cfg, 0, sizeof(ring_cfg));
1515 
1516 	if (uc->config.pkt_mode)
1517 		ring_cfg.size = SG_MAX_SEGMENTS;
1518 	else
1519 		ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1520 
1521 	ring_cfg.elm_size = K3_RINGACC_RING_ELSIZE_8;
1522 	ring_cfg.mode = K3_RINGACC_RING_MODE_MESSAGE;
1523 
1524 	ret = k3_ringacc_ring_cfg(rflow->fd_ring, &ring_cfg);
1525 	ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1526 	ret |= k3_ringacc_ring_cfg(rflow->r_ring, &ring_cfg);
1527 
1528 	if (ret)
1529 		goto err_ringcfg;
1530 
1531 	return 0;
1532 
1533 err_ringcfg:
1534 	k3_ringacc_ring_free(rflow->r_ring);
1535 	rflow->r_ring = NULL;
1536 err_rxc_ring:
1537 	k3_ringacc_ring_free(rflow->fd_ring);
1538 	rflow->fd_ring = NULL;
1539 err_rx_ring:
1540 	udma_put_rflow(uc);
1541 err_rflow:
1542 	udma_put_rchan(uc);
1543 
1544 	return ret;
1545 }
1546 
1547 #define TISCI_TCHAN_VALID_PARAMS (				\
1548 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID |	\
1549 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_FILT_EINFO_VALID |	\
1550 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_FILT_PSWORDS_VALID |	\
1551 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_CHAN_TYPE_VALID |		\
1552 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_SUPR_TDPKT_VALID |	\
1553 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_FETCH_SIZE_VALID |		\
1554 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_CQ_QNUM_VALID |		\
1555 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_ATYPE_VALID)
1556 
1557 #define TISCI_RCHAN_VALID_PARAMS (				\
1558 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID |	\
1559 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_FETCH_SIZE_VALID |		\
1560 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_CQ_QNUM_VALID |		\
1561 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_CHAN_TYPE_VALID |		\
1562 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_IGNORE_SHORT_VALID |	\
1563 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_IGNORE_LONG_VALID |	\
1564 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_FLOWID_START_VALID |	\
1565 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_FLOWID_CNT_VALID |	\
1566 	TI_SCI_MSG_VALUE_RM_UDMAP_CH_ATYPE_VALID)
1567 
1568 static int udma_tisci_m2m_channel_config(struct udma_chan *uc)
1569 {
1570 	struct udma_dev *ud = uc->ud;
1571 	struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1572 	const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1573 	struct udma_tchan *tchan = uc->tchan;
1574 	struct udma_rchan *rchan = uc->rchan;
1575 	int ret = 0;
1576 
1577 	/* Non synchronized - mem to mem type of transfer */
1578 	int tc_ring = k3_ringacc_get_ring_id(tchan->tc_ring);
1579 	struct ti_sci_msg_rm_udmap_tx_ch_cfg req_tx = { 0 };
1580 	struct ti_sci_msg_rm_udmap_rx_ch_cfg req_rx = { 0 };
1581 
1582 	req_tx.valid_params = TISCI_TCHAN_VALID_PARAMS;
1583 	req_tx.nav_id = tisci_rm->tisci_dev_id;
1584 	req_tx.index = tchan->id;
1585 	req_tx.tx_chan_type = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_BCOPY_PBRR;
1586 	req_tx.tx_fetch_size = sizeof(struct cppi5_desc_hdr_t) >> 2;
1587 	req_tx.txcq_qnum = tc_ring;
1588 	req_tx.tx_atype = ud->atype;
1589 
1590 	ret = tisci_ops->tx_ch_cfg(tisci_rm->tisci, &req_tx);
1591 	if (ret) {
1592 		dev_err(ud->dev, "tchan%d cfg failed %d\n", tchan->id, ret);
1593 		return ret;
1594 	}
1595 
1596 	req_rx.valid_params = TISCI_RCHAN_VALID_PARAMS;
1597 	req_rx.nav_id = tisci_rm->tisci_dev_id;
1598 	req_rx.index = rchan->id;
1599 	req_rx.rx_fetch_size = sizeof(struct cppi5_desc_hdr_t) >> 2;
1600 	req_rx.rxcq_qnum = tc_ring;
1601 	req_rx.rx_chan_type = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_BCOPY_PBRR;
1602 	req_rx.rx_atype = ud->atype;
1603 
1604 	ret = tisci_ops->rx_ch_cfg(tisci_rm->tisci, &req_rx);
1605 	if (ret)
1606 		dev_err(ud->dev, "rchan%d alloc failed %d\n", rchan->id, ret);
1607 
1608 	return ret;
1609 }
1610 
1611 static int udma_tisci_tx_channel_config(struct udma_chan *uc)
1612 {
1613 	struct udma_dev *ud = uc->ud;
1614 	struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1615 	const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1616 	struct udma_tchan *tchan = uc->tchan;
1617 	int tc_ring = k3_ringacc_get_ring_id(tchan->tc_ring);
1618 	struct ti_sci_msg_rm_udmap_tx_ch_cfg req_tx = { 0 };
1619 	u32 mode, fetch_size;
1620 	int ret = 0;
1621 
1622 	if (uc->config.pkt_mode) {
1623 		mode = TI_SCI_RM_UDMAP_CHAN_TYPE_PKT_PBRR;
1624 		fetch_size = cppi5_hdesc_calc_size(uc->config.needs_epib,
1625 						   uc->config.psd_size, 0);
1626 	} else {
1627 		mode = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_PBRR;
1628 		fetch_size = sizeof(struct cppi5_desc_hdr_t);
1629 	}
1630 
1631 	req_tx.valid_params = TISCI_TCHAN_VALID_PARAMS;
1632 	req_tx.nav_id = tisci_rm->tisci_dev_id;
1633 	req_tx.index = tchan->id;
1634 	req_tx.tx_chan_type = mode;
1635 	req_tx.tx_supr_tdpkt = uc->config.notdpkt;
1636 	req_tx.tx_fetch_size = fetch_size >> 2;
1637 	req_tx.txcq_qnum = tc_ring;
1638 	req_tx.tx_atype = uc->config.atype;
1639 
1640 	ret = tisci_ops->tx_ch_cfg(tisci_rm->tisci, &req_tx);
1641 	if (ret)
1642 		dev_err(ud->dev, "tchan%d cfg failed %d\n", tchan->id, ret);
1643 
1644 	return ret;
1645 }
1646 
1647 static int udma_tisci_rx_channel_config(struct udma_chan *uc)
1648 {
1649 	struct udma_dev *ud = uc->ud;
1650 	struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1651 	const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1652 	struct udma_rchan *rchan = uc->rchan;
1653 	int fd_ring = k3_ringacc_get_ring_id(uc->rflow->fd_ring);
1654 	int rx_ring = k3_ringacc_get_ring_id(uc->rflow->r_ring);
1655 	struct ti_sci_msg_rm_udmap_rx_ch_cfg req_rx = { 0 };
1656 	struct ti_sci_msg_rm_udmap_flow_cfg flow_req = { 0 };
1657 	u32 mode, fetch_size;
1658 	int ret = 0;
1659 
1660 	if (uc->config.pkt_mode) {
1661 		mode = TI_SCI_RM_UDMAP_CHAN_TYPE_PKT_PBRR;
1662 		fetch_size = cppi5_hdesc_calc_size(uc->config.needs_epib,
1663 						   uc->config.psd_size, 0);
1664 	} else {
1665 		mode = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_PBRR;
1666 		fetch_size = sizeof(struct cppi5_desc_hdr_t);
1667 	}
1668 
1669 	req_rx.valid_params = TISCI_RCHAN_VALID_PARAMS;
1670 	req_rx.nav_id = tisci_rm->tisci_dev_id;
1671 	req_rx.index = rchan->id;
1672 	req_rx.rx_fetch_size =  fetch_size >> 2;
1673 	req_rx.rxcq_qnum = rx_ring;
1674 	req_rx.rx_chan_type = mode;
1675 	req_rx.rx_atype = uc->config.atype;
1676 
1677 	ret = tisci_ops->rx_ch_cfg(tisci_rm->tisci, &req_rx);
1678 	if (ret) {
1679 		dev_err(ud->dev, "rchan%d cfg failed %d\n", rchan->id, ret);
1680 		return ret;
1681 	}
1682 
1683 	flow_req.valid_params =
1684 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_EINFO_PRESENT_VALID |
1685 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_PSINFO_PRESENT_VALID |
1686 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_ERROR_HANDLING_VALID |
1687 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DESC_TYPE_VALID |
1688 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_QNUM_VALID |
1689 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_SRC_TAG_HI_SEL_VALID |
1690 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_SRC_TAG_LO_SEL_VALID |
1691 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_TAG_HI_SEL_VALID |
1692 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_TAG_LO_SEL_VALID |
1693 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ0_SZ0_QNUM_VALID |
1694 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ1_QNUM_VALID |
1695 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ2_QNUM_VALID |
1696 		TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ3_QNUM_VALID;
1697 
1698 	flow_req.nav_id = tisci_rm->tisci_dev_id;
1699 	flow_req.flow_index = rchan->id;
1700 
1701 	if (uc->config.needs_epib)
1702 		flow_req.rx_einfo_present = 1;
1703 	else
1704 		flow_req.rx_einfo_present = 0;
1705 	if (uc->config.psd_size)
1706 		flow_req.rx_psinfo_present = 1;
1707 	else
1708 		flow_req.rx_psinfo_present = 0;
1709 	flow_req.rx_error_handling = 1;
1710 	flow_req.rx_dest_qnum = rx_ring;
1711 	flow_req.rx_src_tag_hi_sel = UDMA_RFLOW_SRCTAG_NONE;
1712 	flow_req.rx_src_tag_lo_sel = UDMA_RFLOW_SRCTAG_SRC_TAG;
1713 	flow_req.rx_dest_tag_hi_sel = UDMA_RFLOW_DSTTAG_DST_TAG_HI;
1714 	flow_req.rx_dest_tag_lo_sel = UDMA_RFLOW_DSTTAG_DST_TAG_LO;
1715 	flow_req.rx_fdq0_sz0_qnum = fd_ring;
1716 	flow_req.rx_fdq1_qnum = fd_ring;
1717 	flow_req.rx_fdq2_qnum = fd_ring;
1718 	flow_req.rx_fdq3_qnum = fd_ring;
1719 
1720 	ret = tisci_ops->rx_flow_cfg(tisci_rm->tisci, &flow_req);
1721 
1722 	if (ret)
1723 		dev_err(ud->dev, "flow%d config failed: %d\n", rchan->id, ret);
1724 
1725 	return 0;
1726 }
1727 
1728 static int udma_alloc_chan_resources(struct dma_chan *chan)
1729 {
1730 	struct udma_chan *uc = to_udma_chan(chan);
1731 	struct udma_dev *ud = to_udma_dev(chan->device);
1732 	const struct udma_match_data *match_data = ud->match_data;
1733 	struct k3_ring *irq_ring;
1734 	u32 irq_udma_idx;
1735 	int ret;
1736 
1737 	if (uc->config.pkt_mode || uc->config.dir == DMA_MEM_TO_MEM) {
1738 		uc->use_dma_pool = true;
1739 		/* in case of MEM_TO_MEM we have maximum of two TRs */
1740 		if (uc->config.dir == DMA_MEM_TO_MEM) {
1741 			uc->config.hdesc_size = cppi5_trdesc_calc_size(
1742 					sizeof(struct cppi5_tr_type15_t), 2);
1743 			uc->config.pkt_mode = false;
1744 		}
1745 	}
1746 
1747 	if (uc->use_dma_pool) {
1748 		uc->hdesc_pool = dma_pool_create(uc->name, ud->ddev.dev,
1749 						 uc->config.hdesc_size,
1750 						 ud->desc_align,
1751 						 0);
1752 		if (!uc->hdesc_pool) {
1753 			dev_err(ud->ddev.dev,
1754 				"Descriptor pool allocation failed\n");
1755 			uc->use_dma_pool = false;
1756 			return -ENOMEM;
1757 		}
1758 	}
1759 
1760 	/*
1761 	 * Make sure that the completion is in a known state:
1762 	 * No teardown, the channel is idle
1763 	 */
1764 	reinit_completion(&uc->teardown_completed);
1765 	complete_all(&uc->teardown_completed);
1766 	uc->state = UDMA_CHAN_IS_IDLE;
1767 
1768 	switch (uc->config.dir) {
1769 	case DMA_MEM_TO_MEM:
1770 		/* Non synchronized - mem to mem type of transfer */
1771 		dev_dbg(uc->ud->dev, "%s: chan%d as MEM-to-MEM\n", __func__,
1772 			uc->id);
1773 
1774 		ret = udma_get_chan_pair(uc);
1775 		if (ret)
1776 			return ret;
1777 
1778 		ret = udma_alloc_tx_resources(uc);
1779 		if (ret)
1780 			return ret;
1781 
1782 		ret = udma_alloc_rx_resources(uc);
1783 		if (ret) {
1784 			udma_free_tx_resources(uc);
1785 			return ret;
1786 		}
1787 
1788 		uc->config.src_thread = ud->psil_base + uc->tchan->id;
1789 		uc->config.dst_thread = (ud->psil_base + uc->rchan->id) |
1790 					K3_PSIL_DST_THREAD_ID_OFFSET;
1791 
1792 		irq_ring = uc->tchan->tc_ring;
1793 		irq_udma_idx = uc->tchan->id;
1794 
1795 		ret = udma_tisci_m2m_channel_config(uc);
1796 		break;
1797 	case DMA_MEM_TO_DEV:
1798 		/* Slave transfer synchronized - mem to dev (TX) trasnfer */
1799 		dev_dbg(uc->ud->dev, "%s: chan%d as MEM-to-DEV\n", __func__,
1800 			uc->id);
1801 
1802 		ret = udma_alloc_tx_resources(uc);
1803 		if (ret) {
1804 			uc->config.remote_thread_id = -1;
1805 			return ret;
1806 		}
1807 
1808 		uc->config.src_thread = ud->psil_base + uc->tchan->id;
1809 		uc->config.dst_thread = uc->config.remote_thread_id;
1810 		uc->config.dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
1811 
1812 		irq_ring = uc->tchan->tc_ring;
1813 		irq_udma_idx = uc->tchan->id;
1814 
1815 		ret = udma_tisci_tx_channel_config(uc);
1816 		break;
1817 	case DMA_DEV_TO_MEM:
1818 		/* Slave transfer synchronized - dev to mem (RX) trasnfer */
1819 		dev_dbg(uc->ud->dev, "%s: chan%d as DEV-to-MEM\n", __func__,
1820 			uc->id);
1821 
1822 		ret = udma_alloc_rx_resources(uc);
1823 		if (ret) {
1824 			uc->config.remote_thread_id = -1;
1825 			return ret;
1826 		}
1827 
1828 		uc->config.src_thread = uc->config.remote_thread_id;
1829 		uc->config.dst_thread = (ud->psil_base + uc->rchan->id) |
1830 					K3_PSIL_DST_THREAD_ID_OFFSET;
1831 
1832 		irq_ring = uc->rflow->r_ring;
1833 		irq_udma_idx = match_data->rchan_oes_offset + uc->rchan->id;
1834 
1835 		ret = udma_tisci_rx_channel_config(uc);
1836 		break;
1837 	default:
1838 		/* Can not happen */
1839 		dev_err(uc->ud->dev, "%s: chan%d invalid direction (%u)\n",
1840 			__func__, uc->id, uc->config.dir);
1841 		return -EINVAL;
1842 	}
1843 
1844 	/* check if the channel configuration was successful */
1845 	if (ret)
1846 		goto err_res_free;
1847 
1848 	if (udma_is_chan_running(uc)) {
1849 		dev_warn(ud->dev, "chan%d: is running!\n", uc->id);
1850 		udma_stop(uc);
1851 		if (udma_is_chan_running(uc)) {
1852 			dev_err(ud->dev, "chan%d: won't stop!\n", uc->id);
1853 			ret = -EBUSY;
1854 			goto err_res_free;
1855 		}
1856 	}
1857 
1858 	/* PSI-L pairing */
1859 	ret = navss_psil_pair(ud, uc->config.src_thread, uc->config.dst_thread);
1860 	if (ret) {
1861 		dev_err(ud->dev, "PSI-L pairing failed: 0x%04x -> 0x%04x\n",
1862 			uc->config.src_thread, uc->config.dst_thread);
1863 		goto err_res_free;
1864 	}
1865 
1866 	uc->psil_paired = true;
1867 
1868 	uc->irq_num_ring = k3_ringacc_get_ring_irq_num(irq_ring);
1869 	if (uc->irq_num_ring <= 0) {
1870 		dev_err(ud->dev, "Failed to get ring irq (index: %u)\n",
1871 			k3_ringacc_get_ring_id(irq_ring));
1872 		ret = -EINVAL;
1873 		goto err_psi_free;
1874 	}
1875 
1876 	ret = request_irq(uc->irq_num_ring, udma_ring_irq_handler,
1877 			  IRQF_TRIGGER_HIGH, uc->name, uc);
1878 	if (ret) {
1879 		dev_err(ud->dev, "chan%d: ring irq request failed\n", uc->id);
1880 		goto err_irq_free;
1881 	}
1882 
1883 	/* Event from UDMA (TR events) only needed for slave TR mode channels */
1884 	if (is_slave_direction(uc->config.dir) && !uc->config.pkt_mode) {
1885 		uc->irq_num_udma = ti_sci_inta_msi_get_virq(ud->dev,
1886 							    irq_udma_idx);
1887 		if (uc->irq_num_udma <= 0) {
1888 			dev_err(ud->dev, "Failed to get udma irq (index: %u)\n",
1889 				irq_udma_idx);
1890 			free_irq(uc->irq_num_ring, uc);
1891 			ret = -EINVAL;
1892 			goto err_irq_free;
1893 		}
1894 
1895 		ret = request_irq(uc->irq_num_udma, udma_udma_irq_handler, 0,
1896 				  uc->name, uc);
1897 		if (ret) {
1898 			dev_err(ud->dev, "chan%d: UDMA irq request failed\n",
1899 				uc->id);
1900 			free_irq(uc->irq_num_ring, uc);
1901 			goto err_irq_free;
1902 		}
1903 	} else {
1904 		uc->irq_num_udma = 0;
1905 	}
1906 
1907 	udma_reset_rings(uc);
1908 
1909 	INIT_DELAYED_WORK_ONSTACK(&uc->tx_drain.work,
1910 				  udma_check_tx_completion);
1911 	return 0;
1912 
1913 err_irq_free:
1914 	uc->irq_num_ring = 0;
1915 	uc->irq_num_udma = 0;
1916 err_psi_free:
1917 	navss_psil_unpair(ud, uc->config.src_thread, uc->config.dst_thread);
1918 	uc->psil_paired = false;
1919 err_res_free:
1920 	udma_free_tx_resources(uc);
1921 	udma_free_rx_resources(uc);
1922 
1923 	udma_reset_uchan(uc);
1924 
1925 	if (uc->use_dma_pool) {
1926 		dma_pool_destroy(uc->hdesc_pool);
1927 		uc->use_dma_pool = false;
1928 	}
1929 
1930 	return ret;
1931 }
1932 
1933 static int udma_slave_config(struct dma_chan *chan,
1934 			     struct dma_slave_config *cfg)
1935 {
1936 	struct udma_chan *uc = to_udma_chan(chan);
1937 
1938 	memcpy(&uc->cfg, cfg, sizeof(uc->cfg));
1939 
1940 	return 0;
1941 }
1942 
1943 static struct udma_desc *udma_alloc_tr_desc(struct udma_chan *uc,
1944 					    size_t tr_size, int tr_count,
1945 					    enum dma_transfer_direction dir)
1946 {
1947 	struct udma_hwdesc *hwdesc;
1948 	struct cppi5_desc_hdr_t *tr_desc;
1949 	struct udma_desc *d;
1950 	u32 reload_count = 0;
1951 	u32 ring_id;
1952 
1953 	switch (tr_size) {
1954 	case 16:
1955 	case 32:
1956 	case 64:
1957 	case 128:
1958 		break;
1959 	default:
1960 		dev_err(uc->ud->dev, "Unsupported TR size of %zu\n", tr_size);
1961 		return NULL;
1962 	}
1963 
1964 	/* We have only one descriptor containing multiple TRs */
1965 	d = kzalloc(sizeof(*d) + sizeof(d->hwdesc[0]), GFP_NOWAIT);
1966 	if (!d)
1967 		return NULL;
1968 
1969 	d->sglen = tr_count;
1970 
1971 	d->hwdesc_count = 1;
1972 	hwdesc = &d->hwdesc[0];
1973 
1974 	/* Allocate memory for DMA ring descriptor */
1975 	if (uc->use_dma_pool) {
1976 		hwdesc->cppi5_desc_size = uc->config.hdesc_size;
1977 		hwdesc->cppi5_desc_vaddr = dma_pool_zalloc(uc->hdesc_pool,
1978 						GFP_NOWAIT,
1979 						&hwdesc->cppi5_desc_paddr);
1980 	} else {
1981 		hwdesc->cppi5_desc_size = cppi5_trdesc_calc_size(tr_size,
1982 								 tr_count);
1983 		hwdesc->cppi5_desc_size = ALIGN(hwdesc->cppi5_desc_size,
1984 						uc->ud->desc_align);
1985 		hwdesc->cppi5_desc_vaddr = dma_alloc_coherent(uc->ud->dev,
1986 						hwdesc->cppi5_desc_size,
1987 						&hwdesc->cppi5_desc_paddr,
1988 						GFP_NOWAIT);
1989 	}
1990 
1991 	if (!hwdesc->cppi5_desc_vaddr) {
1992 		kfree(d);
1993 		return NULL;
1994 	}
1995 
1996 	/* Start of the TR req records */
1997 	hwdesc->tr_req_base = hwdesc->cppi5_desc_vaddr + tr_size;
1998 	/* Start address of the TR response array */
1999 	hwdesc->tr_resp_base = hwdesc->tr_req_base + tr_size * tr_count;
2000 
2001 	tr_desc = hwdesc->cppi5_desc_vaddr;
2002 
2003 	if (uc->cyclic)
2004 		reload_count = CPPI5_INFO0_TRDESC_RLDCNT_INFINITE;
2005 
2006 	if (dir == DMA_DEV_TO_MEM)
2007 		ring_id = k3_ringacc_get_ring_id(uc->rflow->r_ring);
2008 	else
2009 		ring_id = k3_ringacc_get_ring_id(uc->tchan->tc_ring);
2010 
2011 	cppi5_trdesc_init(tr_desc, tr_count, tr_size, 0, reload_count);
2012 	cppi5_desc_set_pktids(tr_desc, uc->id,
2013 			      CPPI5_INFO1_DESC_FLOWID_DEFAULT);
2014 	cppi5_desc_set_retpolicy(tr_desc, 0, ring_id);
2015 
2016 	return d;
2017 }
2018 
2019 /**
2020  * udma_get_tr_counters - calculate TR counters for a given length
2021  * @len: Length of the trasnfer
2022  * @align_to: Preferred alignment
2023  * @tr0_cnt0: First TR icnt0
2024  * @tr0_cnt1: First TR icnt1
2025  * @tr1_cnt0: Second (if used) TR icnt0
2026  *
2027  * For len < SZ_64K only one TR is enough, tr1_cnt0 is not updated
2028  * For len >= SZ_64K two TRs are used in a simple way:
2029  * First TR: SZ_64K-alignment blocks (tr0_cnt0, tr0_cnt1)
2030  * Second TR: the remaining length (tr1_cnt0)
2031  *
2032  * Returns the number of TRs the length needs (1 or 2)
2033  * -EINVAL if the length can not be supported
2034  */
2035 static int udma_get_tr_counters(size_t len, unsigned long align_to,
2036 				u16 *tr0_cnt0, u16 *tr0_cnt1, u16 *tr1_cnt0)
2037 {
2038 	if (len < SZ_64K) {
2039 		*tr0_cnt0 = len;
2040 		*tr0_cnt1 = 1;
2041 
2042 		return 1;
2043 	}
2044 
2045 	if (align_to > 3)
2046 		align_to = 3;
2047 
2048 realign:
2049 	*tr0_cnt0 = SZ_64K - BIT(align_to);
2050 	if (len / *tr0_cnt0 >= SZ_64K) {
2051 		if (align_to) {
2052 			align_to--;
2053 			goto realign;
2054 		}
2055 		return -EINVAL;
2056 	}
2057 
2058 	*tr0_cnt1 = len / *tr0_cnt0;
2059 	*tr1_cnt0 = len % *tr0_cnt0;
2060 
2061 	return 2;
2062 }
2063 
2064 static struct udma_desc *
2065 udma_prep_slave_sg_tr(struct udma_chan *uc, struct scatterlist *sgl,
2066 		      unsigned int sglen, enum dma_transfer_direction dir,
2067 		      unsigned long tx_flags, void *context)
2068 {
2069 	struct scatterlist *sgent;
2070 	struct udma_desc *d;
2071 	struct cppi5_tr_type1_t *tr_req = NULL;
2072 	u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
2073 	unsigned int i;
2074 	size_t tr_size;
2075 	int num_tr = 0;
2076 	int tr_idx = 0;
2077 
2078 	if (!is_slave_direction(dir)) {
2079 		dev_err(uc->ud->dev, "Only slave cyclic is supported\n");
2080 		return NULL;
2081 	}
2082 
2083 	/* estimate the number of TRs we will need */
2084 	for_each_sg(sgl, sgent, sglen, i) {
2085 		if (sg_dma_len(sgent) < SZ_64K)
2086 			num_tr++;
2087 		else
2088 			num_tr += 2;
2089 	}
2090 
2091 	/* Now allocate and setup the descriptor. */
2092 	tr_size = sizeof(struct cppi5_tr_type1_t);
2093 	d = udma_alloc_tr_desc(uc, tr_size, num_tr, dir);
2094 	if (!d)
2095 		return NULL;
2096 
2097 	d->sglen = sglen;
2098 
2099 	tr_req = d->hwdesc[0].tr_req_base;
2100 	for_each_sg(sgl, sgent, sglen, i) {
2101 		dma_addr_t sg_addr = sg_dma_address(sgent);
2102 
2103 		num_tr = udma_get_tr_counters(sg_dma_len(sgent), __ffs(sg_addr),
2104 					      &tr0_cnt0, &tr0_cnt1, &tr1_cnt0);
2105 		if (num_tr < 0) {
2106 			dev_err(uc->ud->dev, "size %u is not supported\n",
2107 				sg_dma_len(sgent));
2108 			udma_free_hwdesc(uc, d);
2109 			kfree(d);
2110 			return NULL;
2111 		}
2112 
2113 		cppi5_tr_init(&tr_req[i].flags, CPPI5_TR_TYPE1, false, false,
2114 			      CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2115 		cppi5_tr_csf_set(&tr_req[i].flags, CPPI5_TR_CSF_SUPR_EVT);
2116 
2117 		tr_req[tr_idx].addr = sg_addr;
2118 		tr_req[tr_idx].icnt0 = tr0_cnt0;
2119 		tr_req[tr_idx].icnt1 = tr0_cnt1;
2120 		tr_req[tr_idx].dim1 = tr0_cnt0;
2121 		tr_idx++;
2122 
2123 		if (num_tr == 2) {
2124 			cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1,
2125 				      false, false,
2126 				      CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2127 			cppi5_tr_csf_set(&tr_req[tr_idx].flags,
2128 					 CPPI5_TR_CSF_SUPR_EVT);
2129 
2130 			tr_req[tr_idx].addr = sg_addr + tr0_cnt1 * tr0_cnt0;
2131 			tr_req[tr_idx].icnt0 = tr1_cnt0;
2132 			tr_req[tr_idx].icnt1 = 1;
2133 			tr_req[tr_idx].dim1 = tr1_cnt0;
2134 			tr_idx++;
2135 		}
2136 
2137 		d->residue += sg_dma_len(sgent);
2138 	}
2139 
2140 	cppi5_tr_csf_set(&tr_req[tr_idx - 1].flags,
2141 			 CPPI5_TR_CSF_SUPR_EVT | CPPI5_TR_CSF_EOP);
2142 
2143 	return d;
2144 }
2145 
2146 static int udma_configure_statictr(struct udma_chan *uc, struct udma_desc *d,
2147 				   enum dma_slave_buswidth dev_width,
2148 				   u16 elcnt)
2149 {
2150 	if (uc->config.ep_type != PSIL_EP_PDMA_XY)
2151 		return 0;
2152 
2153 	/* Bus width translates to the element size (ES) */
2154 	switch (dev_width) {
2155 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
2156 		d->static_tr.elsize = 0;
2157 		break;
2158 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
2159 		d->static_tr.elsize = 1;
2160 		break;
2161 	case DMA_SLAVE_BUSWIDTH_3_BYTES:
2162 		d->static_tr.elsize = 2;
2163 		break;
2164 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
2165 		d->static_tr.elsize = 3;
2166 		break;
2167 	case DMA_SLAVE_BUSWIDTH_8_BYTES:
2168 		d->static_tr.elsize = 4;
2169 		break;
2170 	default: /* not reached */
2171 		return -EINVAL;
2172 	}
2173 
2174 	d->static_tr.elcnt = elcnt;
2175 
2176 	/*
2177 	 * PDMA must to close the packet when the channel is in packet mode.
2178 	 * For TR mode when the channel is not cyclic we also need PDMA to close
2179 	 * the packet otherwise the transfer will stall because PDMA holds on
2180 	 * the data it has received from the peripheral.
2181 	 */
2182 	if (uc->config.pkt_mode || !uc->cyclic) {
2183 		unsigned int div = dev_width * elcnt;
2184 
2185 		if (uc->cyclic)
2186 			d->static_tr.bstcnt = d->residue / d->sglen / div;
2187 		else
2188 			d->static_tr.bstcnt = d->residue / div;
2189 
2190 		if (uc->config.dir == DMA_DEV_TO_MEM &&
2191 		    d->static_tr.bstcnt > uc->ud->match_data->statictr_z_mask)
2192 			return -EINVAL;
2193 	} else {
2194 		d->static_tr.bstcnt = 0;
2195 	}
2196 
2197 	return 0;
2198 }
2199 
2200 static struct udma_desc *
2201 udma_prep_slave_sg_pkt(struct udma_chan *uc, struct scatterlist *sgl,
2202 		       unsigned int sglen, enum dma_transfer_direction dir,
2203 		       unsigned long tx_flags, void *context)
2204 {
2205 	struct scatterlist *sgent;
2206 	struct cppi5_host_desc_t *h_desc = NULL;
2207 	struct udma_desc *d;
2208 	u32 ring_id;
2209 	unsigned int i;
2210 
2211 	d = kzalloc(sizeof(*d) + sglen * sizeof(d->hwdesc[0]), GFP_NOWAIT);
2212 	if (!d)
2213 		return NULL;
2214 
2215 	d->sglen = sglen;
2216 	d->hwdesc_count = sglen;
2217 
2218 	if (dir == DMA_DEV_TO_MEM)
2219 		ring_id = k3_ringacc_get_ring_id(uc->rflow->r_ring);
2220 	else
2221 		ring_id = k3_ringacc_get_ring_id(uc->tchan->tc_ring);
2222 
2223 	for_each_sg(sgl, sgent, sglen, i) {
2224 		struct udma_hwdesc *hwdesc = &d->hwdesc[i];
2225 		dma_addr_t sg_addr = sg_dma_address(sgent);
2226 		struct cppi5_host_desc_t *desc;
2227 		size_t sg_len = sg_dma_len(sgent);
2228 
2229 		hwdesc->cppi5_desc_vaddr = dma_pool_zalloc(uc->hdesc_pool,
2230 						GFP_NOWAIT,
2231 						&hwdesc->cppi5_desc_paddr);
2232 		if (!hwdesc->cppi5_desc_vaddr) {
2233 			dev_err(uc->ud->dev,
2234 				"descriptor%d allocation failed\n", i);
2235 
2236 			udma_free_hwdesc(uc, d);
2237 			kfree(d);
2238 			return NULL;
2239 		}
2240 
2241 		d->residue += sg_len;
2242 		hwdesc->cppi5_desc_size = uc->config.hdesc_size;
2243 		desc = hwdesc->cppi5_desc_vaddr;
2244 
2245 		if (i == 0) {
2246 			cppi5_hdesc_init(desc, 0, 0);
2247 			/* Flow and Packed ID */
2248 			cppi5_desc_set_pktids(&desc->hdr, uc->id,
2249 					      CPPI5_INFO1_DESC_FLOWID_DEFAULT);
2250 			cppi5_desc_set_retpolicy(&desc->hdr, 0, ring_id);
2251 		} else {
2252 			cppi5_hdesc_reset_hbdesc(desc);
2253 			cppi5_desc_set_retpolicy(&desc->hdr, 0, 0xffff);
2254 		}
2255 
2256 		/* attach the sg buffer to the descriptor */
2257 		cppi5_hdesc_attach_buf(desc, sg_addr, sg_len, sg_addr, sg_len);
2258 
2259 		/* Attach link as host buffer descriptor */
2260 		if (h_desc)
2261 			cppi5_hdesc_link_hbdesc(h_desc,
2262 						hwdesc->cppi5_desc_paddr);
2263 
2264 		if (dir == DMA_MEM_TO_DEV)
2265 			h_desc = desc;
2266 	}
2267 
2268 	if (d->residue >= SZ_4M) {
2269 		dev_err(uc->ud->dev,
2270 			"%s: Transfer size %u is over the supported 4M range\n",
2271 			__func__, d->residue);
2272 		udma_free_hwdesc(uc, d);
2273 		kfree(d);
2274 		return NULL;
2275 	}
2276 
2277 	h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2278 	cppi5_hdesc_set_pktlen(h_desc, d->residue);
2279 
2280 	return d;
2281 }
2282 
2283 static int udma_attach_metadata(struct dma_async_tx_descriptor *desc,
2284 				void *data, size_t len)
2285 {
2286 	struct udma_desc *d = to_udma_desc(desc);
2287 	struct udma_chan *uc = to_udma_chan(desc->chan);
2288 	struct cppi5_host_desc_t *h_desc;
2289 	u32 psd_size = len;
2290 	u32 flags = 0;
2291 
2292 	if (!uc->config.pkt_mode || !uc->config.metadata_size)
2293 		return -ENOTSUPP;
2294 
2295 	if (!data || len > uc->config.metadata_size)
2296 		return -EINVAL;
2297 
2298 	if (uc->config.needs_epib && len < CPPI5_INFO0_HDESC_EPIB_SIZE)
2299 		return -EINVAL;
2300 
2301 	h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2302 	if (d->dir == DMA_MEM_TO_DEV)
2303 		memcpy(h_desc->epib, data, len);
2304 
2305 	if (uc->config.needs_epib)
2306 		psd_size -= CPPI5_INFO0_HDESC_EPIB_SIZE;
2307 
2308 	d->metadata = data;
2309 	d->metadata_size = len;
2310 	if (uc->config.needs_epib)
2311 		flags |= CPPI5_INFO0_HDESC_EPIB_PRESENT;
2312 
2313 	cppi5_hdesc_update_flags(h_desc, flags);
2314 	cppi5_hdesc_update_psdata_size(h_desc, psd_size);
2315 
2316 	return 0;
2317 }
2318 
2319 static void *udma_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
2320 				   size_t *payload_len, size_t *max_len)
2321 {
2322 	struct udma_desc *d = to_udma_desc(desc);
2323 	struct udma_chan *uc = to_udma_chan(desc->chan);
2324 	struct cppi5_host_desc_t *h_desc;
2325 
2326 	if (!uc->config.pkt_mode || !uc->config.metadata_size)
2327 		return ERR_PTR(-ENOTSUPP);
2328 
2329 	h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2330 
2331 	*max_len = uc->config.metadata_size;
2332 
2333 	*payload_len = cppi5_hdesc_epib_present(&h_desc->hdr) ?
2334 		       CPPI5_INFO0_HDESC_EPIB_SIZE : 0;
2335 	*payload_len += cppi5_hdesc_get_psdata_size(h_desc);
2336 
2337 	return h_desc->epib;
2338 }
2339 
2340 static int udma_set_metadata_len(struct dma_async_tx_descriptor *desc,
2341 				 size_t payload_len)
2342 {
2343 	struct udma_desc *d = to_udma_desc(desc);
2344 	struct udma_chan *uc = to_udma_chan(desc->chan);
2345 	struct cppi5_host_desc_t *h_desc;
2346 	u32 psd_size = payload_len;
2347 	u32 flags = 0;
2348 
2349 	if (!uc->config.pkt_mode || !uc->config.metadata_size)
2350 		return -ENOTSUPP;
2351 
2352 	if (payload_len > uc->config.metadata_size)
2353 		return -EINVAL;
2354 
2355 	if (uc->config.needs_epib && payload_len < CPPI5_INFO0_HDESC_EPIB_SIZE)
2356 		return -EINVAL;
2357 
2358 	h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2359 
2360 	if (uc->config.needs_epib) {
2361 		psd_size -= CPPI5_INFO0_HDESC_EPIB_SIZE;
2362 		flags |= CPPI5_INFO0_HDESC_EPIB_PRESENT;
2363 	}
2364 
2365 	cppi5_hdesc_update_flags(h_desc, flags);
2366 	cppi5_hdesc_update_psdata_size(h_desc, psd_size);
2367 
2368 	return 0;
2369 }
2370 
2371 static struct dma_descriptor_metadata_ops metadata_ops = {
2372 	.attach = udma_attach_metadata,
2373 	.get_ptr = udma_get_metadata_ptr,
2374 	.set_len = udma_set_metadata_len,
2375 };
2376 
2377 static struct dma_async_tx_descriptor *
2378 udma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2379 		   unsigned int sglen, enum dma_transfer_direction dir,
2380 		   unsigned long tx_flags, void *context)
2381 {
2382 	struct udma_chan *uc = to_udma_chan(chan);
2383 	enum dma_slave_buswidth dev_width;
2384 	struct udma_desc *d;
2385 	u32 burst;
2386 
2387 	if (dir != uc->config.dir) {
2388 		dev_err(chan->device->dev,
2389 			"%s: chan%d is for %s, not supporting %s\n",
2390 			__func__, uc->id,
2391 			dmaengine_get_direction_text(uc->config.dir),
2392 			dmaengine_get_direction_text(dir));
2393 		return NULL;
2394 	}
2395 
2396 	if (dir == DMA_DEV_TO_MEM) {
2397 		dev_width = uc->cfg.src_addr_width;
2398 		burst = uc->cfg.src_maxburst;
2399 	} else if (dir == DMA_MEM_TO_DEV) {
2400 		dev_width = uc->cfg.dst_addr_width;
2401 		burst = uc->cfg.dst_maxburst;
2402 	} else {
2403 		dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
2404 		return NULL;
2405 	}
2406 
2407 	if (!burst)
2408 		burst = 1;
2409 
2410 	if (uc->config.pkt_mode)
2411 		d = udma_prep_slave_sg_pkt(uc, sgl, sglen, dir, tx_flags,
2412 					   context);
2413 	else
2414 		d = udma_prep_slave_sg_tr(uc, sgl, sglen, dir, tx_flags,
2415 					  context);
2416 
2417 	if (!d)
2418 		return NULL;
2419 
2420 	d->dir = dir;
2421 	d->desc_idx = 0;
2422 	d->tr_idx = 0;
2423 
2424 	/* static TR for remote PDMA */
2425 	if (udma_configure_statictr(uc, d, dev_width, burst)) {
2426 		dev_err(uc->ud->dev,
2427 			"%s: StaticTR Z is limited to maximum 4095 (%u)\n",
2428 			__func__, d->static_tr.bstcnt);
2429 
2430 		udma_free_hwdesc(uc, d);
2431 		kfree(d);
2432 		return NULL;
2433 	}
2434 
2435 	if (uc->config.metadata_size)
2436 		d->vd.tx.metadata_ops = &metadata_ops;
2437 
2438 	return vchan_tx_prep(&uc->vc, &d->vd, tx_flags);
2439 }
2440 
2441 static struct udma_desc *
2442 udma_prep_dma_cyclic_tr(struct udma_chan *uc, dma_addr_t buf_addr,
2443 			size_t buf_len, size_t period_len,
2444 			enum dma_transfer_direction dir, unsigned long flags)
2445 {
2446 	struct udma_desc *d;
2447 	size_t tr_size, period_addr;
2448 	struct cppi5_tr_type1_t *tr_req;
2449 	unsigned int periods = buf_len / period_len;
2450 	u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
2451 	unsigned int i;
2452 	int num_tr;
2453 
2454 	if (!is_slave_direction(dir)) {
2455 		dev_err(uc->ud->dev, "Only slave cyclic is supported\n");
2456 		return NULL;
2457 	}
2458 
2459 	num_tr = udma_get_tr_counters(period_len, __ffs(buf_addr), &tr0_cnt0,
2460 				      &tr0_cnt1, &tr1_cnt0);
2461 	if (num_tr < 0) {
2462 		dev_err(uc->ud->dev, "size %zu is not supported\n",
2463 			period_len);
2464 		return NULL;
2465 	}
2466 
2467 	/* Now allocate and setup the descriptor. */
2468 	tr_size = sizeof(struct cppi5_tr_type1_t);
2469 	d = udma_alloc_tr_desc(uc, tr_size, periods * num_tr, dir);
2470 	if (!d)
2471 		return NULL;
2472 
2473 	tr_req = d->hwdesc[0].tr_req_base;
2474 	period_addr = buf_addr;
2475 	for (i = 0; i < periods; i++) {
2476 		int tr_idx = i * num_tr;
2477 
2478 		cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1, false,
2479 			      false, CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2480 
2481 		tr_req[tr_idx].addr = period_addr;
2482 		tr_req[tr_idx].icnt0 = tr0_cnt0;
2483 		tr_req[tr_idx].icnt1 = tr0_cnt1;
2484 		tr_req[tr_idx].dim1 = tr0_cnt0;
2485 
2486 		if (num_tr == 2) {
2487 			cppi5_tr_csf_set(&tr_req[tr_idx].flags,
2488 					 CPPI5_TR_CSF_SUPR_EVT);
2489 			tr_idx++;
2490 
2491 			cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1,
2492 				      false, false,
2493 				      CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2494 
2495 			tr_req[tr_idx].addr = period_addr + tr0_cnt1 * tr0_cnt0;
2496 			tr_req[tr_idx].icnt0 = tr1_cnt0;
2497 			tr_req[tr_idx].icnt1 = 1;
2498 			tr_req[tr_idx].dim1 = tr1_cnt0;
2499 		}
2500 
2501 		if (!(flags & DMA_PREP_INTERRUPT))
2502 			cppi5_tr_csf_set(&tr_req[tr_idx].flags,
2503 					 CPPI5_TR_CSF_SUPR_EVT);
2504 
2505 		period_addr += period_len;
2506 	}
2507 
2508 	return d;
2509 }
2510 
2511 static struct udma_desc *
2512 udma_prep_dma_cyclic_pkt(struct udma_chan *uc, dma_addr_t buf_addr,
2513 			 size_t buf_len, size_t period_len,
2514 			 enum dma_transfer_direction dir, unsigned long flags)
2515 {
2516 	struct udma_desc *d;
2517 	u32 ring_id;
2518 	int i;
2519 	int periods = buf_len / period_len;
2520 
2521 	if (periods > (K3_UDMA_DEFAULT_RING_SIZE - 1))
2522 		return NULL;
2523 
2524 	if (period_len >= SZ_4M)
2525 		return NULL;
2526 
2527 	d = kzalloc(sizeof(*d) + periods * sizeof(d->hwdesc[0]), GFP_NOWAIT);
2528 	if (!d)
2529 		return NULL;
2530 
2531 	d->hwdesc_count = periods;
2532 
2533 	/* TODO: re-check this... */
2534 	if (dir == DMA_DEV_TO_MEM)
2535 		ring_id = k3_ringacc_get_ring_id(uc->rflow->r_ring);
2536 	else
2537 		ring_id = k3_ringacc_get_ring_id(uc->tchan->tc_ring);
2538 
2539 	for (i = 0; i < periods; i++) {
2540 		struct udma_hwdesc *hwdesc = &d->hwdesc[i];
2541 		dma_addr_t period_addr = buf_addr + (period_len * i);
2542 		struct cppi5_host_desc_t *h_desc;
2543 
2544 		hwdesc->cppi5_desc_vaddr = dma_pool_zalloc(uc->hdesc_pool,
2545 						GFP_NOWAIT,
2546 						&hwdesc->cppi5_desc_paddr);
2547 		if (!hwdesc->cppi5_desc_vaddr) {
2548 			dev_err(uc->ud->dev,
2549 				"descriptor%d allocation failed\n", i);
2550 
2551 			udma_free_hwdesc(uc, d);
2552 			kfree(d);
2553 			return NULL;
2554 		}
2555 
2556 		hwdesc->cppi5_desc_size = uc->config.hdesc_size;
2557 		h_desc = hwdesc->cppi5_desc_vaddr;
2558 
2559 		cppi5_hdesc_init(h_desc, 0, 0);
2560 		cppi5_hdesc_set_pktlen(h_desc, period_len);
2561 
2562 		/* Flow and Packed ID */
2563 		cppi5_desc_set_pktids(&h_desc->hdr, uc->id,
2564 				      CPPI5_INFO1_DESC_FLOWID_DEFAULT);
2565 		cppi5_desc_set_retpolicy(&h_desc->hdr, 0, ring_id);
2566 
2567 		/* attach each period to a new descriptor */
2568 		cppi5_hdesc_attach_buf(h_desc,
2569 				       period_addr, period_len,
2570 				       period_addr, period_len);
2571 	}
2572 
2573 	return d;
2574 }
2575 
2576 static struct dma_async_tx_descriptor *
2577 udma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
2578 		     size_t period_len, enum dma_transfer_direction dir,
2579 		     unsigned long flags)
2580 {
2581 	struct udma_chan *uc = to_udma_chan(chan);
2582 	enum dma_slave_buswidth dev_width;
2583 	struct udma_desc *d;
2584 	u32 burst;
2585 
2586 	if (dir != uc->config.dir) {
2587 		dev_err(chan->device->dev,
2588 			"%s: chan%d is for %s, not supporting %s\n",
2589 			__func__, uc->id,
2590 			dmaengine_get_direction_text(uc->config.dir),
2591 			dmaengine_get_direction_text(dir));
2592 		return NULL;
2593 	}
2594 
2595 	uc->cyclic = true;
2596 
2597 	if (dir == DMA_DEV_TO_MEM) {
2598 		dev_width = uc->cfg.src_addr_width;
2599 		burst = uc->cfg.src_maxburst;
2600 	} else if (dir == DMA_MEM_TO_DEV) {
2601 		dev_width = uc->cfg.dst_addr_width;
2602 		burst = uc->cfg.dst_maxburst;
2603 	} else {
2604 		dev_err(uc->ud->dev, "%s: bad direction?\n", __func__);
2605 		return NULL;
2606 	}
2607 
2608 	if (!burst)
2609 		burst = 1;
2610 
2611 	if (uc->config.pkt_mode)
2612 		d = udma_prep_dma_cyclic_pkt(uc, buf_addr, buf_len, period_len,
2613 					     dir, flags);
2614 	else
2615 		d = udma_prep_dma_cyclic_tr(uc, buf_addr, buf_len, period_len,
2616 					    dir, flags);
2617 
2618 	if (!d)
2619 		return NULL;
2620 
2621 	d->sglen = buf_len / period_len;
2622 
2623 	d->dir = dir;
2624 	d->residue = buf_len;
2625 
2626 	/* static TR for remote PDMA */
2627 	if (udma_configure_statictr(uc, d, dev_width, burst)) {
2628 		dev_err(uc->ud->dev,
2629 			"%s: StaticTR Z is limited to maximum 4095 (%u)\n",
2630 			__func__, d->static_tr.bstcnt);
2631 
2632 		udma_free_hwdesc(uc, d);
2633 		kfree(d);
2634 		return NULL;
2635 	}
2636 
2637 	if (uc->config.metadata_size)
2638 		d->vd.tx.metadata_ops = &metadata_ops;
2639 
2640 	return vchan_tx_prep(&uc->vc, &d->vd, flags);
2641 }
2642 
2643 static struct dma_async_tx_descriptor *
2644 udma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
2645 		     size_t len, unsigned long tx_flags)
2646 {
2647 	struct udma_chan *uc = to_udma_chan(chan);
2648 	struct udma_desc *d;
2649 	struct cppi5_tr_type15_t *tr_req;
2650 	int num_tr;
2651 	size_t tr_size = sizeof(struct cppi5_tr_type15_t);
2652 	u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
2653 
2654 	if (uc->config.dir != DMA_MEM_TO_MEM) {
2655 		dev_err(chan->device->dev,
2656 			"%s: chan%d is for %s, not supporting %s\n",
2657 			__func__, uc->id,
2658 			dmaengine_get_direction_text(uc->config.dir),
2659 			dmaengine_get_direction_text(DMA_MEM_TO_MEM));
2660 		return NULL;
2661 	}
2662 
2663 	num_tr = udma_get_tr_counters(len, __ffs(src | dest), &tr0_cnt0,
2664 				      &tr0_cnt1, &tr1_cnt0);
2665 	if (num_tr < 0) {
2666 		dev_err(uc->ud->dev, "size %zu is not supported\n",
2667 			len);
2668 		return NULL;
2669 	}
2670 
2671 	d = udma_alloc_tr_desc(uc, tr_size, num_tr, DMA_MEM_TO_MEM);
2672 	if (!d)
2673 		return NULL;
2674 
2675 	d->dir = DMA_MEM_TO_MEM;
2676 	d->desc_idx = 0;
2677 	d->tr_idx = 0;
2678 	d->residue = len;
2679 
2680 	tr_req = d->hwdesc[0].tr_req_base;
2681 
2682 	cppi5_tr_init(&tr_req[0].flags, CPPI5_TR_TYPE15, false, true,
2683 		      CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2684 	cppi5_tr_csf_set(&tr_req[0].flags, CPPI5_TR_CSF_SUPR_EVT);
2685 
2686 	tr_req[0].addr = src;
2687 	tr_req[0].icnt0 = tr0_cnt0;
2688 	tr_req[0].icnt1 = tr0_cnt1;
2689 	tr_req[0].icnt2 = 1;
2690 	tr_req[0].icnt3 = 1;
2691 	tr_req[0].dim1 = tr0_cnt0;
2692 
2693 	tr_req[0].daddr = dest;
2694 	tr_req[0].dicnt0 = tr0_cnt0;
2695 	tr_req[0].dicnt1 = tr0_cnt1;
2696 	tr_req[0].dicnt2 = 1;
2697 	tr_req[0].dicnt3 = 1;
2698 	tr_req[0].ddim1 = tr0_cnt0;
2699 
2700 	if (num_tr == 2) {
2701 		cppi5_tr_init(&tr_req[1].flags, CPPI5_TR_TYPE15, false, true,
2702 			      CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2703 		cppi5_tr_csf_set(&tr_req[1].flags, CPPI5_TR_CSF_SUPR_EVT);
2704 
2705 		tr_req[1].addr = src + tr0_cnt1 * tr0_cnt0;
2706 		tr_req[1].icnt0 = tr1_cnt0;
2707 		tr_req[1].icnt1 = 1;
2708 		tr_req[1].icnt2 = 1;
2709 		tr_req[1].icnt3 = 1;
2710 
2711 		tr_req[1].daddr = dest + tr0_cnt1 * tr0_cnt0;
2712 		tr_req[1].dicnt0 = tr1_cnt0;
2713 		tr_req[1].dicnt1 = 1;
2714 		tr_req[1].dicnt2 = 1;
2715 		tr_req[1].dicnt3 = 1;
2716 	}
2717 
2718 	cppi5_tr_csf_set(&tr_req[num_tr - 1].flags,
2719 			 CPPI5_TR_CSF_SUPR_EVT | CPPI5_TR_CSF_EOP);
2720 
2721 	if (uc->config.metadata_size)
2722 		d->vd.tx.metadata_ops = &metadata_ops;
2723 
2724 	return vchan_tx_prep(&uc->vc, &d->vd, tx_flags);
2725 }
2726 
2727 static void udma_issue_pending(struct dma_chan *chan)
2728 {
2729 	struct udma_chan *uc = to_udma_chan(chan);
2730 	unsigned long flags;
2731 
2732 	spin_lock_irqsave(&uc->vc.lock, flags);
2733 
2734 	/* If we have something pending and no active descriptor, then */
2735 	if (vchan_issue_pending(&uc->vc) && !uc->desc) {
2736 		/*
2737 		 * start a descriptor if the channel is NOT [marked as
2738 		 * terminating _and_ it is still running (teardown has not
2739 		 * completed yet)].
2740 		 */
2741 		if (!(uc->state == UDMA_CHAN_IS_TERMINATING &&
2742 		      udma_is_chan_running(uc)))
2743 			udma_start(uc);
2744 	}
2745 
2746 	spin_unlock_irqrestore(&uc->vc.lock, flags);
2747 }
2748 
2749 static enum dma_status udma_tx_status(struct dma_chan *chan,
2750 				      dma_cookie_t cookie,
2751 				      struct dma_tx_state *txstate)
2752 {
2753 	struct udma_chan *uc = to_udma_chan(chan);
2754 	enum dma_status ret;
2755 	unsigned long flags;
2756 
2757 	spin_lock_irqsave(&uc->vc.lock, flags);
2758 
2759 	ret = dma_cookie_status(chan, cookie, txstate);
2760 
2761 	if (!udma_is_chan_running(uc))
2762 		ret = DMA_COMPLETE;
2763 
2764 	if (ret == DMA_IN_PROGRESS && udma_is_chan_paused(uc))
2765 		ret = DMA_PAUSED;
2766 
2767 	if (ret == DMA_COMPLETE || !txstate)
2768 		goto out;
2769 
2770 	if (uc->desc && uc->desc->vd.tx.cookie == cookie) {
2771 		u32 peer_bcnt = 0;
2772 		u32 bcnt = 0;
2773 		u32 residue = uc->desc->residue;
2774 		u32 delay = 0;
2775 
2776 		if (uc->desc->dir == DMA_MEM_TO_DEV) {
2777 			bcnt = udma_tchanrt_read(uc->tchan,
2778 						 UDMA_TCHAN_RT_SBCNT_REG);
2779 
2780 			if (uc->config.ep_type != PSIL_EP_NATIVE) {
2781 				peer_bcnt = udma_tchanrt_read(uc->tchan,
2782 						UDMA_TCHAN_RT_PEER_BCNT_REG);
2783 
2784 				if (bcnt > peer_bcnt)
2785 					delay = bcnt - peer_bcnt;
2786 			}
2787 		} else if (uc->desc->dir == DMA_DEV_TO_MEM) {
2788 			bcnt = udma_rchanrt_read(uc->rchan,
2789 						 UDMA_RCHAN_RT_BCNT_REG);
2790 
2791 			if (uc->config.ep_type != PSIL_EP_NATIVE) {
2792 				peer_bcnt = udma_rchanrt_read(uc->rchan,
2793 						UDMA_RCHAN_RT_PEER_BCNT_REG);
2794 
2795 				if (peer_bcnt > bcnt)
2796 					delay = peer_bcnt - bcnt;
2797 			}
2798 		} else {
2799 			bcnt = udma_tchanrt_read(uc->tchan,
2800 						 UDMA_TCHAN_RT_BCNT_REG);
2801 		}
2802 
2803 		bcnt -= uc->bcnt;
2804 		if (bcnt && !(bcnt % uc->desc->residue))
2805 			residue = 0;
2806 		else
2807 			residue -= bcnt % uc->desc->residue;
2808 
2809 		if (!residue && (uc->config.dir == DMA_DEV_TO_MEM || !delay)) {
2810 			ret = DMA_COMPLETE;
2811 			delay = 0;
2812 		}
2813 
2814 		dma_set_residue(txstate, residue);
2815 		dma_set_in_flight_bytes(txstate, delay);
2816 
2817 	} else {
2818 		ret = DMA_COMPLETE;
2819 	}
2820 
2821 out:
2822 	spin_unlock_irqrestore(&uc->vc.lock, flags);
2823 	return ret;
2824 }
2825 
2826 static int udma_pause(struct dma_chan *chan)
2827 {
2828 	struct udma_chan *uc = to_udma_chan(chan);
2829 
2830 	/* pause the channel */
2831 	switch (uc->config.dir) {
2832 	case DMA_DEV_TO_MEM:
2833 		udma_rchanrt_update_bits(uc->rchan,
2834 					 UDMA_RCHAN_RT_PEER_RT_EN_REG,
2835 					 UDMA_PEER_RT_EN_PAUSE,
2836 					 UDMA_PEER_RT_EN_PAUSE);
2837 		break;
2838 	case DMA_MEM_TO_DEV:
2839 		udma_tchanrt_update_bits(uc->tchan,
2840 					 UDMA_TCHAN_RT_PEER_RT_EN_REG,
2841 					 UDMA_PEER_RT_EN_PAUSE,
2842 					 UDMA_PEER_RT_EN_PAUSE);
2843 		break;
2844 	case DMA_MEM_TO_MEM:
2845 		udma_tchanrt_update_bits(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
2846 					 UDMA_CHAN_RT_CTL_PAUSE,
2847 					 UDMA_CHAN_RT_CTL_PAUSE);
2848 		break;
2849 	default:
2850 		return -EINVAL;
2851 	}
2852 
2853 	return 0;
2854 }
2855 
2856 static int udma_resume(struct dma_chan *chan)
2857 {
2858 	struct udma_chan *uc = to_udma_chan(chan);
2859 
2860 	/* resume the channel */
2861 	switch (uc->config.dir) {
2862 	case DMA_DEV_TO_MEM:
2863 		udma_rchanrt_update_bits(uc->rchan,
2864 					 UDMA_RCHAN_RT_PEER_RT_EN_REG,
2865 					 UDMA_PEER_RT_EN_PAUSE, 0);
2866 
2867 		break;
2868 	case DMA_MEM_TO_DEV:
2869 		udma_tchanrt_update_bits(uc->tchan,
2870 					 UDMA_TCHAN_RT_PEER_RT_EN_REG,
2871 					 UDMA_PEER_RT_EN_PAUSE, 0);
2872 		break;
2873 	case DMA_MEM_TO_MEM:
2874 		udma_tchanrt_update_bits(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
2875 					 UDMA_CHAN_RT_CTL_PAUSE, 0);
2876 		break;
2877 	default:
2878 		return -EINVAL;
2879 	}
2880 
2881 	return 0;
2882 }
2883 
2884 static int udma_terminate_all(struct dma_chan *chan)
2885 {
2886 	struct udma_chan *uc = to_udma_chan(chan);
2887 	unsigned long flags;
2888 	LIST_HEAD(head);
2889 
2890 	spin_lock_irqsave(&uc->vc.lock, flags);
2891 
2892 	if (udma_is_chan_running(uc))
2893 		udma_stop(uc);
2894 
2895 	if (uc->desc) {
2896 		uc->terminated_desc = uc->desc;
2897 		uc->desc = NULL;
2898 		uc->terminated_desc->terminated = true;
2899 		cancel_delayed_work(&uc->tx_drain.work);
2900 	}
2901 
2902 	uc->paused = false;
2903 
2904 	vchan_get_all_descriptors(&uc->vc, &head);
2905 	spin_unlock_irqrestore(&uc->vc.lock, flags);
2906 	vchan_dma_desc_free_list(&uc->vc, &head);
2907 
2908 	return 0;
2909 }
2910 
2911 static void udma_synchronize(struct dma_chan *chan)
2912 {
2913 	struct udma_chan *uc = to_udma_chan(chan);
2914 	unsigned long timeout = msecs_to_jiffies(1000);
2915 
2916 	vchan_synchronize(&uc->vc);
2917 
2918 	if (uc->state == UDMA_CHAN_IS_TERMINATING) {
2919 		timeout = wait_for_completion_timeout(&uc->teardown_completed,
2920 						      timeout);
2921 		if (!timeout) {
2922 			dev_warn(uc->ud->dev, "chan%d teardown timeout!\n",
2923 				 uc->id);
2924 			udma_dump_chan_stdata(uc);
2925 			udma_reset_chan(uc, true);
2926 		}
2927 	}
2928 
2929 	udma_reset_chan(uc, false);
2930 	if (udma_is_chan_running(uc))
2931 		dev_warn(uc->ud->dev, "chan%d refused to stop!\n", uc->id);
2932 
2933 	cancel_delayed_work_sync(&uc->tx_drain.work);
2934 	udma_reset_rings(uc);
2935 }
2936 
2937 static void udma_desc_pre_callback(struct virt_dma_chan *vc,
2938 				   struct virt_dma_desc *vd,
2939 				   struct dmaengine_result *result)
2940 {
2941 	struct udma_chan *uc = to_udma_chan(&vc->chan);
2942 	struct udma_desc *d;
2943 
2944 	if (!vd)
2945 		return;
2946 
2947 	d = to_udma_desc(&vd->tx);
2948 
2949 	if (d->metadata_size)
2950 		udma_fetch_epib(uc, d);
2951 
2952 	/* Provide residue information for the client */
2953 	if (result) {
2954 		void *desc_vaddr = udma_curr_cppi5_desc_vaddr(d, d->desc_idx);
2955 
2956 		if (cppi5_desc_get_type(desc_vaddr) ==
2957 		    CPPI5_INFO0_DESC_TYPE_VAL_HOST) {
2958 			result->residue = d->residue -
2959 					  cppi5_hdesc_get_pktlen(desc_vaddr);
2960 			if (result->residue)
2961 				result->result = DMA_TRANS_ABORTED;
2962 			else
2963 				result->result = DMA_TRANS_NOERROR;
2964 		} else {
2965 			result->residue = 0;
2966 			result->result = DMA_TRANS_NOERROR;
2967 		}
2968 	}
2969 }
2970 
2971 /*
2972  * This tasklet handles the completion of a DMA descriptor by
2973  * calling its callback and freeing it.
2974  */
2975 static void udma_vchan_complete(unsigned long arg)
2976 {
2977 	struct virt_dma_chan *vc = (struct virt_dma_chan *)arg;
2978 	struct virt_dma_desc *vd, *_vd;
2979 	struct dmaengine_desc_callback cb;
2980 	LIST_HEAD(head);
2981 
2982 	spin_lock_irq(&vc->lock);
2983 	list_splice_tail_init(&vc->desc_completed, &head);
2984 	vd = vc->cyclic;
2985 	if (vd) {
2986 		vc->cyclic = NULL;
2987 		dmaengine_desc_get_callback(&vd->tx, &cb);
2988 	} else {
2989 		memset(&cb, 0, sizeof(cb));
2990 	}
2991 	spin_unlock_irq(&vc->lock);
2992 
2993 	udma_desc_pre_callback(vc, vd, NULL);
2994 	dmaengine_desc_callback_invoke(&cb, NULL);
2995 
2996 	list_for_each_entry_safe(vd, _vd, &head, node) {
2997 		struct dmaengine_result result;
2998 
2999 		dmaengine_desc_get_callback(&vd->tx, &cb);
3000 
3001 		list_del(&vd->node);
3002 
3003 		udma_desc_pre_callback(vc, vd, &result);
3004 		dmaengine_desc_callback_invoke(&cb, &result);
3005 
3006 		vchan_vdesc_fini(vd);
3007 	}
3008 }
3009 
3010 static void udma_free_chan_resources(struct dma_chan *chan)
3011 {
3012 	struct udma_chan *uc = to_udma_chan(chan);
3013 	struct udma_dev *ud = to_udma_dev(chan->device);
3014 
3015 	udma_terminate_all(chan);
3016 	if (uc->terminated_desc) {
3017 		udma_reset_chan(uc, false);
3018 		udma_reset_rings(uc);
3019 	}
3020 
3021 	cancel_delayed_work_sync(&uc->tx_drain.work);
3022 	destroy_delayed_work_on_stack(&uc->tx_drain.work);
3023 
3024 	if (uc->irq_num_ring > 0) {
3025 		free_irq(uc->irq_num_ring, uc);
3026 
3027 		uc->irq_num_ring = 0;
3028 	}
3029 	if (uc->irq_num_udma > 0) {
3030 		free_irq(uc->irq_num_udma, uc);
3031 
3032 		uc->irq_num_udma = 0;
3033 	}
3034 
3035 	/* Release PSI-L pairing */
3036 	if (uc->psil_paired) {
3037 		navss_psil_unpair(ud, uc->config.src_thread,
3038 				  uc->config.dst_thread);
3039 		uc->psil_paired = false;
3040 	}
3041 
3042 	vchan_free_chan_resources(&uc->vc);
3043 	tasklet_kill(&uc->vc.task);
3044 
3045 	udma_free_tx_resources(uc);
3046 	udma_free_rx_resources(uc);
3047 	udma_reset_uchan(uc);
3048 
3049 	if (uc->use_dma_pool) {
3050 		dma_pool_destroy(uc->hdesc_pool);
3051 		uc->use_dma_pool = false;
3052 	}
3053 }
3054 
3055 static struct platform_driver udma_driver;
3056 
3057 struct udma_filter_param {
3058 	int remote_thread_id;
3059 	u32 atype;
3060 };
3061 
3062 static bool udma_dma_filter_fn(struct dma_chan *chan, void *param)
3063 {
3064 	struct udma_chan_config *ucc;
3065 	struct psil_endpoint_config *ep_config;
3066 	struct udma_filter_param *filter_param;
3067 	struct udma_chan *uc;
3068 	struct udma_dev *ud;
3069 
3070 	if (chan->device->dev->driver != &udma_driver.driver)
3071 		return false;
3072 
3073 	uc = to_udma_chan(chan);
3074 	ucc = &uc->config;
3075 	ud = uc->ud;
3076 	filter_param = param;
3077 
3078 	if (filter_param->atype > 2) {
3079 		dev_err(ud->dev, "Invalid channel atype: %u\n",
3080 			filter_param->atype);
3081 		return false;
3082 	}
3083 
3084 	ucc->remote_thread_id = filter_param->remote_thread_id;
3085 	ucc->atype = filter_param->atype;
3086 
3087 	if (ucc->remote_thread_id & K3_PSIL_DST_THREAD_ID_OFFSET)
3088 		ucc->dir = DMA_MEM_TO_DEV;
3089 	else
3090 		ucc->dir = DMA_DEV_TO_MEM;
3091 
3092 	ep_config = psil_get_ep_config(ucc->remote_thread_id);
3093 	if (IS_ERR(ep_config)) {
3094 		dev_err(ud->dev, "No configuration for psi-l thread 0x%04x\n",
3095 			ucc->remote_thread_id);
3096 		ucc->dir = DMA_MEM_TO_MEM;
3097 		ucc->remote_thread_id = -1;
3098 		ucc->atype = 0;
3099 		return false;
3100 	}
3101 
3102 	ucc->pkt_mode = ep_config->pkt_mode;
3103 	ucc->channel_tpl = ep_config->channel_tpl;
3104 	ucc->notdpkt = ep_config->notdpkt;
3105 	ucc->ep_type = ep_config->ep_type;
3106 
3107 	if (ucc->ep_type != PSIL_EP_NATIVE) {
3108 		const struct udma_match_data *match_data = ud->match_data;
3109 
3110 		if (match_data->flags & UDMA_FLAG_PDMA_ACC32)
3111 			ucc->enable_acc32 = ep_config->pdma_acc32;
3112 		if (match_data->flags & UDMA_FLAG_PDMA_BURST)
3113 			ucc->enable_burst = ep_config->pdma_burst;
3114 	}
3115 
3116 	ucc->needs_epib = ep_config->needs_epib;
3117 	ucc->psd_size = ep_config->psd_size;
3118 	ucc->metadata_size =
3119 			(ucc->needs_epib ? CPPI5_INFO0_HDESC_EPIB_SIZE : 0) +
3120 			ucc->psd_size;
3121 
3122 	if (ucc->pkt_mode)
3123 		ucc->hdesc_size = ALIGN(sizeof(struct cppi5_host_desc_t) +
3124 				 ucc->metadata_size, ud->desc_align);
3125 
3126 	dev_dbg(ud->dev, "chan%d: Remote thread: 0x%04x (%s)\n", uc->id,
3127 		ucc->remote_thread_id, dmaengine_get_direction_text(ucc->dir));
3128 
3129 	return true;
3130 }
3131 
3132 static struct dma_chan *udma_of_xlate(struct of_phandle_args *dma_spec,
3133 				      struct of_dma *ofdma)
3134 {
3135 	struct udma_dev *ud = ofdma->of_dma_data;
3136 	dma_cap_mask_t mask = ud->ddev.cap_mask;
3137 	struct udma_filter_param filter_param;
3138 	struct dma_chan *chan;
3139 
3140 	if (dma_spec->args_count != 1 && dma_spec->args_count != 2)
3141 		return NULL;
3142 
3143 	filter_param.remote_thread_id = dma_spec->args[0];
3144 	if (dma_spec->args_count == 2)
3145 		filter_param.atype = dma_spec->args[1];
3146 	else
3147 		filter_param.atype = 0;
3148 
3149 	chan = __dma_request_channel(&mask, udma_dma_filter_fn, &filter_param,
3150 				     ofdma->of_node);
3151 	if (!chan) {
3152 		dev_err(ud->dev, "get channel fail in %s.\n", __func__);
3153 		return ERR_PTR(-EINVAL);
3154 	}
3155 
3156 	return chan;
3157 }
3158 
3159 static struct udma_match_data am654_main_data = {
3160 	.psil_base = 0x1000,
3161 	.enable_memcpy_support = true,
3162 	.statictr_z_mask = GENMASK(11, 0),
3163 	.rchan_oes_offset = 0x2000,
3164 	.tpl_levels = 2,
3165 	.level_start_idx = {
3166 		[0] = 8, /* Normal channels */
3167 		[1] = 0, /* High Throughput channels */
3168 	},
3169 };
3170 
3171 static struct udma_match_data am654_mcu_data = {
3172 	.psil_base = 0x6000,
3173 	.enable_memcpy_support = false,
3174 	.statictr_z_mask = GENMASK(11, 0),
3175 	.rchan_oes_offset = 0x2000,
3176 	.tpl_levels = 2,
3177 	.level_start_idx = {
3178 		[0] = 2, /* Normal channels */
3179 		[1] = 0, /* High Throughput channels */
3180 	},
3181 };
3182 
3183 static struct udma_match_data j721e_main_data = {
3184 	.psil_base = 0x1000,
3185 	.enable_memcpy_support = true,
3186 	.flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST,
3187 	.statictr_z_mask = GENMASK(23, 0),
3188 	.rchan_oes_offset = 0x400,
3189 	.tpl_levels = 3,
3190 	.level_start_idx = {
3191 		[0] = 16, /* Normal channels */
3192 		[1] = 4, /* High Throughput channels */
3193 		[2] = 0, /* Ultra High Throughput channels */
3194 	},
3195 };
3196 
3197 static struct udma_match_data j721e_mcu_data = {
3198 	.psil_base = 0x6000,
3199 	.enable_memcpy_support = false, /* MEM_TO_MEM is slow via MCU UDMA */
3200 	.flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST,
3201 	.statictr_z_mask = GENMASK(23, 0),
3202 	.rchan_oes_offset = 0x400,
3203 	.tpl_levels = 2,
3204 	.level_start_idx = {
3205 		[0] = 2, /* Normal channels */
3206 		[1] = 0, /* High Throughput channels */
3207 	},
3208 };
3209 
3210 static const struct of_device_id udma_of_match[] = {
3211 	{
3212 		.compatible = "ti,am654-navss-main-udmap",
3213 		.data = &am654_main_data,
3214 	},
3215 	{
3216 		.compatible = "ti,am654-navss-mcu-udmap",
3217 		.data = &am654_mcu_data,
3218 	}, {
3219 		.compatible = "ti,j721e-navss-main-udmap",
3220 		.data = &j721e_main_data,
3221 	}, {
3222 		.compatible = "ti,j721e-navss-mcu-udmap",
3223 		.data = &j721e_mcu_data,
3224 	},
3225 	{ /* Sentinel */ },
3226 };
3227 
3228 static int udma_get_mmrs(struct platform_device *pdev, struct udma_dev *ud)
3229 {
3230 	struct resource *res;
3231 	int i;
3232 
3233 	for (i = 0; i < MMR_LAST; i++) {
3234 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3235 						   mmr_names[i]);
3236 		ud->mmrs[i] = devm_ioremap_resource(&pdev->dev, res);
3237 		if (IS_ERR(ud->mmrs[i]))
3238 			return PTR_ERR(ud->mmrs[i]);
3239 	}
3240 
3241 	return 0;
3242 }
3243 
3244 static int udma_setup_resources(struct udma_dev *ud)
3245 {
3246 	struct device *dev = ud->dev;
3247 	int ch_count, ret, i, j;
3248 	u32 cap2, cap3;
3249 	struct ti_sci_resource_desc *rm_desc;
3250 	struct ti_sci_resource *rm_res, irq_res;
3251 	struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
3252 	static const char * const range_names[] = { "ti,sci-rm-range-tchan",
3253 						    "ti,sci-rm-range-rchan",
3254 						    "ti,sci-rm-range-rflow" };
3255 
3256 	cap2 = udma_read(ud->mmrs[MMR_GCFG], 0x28);
3257 	cap3 = udma_read(ud->mmrs[MMR_GCFG], 0x2c);
3258 
3259 	ud->rflow_cnt = cap3 & 0x3fff;
3260 	ud->tchan_cnt = cap2 & 0x1ff;
3261 	ud->echan_cnt = (cap2 >> 9) & 0x1ff;
3262 	ud->rchan_cnt = (cap2 >> 18) & 0x1ff;
3263 	ch_count  = ud->tchan_cnt + ud->rchan_cnt;
3264 
3265 	ud->tchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->tchan_cnt),
3266 					   sizeof(unsigned long), GFP_KERNEL);
3267 	ud->tchans = devm_kcalloc(dev, ud->tchan_cnt, sizeof(*ud->tchans),
3268 				  GFP_KERNEL);
3269 	ud->rchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rchan_cnt),
3270 					   sizeof(unsigned long), GFP_KERNEL);
3271 	ud->rchans = devm_kcalloc(dev, ud->rchan_cnt, sizeof(*ud->rchans),
3272 				  GFP_KERNEL);
3273 	ud->rflow_gp_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rflow_cnt),
3274 					      sizeof(unsigned long),
3275 					      GFP_KERNEL);
3276 	ud->rflow_gp_map_allocated = devm_kcalloc(dev,
3277 						  BITS_TO_LONGS(ud->rflow_cnt),
3278 						  sizeof(unsigned long),
3279 						  GFP_KERNEL);
3280 	ud->rflow_in_use = devm_kcalloc(dev, BITS_TO_LONGS(ud->rflow_cnt),
3281 					sizeof(unsigned long),
3282 					GFP_KERNEL);
3283 	ud->rflows = devm_kcalloc(dev, ud->rflow_cnt, sizeof(*ud->rflows),
3284 				  GFP_KERNEL);
3285 
3286 	if (!ud->tchan_map || !ud->rchan_map || !ud->rflow_gp_map ||
3287 	    !ud->rflow_gp_map_allocated || !ud->tchans || !ud->rchans ||
3288 	    !ud->rflows || !ud->rflow_in_use)
3289 		return -ENOMEM;
3290 
3291 	/*
3292 	 * RX flows with the same Ids as RX channels are reserved to be used
3293 	 * as default flows if remote HW can't generate flow_ids. Those
3294 	 * RX flows can be requested only explicitly by id.
3295 	 */
3296 	bitmap_set(ud->rflow_gp_map_allocated, 0, ud->rchan_cnt);
3297 
3298 	/* by default no GP rflows are assigned to Linux */
3299 	bitmap_set(ud->rflow_gp_map, 0, ud->rflow_cnt);
3300 
3301 	/* Get resource ranges from tisci */
3302 	for (i = 0; i < RM_RANGE_LAST; i++)
3303 		tisci_rm->rm_ranges[i] =
3304 			devm_ti_sci_get_of_resource(tisci_rm->tisci, dev,
3305 						    tisci_rm->tisci_dev_id,
3306 						    (char *)range_names[i]);
3307 
3308 	/* tchan ranges */
3309 	rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
3310 	if (IS_ERR(rm_res)) {
3311 		bitmap_zero(ud->tchan_map, ud->tchan_cnt);
3312 	} else {
3313 		bitmap_fill(ud->tchan_map, ud->tchan_cnt);
3314 		for (i = 0; i < rm_res->sets; i++) {
3315 			rm_desc = &rm_res->desc[i];
3316 			bitmap_clear(ud->tchan_map, rm_desc->start,
3317 				     rm_desc->num);
3318 			dev_dbg(dev, "ti-sci-res: tchan: %d:%d\n",
3319 				rm_desc->start, rm_desc->num);
3320 		}
3321 	}
3322 	irq_res.sets = rm_res->sets;
3323 
3324 	/* rchan and matching default flow ranges */
3325 	rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
3326 	if (IS_ERR(rm_res)) {
3327 		bitmap_zero(ud->rchan_map, ud->rchan_cnt);
3328 	} else {
3329 		bitmap_fill(ud->rchan_map, ud->rchan_cnt);
3330 		for (i = 0; i < rm_res->sets; i++) {
3331 			rm_desc = &rm_res->desc[i];
3332 			bitmap_clear(ud->rchan_map, rm_desc->start,
3333 				     rm_desc->num);
3334 			dev_dbg(dev, "ti-sci-res: rchan: %d:%d\n",
3335 				rm_desc->start, rm_desc->num);
3336 		}
3337 	}
3338 
3339 	irq_res.sets += rm_res->sets;
3340 	irq_res.desc = kcalloc(irq_res.sets, sizeof(*irq_res.desc), GFP_KERNEL);
3341 	rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
3342 	for (i = 0; i < rm_res->sets; i++) {
3343 		irq_res.desc[i].start = rm_res->desc[i].start;
3344 		irq_res.desc[i].num = rm_res->desc[i].num;
3345 	}
3346 	rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
3347 	for (j = 0; j < rm_res->sets; j++, i++) {
3348 		irq_res.desc[i].start = rm_res->desc[j].start +
3349 					ud->match_data->rchan_oes_offset;
3350 		irq_res.desc[i].num = rm_res->desc[j].num;
3351 	}
3352 	ret = ti_sci_inta_msi_domain_alloc_irqs(ud->dev, &irq_res);
3353 	kfree(irq_res.desc);
3354 	if (ret) {
3355 		dev_err(ud->dev, "Failed to allocate MSI interrupts\n");
3356 		return ret;
3357 	}
3358 
3359 	/* GP rflow ranges */
3360 	rm_res = tisci_rm->rm_ranges[RM_RANGE_RFLOW];
3361 	if (IS_ERR(rm_res)) {
3362 		/* all gp flows are assigned exclusively to Linux */
3363 		bitmap_clear(ud->rflow_gp_map, ud->rchan_cnt,
3364 			     ud->rflow_cnt - ud->rchan_cnt);
3365 	} else {
3366 		for (i = 0; i < rm_res->sets; i++) {
3367 			rm_desc = &rm_res->desc[i];
3368 			bitmap_clear(ud->rflow_gp_map, rm_desc->start,
3369 				     rm_desc->num);
3370 			dev_dbg(dev, "ti-sci-res: rflow: %d:%d\n",
3371 				rm_desc->start, rm_desc->num);
3372 		}
3373 	}
3374 
3375 	ch_count -= bitmap_weight(ud->tchan_map, ud->tchan_cnt);
3376 	ch_count -= bitmap_weight(ud->rchan_map, ud->rchan_cnt);
3377 	if (!ch_count)
3378 		return -ENODEV;
3379 
3380 	ud->channels = devm_kcalloc(dev, ch_count, sizeof(*ud->channels),
3381 				    GFP_KERNEL);
3382 	if (!ud->channels)
3383 		return -ENOMEM;
3384 
3385 	dev_info(dev, "Channels: %d (tchan: %u, rchan: %u, gp-rflow: %u)\n",
3386 		 ch_count,
3387 		 ud->tchan_cnt - bitmap_weight(ud->tchan_map, ud->tchan_cnt),
3388 		 ud->rchan_cnt - bitmap_weight(ud->rchan_map, ud->rchan_cnt),
3389 		 ud->rflow_cnt - bitmap_weight(ud->rflow_gp_map,
3390 					       ud->rflow_cnt));
3391 
3392 	return ch_count;
3393 }
3394 
3395 static int udma_setup_rx_flush(struct udma_dev *ud)
3396 {
3397 	struct udma_rx_flush *rx_flush = &ud->rx_flush;
3398 	struct cppi5_desc_hdr_t *tr_desc;
3399 	struct cppi5_tr_type1_t *tr_req;
3400 	struct cppi5_host_desc_t *desc;
3401 	struct device *dev = ud->dev;
3402 	struct udma_hwdesc *hwdesc;
3403 	size_t tr_size;
3404 
3405 	/* Allocate 1K buffer for discarded data on RX channel teardown */
3406 	rx_flush->buffer_size = SZ_1K;
3407 	rx_flush->buffer_vaddr = devm_kzalloc(dev, rx_flush->buffer_size,
3408 					      GFP_KERNEL);
3409 	if (!rx_flush->buffer_vaddr)
3410 		return -ENOMEM;
3411 
3412 	rx_flush->buffer_paddr = dma_map_single(dev, rx_flush->buffer_vaddr,
3413 						rx_flush->buffer_size,
3414 						DMA_TO_DEVICE);
3415 	if (dma_mapping_error(dev, rx_flush->buffer_paddr))
3416 		return -ENOMEM;
3417 
3418 	/* Set up descriptor to be used for TR mode */
3419 	hwdesc = &rx_flush->hwdescs[0];
3420 	tr_size = sizeof(struct cppi5_tr_type1_t);
3421 	hwdesc->cppi5_desc_size = cppi5_trdesc_calc_size(tr_size, 1);
3422 	hwdesc->cppi5_desc_size = ALIGN(hwdesc->cppi5_desc_size,
3423 					ud->desc_align);
3424 
3425 	hwdesc->cppi5_desc_vaddr = devm_kzalloc(dev, hwdesc->cppi5_desc_size,
3426 						GFP_KERNEL);
3427 	if (!hwdesc->cppi5_desc_vaddr)
3428 		return -ENOMEM;
3429 
3430 	hwdesc->cppi5_desc_paddr = dma_map_single(dev, hwdesc->cppi5_desc_vaddr,
3431 						  hwdesc->cppi5_desc_size,
3432 						  DMA_TO_DEVICE);
3433 	if (dma_mapping_error(dev, hwdesc->cppi5_desc_paddr))
3434 		return -ENOMEM;
3435 
3436 	/* Start of the TR req records */
3437 	hwdesc->tr_req_base = hwdesc->cppi5_desc_vaddr + tr_size;
3438 	/* Start address of the TR response array */
3439 	hwdesc->tr_resp_base = hwdesc->tr_req_base + tr_size;
3440 
3441 	tr_desc = hwdesc->cppi5_desc_vaddr;
3442 	cppi5_trdesc_init(tr_desc, 1, tr_size, 0, 0);
3443 	cppi5_desc_set_pktids(tr_desc, 0, CPPI5_INFO1_DESC_FLOWID_DEFAULT);
3444 	cppi5_desc_set_retpolicy(tr_desc, 0, 0);
3445 
3446 	tr_req = hwdesc->tr_req_base;
3447 	cppi5_tr_init(&tr_req->flags, CPPI5_TR_TYPE1, false, false,
3448 		      CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
3449 	cppi5_tr_csf_set(&tr_req->flags, CPPI5_TR_CSF_SUPR_EVT);
3450 
3451 	tr_req->addr = rx_flush->buffer_paddr;
3452 	tr_req->icnt0 = rx_flush->buffer_size;
3453 	tr_req->icnt1 = 1;
3454 
3455 	dma_sync_single_for_device(dev, hwdesc->cppi5_desc_paddr,
3456 				   hwdesc->cppi5_desc_size, DMA_TO_DEVICE);
3457 
3458 	/* Set up descriptor to be used for packet mode */
3459 	hwdesc = &rx_flush->hwdescs[1];
3460 	hwdesc->cppi5_desc_size = ALIGN(sizeof(struct cppi5_host_desc_t) +
3461 					CPPI5_INFO0_HDESC_EPIB_SIZE +
3462 					CPPI5_INFO0_HDESC_PSDATA_MAX_SIZE,
3463 					ud->desc_align);
3464 
3465 	hwdesc->cppi5_desc_vaddr = devm_kzalloc(dev, hwdesc->cppi5_desc_size,
3466 						GFP_KERNEL);
3467 	if (!hwdesc->cppi5_desc_vaddr)
3468 		return -ENOMEM;
3469 
3470 	hwdesc->cppi5_desc_paddr = dma_map_single(dev, hwdesc->cppi5_desc_vaddr,
3471 						  hwdesc->cppi5_desc_size,
3472 						  DMA_TO_DEVICE);
3473 	if (dma_mapping_error(dev, hwdesc->cppi5_desc_paddr))
3474 		return -ENOMEM;
3475 
3476 	desc = hwdesc->cppi5_desc_vaddr;
3477 	cppi5_hdesc_init(desc, 0, 0);
3478 	cppi5_desc_set_pktids(&desc->hdr, 0, CPPI5_INFO1_DESC_FLOWID_DEFAULT);
3479 	cppi5_desc_set_retpolicy(&desc->hdr, 0, 0);
3480 
3481 	cppi5_hdesc_attach_buf(desc,
3482 			       rx_flush->buffer_paddr, rx_flush->buffer_size,
3483 			       rx_flush->buffer_paddr, rx_flush->buffer_size);
3484 
3485 	dma_sync_single_for_device(dev, hwdesc->cppi5_desc_paddr,
3486 				   hwdesc->cppi5_desc_size, DMA_TO_DEVICE);
3487 	return 0;
3488 }
3489 
3490 #ifdef CONFIG_DEBUG_FS
3491 static void udma_dbg_summary_show_chan(struct seq_file *s,
3492 				       struct dma_chan *chan)
3493 {
3494 	struct udma_chan *uc = to_udma_chan(chan);
3495 	struct udma_chan_config *ucc = &uc->config;
3496 
3497 	seq_printf(s, " %-13s| %s", dma_chan_name(chan),
3498 		   chan->dbg_client_name ?: "in-use");
3499 	seq_printf(s, " (%s, ", dmaengine_get_direction_text(uc->config.dir));
3500 
3501 	switch (uc->config.dir) {
3502 	case DMA_MEM_TO_MEM:
3503 		seq_printf(s, "chan%d pair [0x%04x -> 0x%04x], ", uc->tchan->id,
3504 			   ucc->src_thread, ucc->dst_thread);
3505 		break;
3506 	case DMA_DEV_TO_MEM:
3507 		seq_printf(s, "rchan%d [0x%04x -> 0x%04x], ", uc->rchan->id,
3508 			   ucc->src_thread, ucc->dst_thread);
3509 		break;
3510 	case DMA_MEM_TO_DEV:
3511 		seq_printf(s, "tchan%d [0x%04x -> 0x%04x], ", uc->tchan->id,
3512 			   ucc->src_thread, ucc->dst_thread);
3513 		break;
3514 	default:
3515 		seq_printf(s, ")\n");
3516 		return;
3517 	}
3518 
3519 	if (ucc->ep_type == PSIL_EP_NATIVE) {
3520 		seq_printf(s, "PSI-L Native");
3521 		if (ucc->metadata_size) {
3522 			seq_printf(s, "[%s", ucc->needs_epib ? " EPIB" : "");
3523 			if (ucc->psd_size)
3524 				seq_printf(s, " PSDsize:%u", ucc->psd_size);
3525 			seq_printf(s, " ]");
3526 		}
3527 	} else {
3528 		seq_printf(s, "PDMA");
3529 		if (ucc->enable_acc32 || ucc->enable_burst)
3530 			seq_printf(s, "[%s%s ]",
3531 				   ucc->enable_acc32 ? " ACC32" : "",
3532 				   ucc->enable_burst ? " BURST" : "");
3533 	}
3534 
3535 	seq_printf(s, ", %s)\n", ucc->pkt_mode ? "Packet mode" : "TR mode");
3536 }
3537 
3538 static void udma_dbg_summary_show(struct seq_file *s,
3539 				  struct dma_device *dma_dev)
3540 {
3541 	struct dma_chan *chan;
3542 
3543 	list_for_each_entry(chan, &dma_dev->channels, device_node) {
3544 		if (chan->client_count)
3545 			udma_dbg_summary_show_chan(s, chan);
3546 	}
3547 }
3548 #endif /* CONFIG_DEBUG_FS */
3549 
3550 #define TI_UDMAC_BUSWIDTHS	(BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
3551 				 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
3552 				 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
3553 				 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
3554 				 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
3555 
3556 static int udma_probe(struct platform_device *pdev)
3557 {
3558 	struct device_node *navss_node = pdev->dev.parent->of_node;
3559 	struct device *dev = &pdev->dev;
3560 	struct udma_dev *ud;
3561 	const struct of_device_id *match;
3562 	int i, ret;
3563 	int ch_count;
3564 
3565 	ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(48));
3566 	if (ret)
3567 		dev_err(dev, "failed to set dma mask stuff\n");
3568 
3569 	ud = devm_kzalloc(dev, sizeof(*ud), GFP_KERNEL);
3570 	if (!ud)
3571 		return -ENOMEM;
3572 
3573 	ret = udma_get_mmrs(pdev, ud);
3574 	if (ret)
3575 		return ret;
3576 
3577 	ud->tisci_rm.tisci = ti_sci_get_by_phandle(dev->of_node, "ti,sci");
3578 	if (IS_ERR(ud->tisci_rm.tisci))
3579 		return PTR_ERR(ud->tisci_rm.tisci);
3580 
3581 	ret = of_property_read_u32(dev->of_node, "ti,sci-dev-id",
3582 				   &ud->tisci_rm.tisci_dev_id);
3583 	if (ret) {
3584 		dev_err(dev, "ti,sci-dev-id read failure %d\n", ret);
3585 		return ret;
3586 	}
3587 	pdev->id = ud->tisci_rm.tisci_dev_id;
3588 
3589 	ret = of_property_read_u32(navss_node, "ti,sci-dev-id",
3590 				   &ud->tisci_rm.tisci_navss_dev_id);
3591 	if (ret) {
3592 		dev_err(dev, "NAVSS ti,sci-dev-id read failure %d\n", ret);
3593 		return ret;
3594 	}
3595 
3596 	ret = of_property_read_u32(navss_node, "ti,udma-atype", &ud->atype);
3597 	if (!ret && ud->atype > 2) {
3598 		dev_err(dev, "Invalid atype: %u\n", ud->atype);
3599 		return -EINVAL;
3600 	}
3601 
3602 	ud->tisci_rm.tisci_udmap_ops = &ud->tisci_rm.tisci->ops.rm_udmap_ops;
3603 	ud->tisci_rm.tisci_psil_ops = &ud->tisci_rm.tisci->ops.rm_psil_ops;
3604 
3605 	ud->ringacc = of_k3_ringacc_get_by_phandle(dev->of_node, "ti,ringacc");
3606 	if (IS_ERR(ud->ringacc))
3607 		return PTR_ERR(ud->ringacc);
3608 
3609 	dev->msi_domain = of_msi_get_domain(dev, dev->of_node,
3610 					    DOMAIN_BUS_TI_SCI_INTA_MSI);
3611 	if (!dev->msi_domain) {
3612 		dev_err(dev, "Failed to get MSI domain\n");
3613 		return -EPROBE_DEFER;
3614 	}
3615 
3616 	match = of_match_node(udma_of_match, dev->of_node);
3617 	if (!match) {
3618 		dev_err(dev, "No compatible match found\n");
3619 		return -ENODEV;
3620 	}
3621 	ud->match_data = match->data;
3622 
3623 	dma_cap_set(DMA_SLAVE, ud->ddev.cap_mask);
3624 	dma_cap_set(DMA_CYCLIC, ud->ddev.cap_mask);
3625 
3626 	ud->ddev.device_alloc_chan_resources = udma_alloc_chan_resources;
3627 	ud->ddev.device_config = udma_slave_config;
3628 	ud->ddev.device_prep_slave_sg = udma_prep_slave_sg;
3629 	ud->ddev.device_prep_dma_cyclic = udma_prep_dma_cyclic;
3630 	ud->ddev.device_issue_pending = udma_issue_pending;
3631 	ud->ddev.device_tx_status = udma_tx_status;
3632 	ud->ddev.device_pause = udma_pause;
3633 	ud->ddev.device_resume = udma_resume;
3634 	ud->ddev.device_terminate_all = udma_terminate_all;
3635 	ud->ddev.device_synchronize = udma_synchronize;
3636 #ifdef CONFIG_DEBUG_FS
3637 	ud->ddev.dbg_summary_show = udma_dbg_summary_show;
3638 #endif
3639 
3640 	ud->ddev.device_free_chan_resources = udma_free_chan_resources;
3641 	ud->ddev.src_addr_widths = TI_UDMAC_BUSWIDTHS;
3642 	ud->ddev.dst_addr_widths = TI_UDMAC_BUSWIDTHS;
3643 	ud->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
3644 	ud->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
3645 	ud->ddev.copy_align = DMAENGINE_ALIGN_8_BYTES;
3646 	ud->ddev.desc_metadata_modes = DESC_METADATA_CLIENT |
3647 				       DESC_METADATA_ENGINE;
3648 	if (ud->match_data->enable_memcpy_support) {
3649 		dma_cap_set(DMA_MEMCPY, ud->ddev.cap_mask);
3650 		ud->ddev.device_prep_dma_memcpy = udma_prep_dma_memcpy;
3651 		ud->ddev.directions |= BIT(DMA_MEM_TO_MEM);
3652 	}
3653 
3654 	ud->ddev.dev = dev;
3655 	ud->dev = dev;
3656 	ud->psil_base = ud->match_data->psil_base;
3657 
3658 	INIT_LIST_HEAD(&ud->ddev.channels);
3659 	INIT_LIST_HEAD(&ud->desc_to_purge);
3660 
3661 	ch_count = udma_setup_resources(ud);
3662 	if (ch_count <= 0)
3663 		return ch_count;
3664 
3665 	spin_lock_init(&ud->lock);
3666 	INIT_WORK(&ud->purge_work, udma_purge_desc_work);
3667 
3668 	ud->desc_align = 64;
3669 	if (ud->desc_align < dma_get_cache_alignment())
3670 		ud->desc_align = dma_get_cache_alignment();
3671 
3672 	ret = udma_setup_rx_flush(ud);
3673 	if (ret)
3674 		return ret;
3675 
3676 	for (i = 0; i < ud->tchan_cnt; i++) {
3677 		struct udma_tchan *tchan = &ud->tchans[i];
3678 
3679 		tchan->id = i;
3680 		tchan->reg_rt = ud->mmrs[MMR_TCHANRT] + i * 0x1000;
3681 	}
3682 
3683 	for (i = 0; i < ud->rchan_cnt; i++) {
3684 		struct udma_rchan *rchan = &ud->rchans[i];
3685 
3686 		rchan->id = i;
3687 		rchan->reg_rt = ud->mmrs[MMR_RCHANRT] + i * 0x1000;
3688 	}
3689 
3690 	for (i = 0; i < ud->rflow_cnt; i++) {
3691 		struct udma_rflow *rflow = &ud->rflows[i];
3692 
3693 		rflow->id = i;
3694 	}
3695 
3696 	for (i = 0; i < ch_count; i++) {
3697 		struct udma_chan *uc = &ud->channels[i];
3698 
3699 		uc->ud = ud;
3700 		uc->vc.desc_free = udma_desc_free;
3701 		uc->id = i;
3702 		uc->tchan = NULL;
3703 		uc->rchan = NULL;
3704 		uc->config.remote_thread_id = -1;
3705 		uc->config.dir = DMA_MEM_TO_MEM;
3706 		uc->name = devm_kasprintf(dev, GFP_KERNEL, "%s chan%d",
3707 					  dev_name(dev), i);
3708 
3709 		vchan_init(&uc->vc, &ud->ddev);
3710 		/* Use custom vchan completion handling */
3711 		tasklet_init(&uc->vc.task, udma_vchan_complete,
3712 			     (unsigned long)&uc->vc);
3713 		init_completion(&uc->teardown_completed);
3714 	}
3715 
3716 	ret = dma_async_device_register(&ud->ddev);
3717 	if (ret) {
3718 		dev_err(dev, "failed to register slave DMA engine: %d\n", ret);
3719 		return ret;
3720 	}
3721 
3722 	platform_set_drvdata(pdev, ud);
3723 
3724 	ret = of_dma_controller_register(dev->of_node, udma_of_xlate, ud);
3725 	if (ret) {
3726 		dev_err(dev, "failed to register of_dma controller\n");
3727 		dma_async_device_unregister(&ud->ddev);
3728 	}
3729 
3730 	return ret;
3731 }
3732 
3733 static struct platform_driver udma_driver = {
3734 	.driver = {
3735 		.name	= "ti-udma",
3736 		.of_match_table = udma_of_match,
3737 		.suppress_bind_attrs = true,
3738 	},
3739 	.probe		= udma_probe,
3740 };
3741 builtin_platform_driver(udma_driver);
3742 
3743 /* Private interfaces to UDMA */
3744 #include "k3-udma-private.c"
3745