blob: f327d436f8adc8a23037eabfd7cfcae162b8a59e [file] [log] [blame]
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001// 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>
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02008#include <linux/delay.h>
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02009#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>
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +030019#include <linux/sys_soc.h>
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +020020#include <linux/of.h>
21#include <linux/of_dma.h>
22#include <linux/of_device.h>
23#include <linux/of_irq.h>
24#include <linux/workqueue.h>
25#include <linux/completion.h>
26#include <linux/soc/ti/k3-ringacc.h>
27#include <linux/soc/ti/ti_sci_protocol.h>
28#include <linux/soc/ti/ti_sci_inta_msi.h>
Peter Ujfalusi01779472020-12-08 11:04:37 +020029#include <linux/dma/k3-event-router.h>
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +020030#include <linux/dma/ti-cppi5.h>
31
32#include "../virt-dma.h"
33#include "k3-udma.h"
34#include "k3-psil-priv.h"
35
36struct udma_static_tr {
37 u8 elsize; /* RPSTR0 */
38 u16 elcnt; /* RPSTR0 */
39 u16 bstcnt; /* RPSTR1 */
40};
41
42#define K3_UDMA_MAX_RFLOWS 1024
43#define K3_UDMA_DEFAULT_RING_SIZE 16
44
45/* How SRC/DST tag should be updated by UDMA in the descriptor's Word 3 */
46#define UDMA_RFLOW_SRCTAG_NONE 0
47#define UDMA_RFLOW_SRCTAG_CFG_TAG 1
48#define UDMA_RFLOW_SRCTAG_FLOW_ID 2
49#define UDMA_RFLOW_SRCTAG_SRC_TAG 4
50
51#define UDMA_RFLOW_DSTTAG_NONE 0
52#define UDMA_RFLOW_DSTTAG_CFG_TAG 1
53#define UDMA_RFLOW_DSTTAG_FLOW_ID 2
54#define UDMA_RFLOW_DSTTAG_DST_TAG_LO 4
55#define UDMA_RFLOW_DSTTAG_DST_TAG_HI 5
56
57struct udma_chan;
58
Peter Ujfalusi01779472020-12-08 11:04:37 +020059enum k3_dma_type {
60 DMA_TYPE_UDMA = 0,
61 DMA_TYPE_BCDMA,
62};
63
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +020064enum udma_mmr {
65 MMR_GCFG = 0,
Peter Ujfalusi01779472020-12-08 11:04:37 +020066 MMR_BCHANRT,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +020067 MMR_RCHANRT,
68 MMR_TCHANRT,
69 MMR_LAST,
70};
71
Peter Ujfalusi01779472020-12-08 11:04:37 +020072static const char * const mmr_names[] = {
73 [MMR_GCFG] = "gcfg",
74 [MMR_BCHANRT] = "bchanrt",
75 [MMR_RCHANRT] = "rchanrt",
76 [MMR_TCHANRT] = "tchanrt",
77};
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +020078
79struct udma_tchan {
80 void __iomem *reg_rt;
81
82 int id;
83 struct k3_ring *t_ring; /* Transmit ring */
84 struct k3_ring *tc_ring; /* Transmit Completion ring */
85};
86
Peter Ujfalusi01779472020-12-08 11:04:37 +020087#define udma_bchan udma_tchan
88
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +020089struct udma_rflow {
90 int id;
91 struct k3_ring *fd_ring; /* Free Descriptor ring */
92 struct k3_ring *r_ring; /* Receive ring */
93};
94
95struct udma_rchan {
96 void __iomem *reg_rt;
97
98 int id;
99};
100
Peter Ujfalusi01779472020-12-08 11:04:37 +0200101struct udma_oes_offsets {
102 /* K3 UDMA Output Event Offset */
103 u32 udma_rchan;
104
105 /* BCDMA Output Event Offsets */
106 u32 bcdma_bchan_data;
107 u32 bcdma_bchan_ring;
108 u32 bcdma_tchan_data;
109 u32 bcdma_tchan_ring;
110 u32 bcdma_rchan_data;
111 u32 bcdma_rchan_ring;
112};
113
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200114#define UDMA_FLAG_PDMA_ACC32 BIT(0)
115#define UDMA_FLAG_PDMA_BURST BIT(1)
Peter Ujfalusi5e1cb1c2020-12-08 11:04:22 +0200116#define UDMA_FLAG_TDTYPE BIT(2)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200117
118struct udma_match_data {
Peter Ujfalusi01779472020-12-08 11:04:37 +0200119 enum k3_dma_type type;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200120 u32 psil_base;
121 bool enable_memcpy_support;
122 u32 flags;
123 u32 statictr_z_mask;
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +0300124};
125
126struct udma_soc_data {
Peter Ujfalusi01779472020-12-08 11:04:37 +0200127 struct udma_oes_offsets oes;
128 u32 bcdma_trigger_event_offset;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200129};
130
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200131struct udma_hwdesc {
132 size_t cppi5_desc_size;
133 void *cppi5_desc_vaddr;
134 dma_addr_t cppi5_desc_paddr;
135
136 /* TR descriptor internal pointers */
137 void *tr_req_base;
138 struct cppi5_tr_resp_t *tr_resp_base;
139};
140
141struct udma_rx_flush {
142 struct udma_hwdesc hwdescs[2];
143
144 size_t buffer_size;
145 void *buffer_vaddr;
146 dma_addr_t buffer_paddr;
147};
148
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200149struct udma_dev {
150 struct dma_device ddev;
151 struct device *dev;
152 void __iomem *mmrs[MMR_LAST];
153 const struct udma_match_data *match_data;
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +0300154 const struct udma_soc_data *soc_data;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200155
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +0300156 u8 tpl_levels;
157 u32 tpl_start_idx[3];
158
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200159 size_t desc_align; /* alignment to use for descriptors */
160
161 struct udma_tisci_rm tisci_rm;
162
163 struct k3_ringacc *ringacc;
164
165 struct work_struct purge_work;
166 struct list_head desc_to_purge;
167 spinlock_t lock;
168
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200169 struct udma_rx_flush rx_flush;
170
Peter Ujfalusi01779472020-12-08 11:04:37 +0200171 int bchan_cnt;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200172 int tchan_cnt;
173 int echan_cnt;
174 int rchan_cnt;
175 int rflow_cnt;
Peter Ujfalusi01779472020-12-08 11:04:37 +0200176 unsigned long *bchan_map;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200177 unsigned long *tchan_map;
178 unsigned long *rchan_map;
179 unsigned long *rflow_gp_map;
180 unsigned long *rflow_gp_map_allocated;
181 unsigned long *rflow_in_use;
182
Peter Ujfalusi01779472020-12-08 11:04:37 +0200183 struct udma_bchan *bchans;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200184 struct udma_tchan *tchans;
185 struct udma_rchan *rchans;
186 struct udma_rflow *rflows;
187
188 struct udma_chan *channels;
189 u32 psil_base;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +0200190 u32 atype;
Peter Ujfalusi01779472020-12-08 11:04:37 +0200191 u32 asel;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200192};
193
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200194struct udma_desc {
195 struct virt_dma_desc vd;
196
197 bool terminated;
198
199 enum dma_transfer_direction dir;
200
201 struct udma_static_tr static_tr;
202 u32 residue;
203
204 unsigned int sglen;
205 unsigned int desc_idx; /* Only used for cyclic in packet mode */
206 unsigned int tr_idx;
207
208 u32 metadata_size;
209 void *metadata; /* pointer to provided metadata buffer (EPIP, PSdata) */
210
211 unsigned int hwdesc_count;
Gustavo A. R. Silva466f9662020-05-28 09:35:11 -0500212 struct udma_hwdesc hwdesc[];
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200213};
214
215enum udma_chan_state {
216 UDMA_CHAN_IS_IDLE = 0, /* not active, no teardown is in progress */
217 UDMA_CHAN_IS_ACTIVE, /* Normal operation */
218 UDMA_CHAN_IS_TERMINATING, /* channel is being terminated */
219};
220
221struct udma_tx_drain {
222 struct delayed_work work;
Vignesh Raghavendra1c837672020-02-14 11:14:36 +0200223 ktime_t tstamp;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200224 u32 residue;
225};
226
227struct udma_chan_config {
228 bool pkt_mode; /* TR or packet */
229 bool needs_epib; /* EPIB is needed for the communication or not */
230 u32 psd_size; /* size of Protocol Specific Data */
231 u32 metadata_size; /* (needs_epib ? 16:0) + psd_size */
232 u32 hdesc_size; /* Size of a packet descriptor in packet mode */
233 bool notdpkt; /* Suppress sending TDC packet */
234 int remote_thread_id;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +0200235 u32 atype;
Peter Ujfalusi01779472020-12-08 11:04:37 +0200236 u32 asel;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200237 u32 src_thread;
238 u32 dst_thread;
239 enum psil_endpoint_type ep_type;
240 bool enable_acc32;
241 bool enable_burst;
242 enum udma_tp_level channel_tpl; /* Channel Throughput Level */
243
Peter Ujfalusi01779472020-12-08 11:04:37 +0200244 u32 tr_trigger_type;
245
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200246 enum dma_transfer_direction dir;
247};
248
249struct udma_chan {
250 struct virt_dma_chan vc;
251 struct dma_slave_config cfg;
252 struct udma_dev *ud;
Peter Ujfalusi01779472020-12-08 11:04:37 +0200253 struct device *dma_dev;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200254 struct udma_desc *desc;
255 struct udma_desc *terminated_desc;
256 struct udma_static_tr static_tr;
257 char *name;
258
Peter Ujfalusi01779472020-12-08 11:04:37 +0200259 struct udma_bchan *bchan;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200260 struct udma_tchan *tchan;
261 struct udma_rchan *rchan;
262 struct udma_rflow *rflow;
263
264 bool psil_paired;
265
266 int irq_num_ring;
267 int irq_num_udma;
268
269 bool cyclic;
270 bool paused;
271
272 enum udma_chan_state state;
273 struct completion teardown_completed;
274
275 struct udma_tx_drain tx_drain;
276
277 u32 bcnt; /* number of bytes completed since the start of the channel */
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200278
279 /* Channel configuration parameters */
280 struct udma_chan_config config;
281
282 /* dmapool for packet mode descriptors */
283 bool use_dma_pool;
284 struct dma_pool *hdesc_pool;
285
286 u32 id;
287};
288
289static inline struct udma_dev *to_udma_dev(struct dma_device *d)
290{
291 return container_of(d, struct udma_dev, ddev);
292}
293
294static inline struct udma_chan *to_udma_chan(struct dma_chan *c)
295{
296 return container_of(c, struct udma_chan, vc.chan);
297}
298
299static inline struct udma_desc *to_udma_desc(struct dma_async_tx_descriptor *t)
300{
301 return container_of(t, struct udma_desc, vd.tx);
302}
303
304/* Generic register access functions */
305static inline u32 udma_read(void __iomem *base, int reg)
306{
307 return readl(base + reg);
308}
309
310static inline void udma_write(void __iomem *base, int reg, u32 val)
311{
312 writel(val, base + reg);
313}
314
315static inline void udma_update_bits(void __iomem *base, int reg,
316 u32 mask, u32 val)
317{
318 u32 tmp, orig;
319
320 orig = readl(base + reg);
321 tmp = orig & ~mask;
322 tmp |= (val & mask);
323
324 if (tmp != orig)
325 writel(tmp, base + reg);
326}
327
328/* TCHANRT */
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300329static inline u32 udma_tchanrt_read(struct udma_chan *uc, int reg)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200330{
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300331 if (!uc->tchan)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200332 return 0;
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300333 return udma_read(uc->tchan->reg_rt, reg);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200334}
335
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300336static inline void udma_tchanrt_write(struct udma_chan *uc, int reg, u32 val)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200337{
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300338 if (!uc->tchan)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200339 return;
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300340 udma_write(uc->tchan->reg_rt, reg, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200341}
342
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300343static inline void udma_tchanrt_update_bits(struct udma_chan *uc, int reg,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200344 u32 mask, u32 val)
345{
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300346 if (!uc->tchan)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200347 return;
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300348 udma_update_bits(uc->tchan->reg_rt, reg, mask, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200349}
350
351/* RCHANRT */
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300352static inline u32 udma_rchanrt_read(struct udma_chan *uc, int reg)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200353{
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300354 if (!uc->rchan)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200355 return 0;
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300356 return udma_read(uc->rchan->reg_rt, reg);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200357}
358
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300359static inline void udma_rchanrt_write(struct udma_chan *uc, int reg, u32 val)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200360{
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300361 if (!uc->rchan)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200362 return;
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300363 udma_write(uc->rchan->reg_rt, reg, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200364}
365
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300366static inline void udma_rchanrt_update_bits(struct udma_chan *uc, int reg,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200367 u32 mask, u32 val)
368{
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300369 if (!uc->rchan)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200370 return;
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300371 udma_update_bits(uc->rchan->reg_rt, reg, mask, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200372}
373
374static int navss_psil_pair(struct udma_dev *ud, u32 src_thread, u32 dst_thread)
375{
376 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
377
378 dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
379 return tisci_rm->tisci_psil_ops->pair(tisci_rm->tisci,
380 tisci_rm->tisci_navss_dev_id,
381 src_thread, dst_thread);
382}
383
384static int navss_psil_unpair(struct udma_dev *ud, u32 src_thread,
385 u32 dst_thread)
386{
387 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
388
389 dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
390 return tisci_rm->tisci_psil_ops->unpair(tisci_rm->tisci,
391 tisci_rm->tisci_navss_dev_id,
392 src_thread, dst_thread);
393}
394
Peter Ujfalusi01779472020-12-08 11:04:37 +0200395static void k3_configure_chan_coherency(struct dma_chan *chan, u32 asel)
396{
397 struct device *chan_dev = &chan->dev->device;
398
399 if (asel == 0) {
400 /* No special handling for the channel */
401 chan->dev->chan_dma_dev = false;
402
403 chan_dev->dma_coherent = false;
404 chan_dev->dma_parms = NULL;
405 } else if (asel == 14 || asel == 15) {
406 chan->dev->chan_dma_dev = true;
407
408 chan_dev->dma_coherent = true;
409 dma_coerce_mask_and_coherent(chan_dev, DMA_BIT_MASK(48));
410 chan_dev->dma_parms = chan_dev->parent->dma_parms;
411 } else {
412 dev_warn(chan->device->dev, "Invalid ASEL value: %u\n", asel);
413
414 chan_dev->dma_coherent = false;
415 chan_dev->dma_parms = NULL;
416 }
417}
418
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200419static void udma_reset_uchan(struct udma_chan *uc)
420{
421 memset(&uc->config, 0, sizeof(uc->config));
422 uc->config.remote_thread_id = -1;
423 uc->state = UDMA_CHAN_IS_IDLE;
424}
425
426static void udma_dump_chan_stdata(struct udma_chan *uc)
427{
428 struct device *dev = uc->ud->dev;
429 u32 offset;
430 int i;
431
432 if (uc->config.dir == DMA_MEM_TO_DEV || uc->config.dir == DMA_MEM_TO_MEM) {
433 dev_dbg(dev, "TCHAN State data:\n");
434 for (i = 0; i < 32; i++) {
Peter Ujfalusibc7e5522020-07-07 13:23:50 +0300435 offset = UDMA_CHAN_RT_STDATA_REG + i * 4;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200436 dev_dbg(dev, "TRT_STDATA[%02d]: 0x%08x\n", i,
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300437 udma_tchanrt_read(uc, offset));
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200438 }
439 }
440
441 if (uc->config.dir == DMA_DEV_TO_MEM || uc->config.dir == DMA_MEM_TO_MEM) {
442 dev_dbg(dev, "RCHAN State data:\n");
443 for (i = 0; i < 32; i++) {
Peter Ujfalusibc7e5522020-07-07 13:23:50 +0300444 offset = UDMA_CHAN_RT_STDATA_REG + i * 4;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200445 dev_dbg(dev, "RRT_STDATA[%02d]: 0x%08x\n", i,
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300446 udma_rchanrt_read(uc, offset));
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200447 }
448 }
449}
450
451static inline dma_addr_t udma_curr_cppi5_desc_paddr(struct udma_desc *d,
452 int idx)
453{
454 return d->hwdesc[idx].cppi5_desc_paddr;
455}
456
457static inline void *udma_curr_cppi5_desc_vaddr(struct udma_desc *d, int idx)
458{
459 return d->hwdesc[idx].cppi5_desc_vaddr;
460}
461
462static struct udma_desc *udma_udma_desc_from_paddr(struct udma_chan *uc,
463 dma_addr_t paddr)
464{
465 struct udma_desc *d = uc->terminated_desc;
466
467 if (d) {
468 dma_addr_t desc_paddr = udma_curr_cppi5_desc_paddr(d,
469 d->desc_idx);
470
471 if (desc_paddr != paddr)
472 d = NULL;
473 }
474
475 if (!d) {
476 d = uc->desc;
477 if (d) {
478 dma_addr_t desc_paddr = udma_curr_cppi5_desc_paddr(d,
479 d->desc_idx);
480
481 if (desc_paddr != paddr)
482 d = NULL;
483 }
484 }
485
486 return d;
487}
488
489static void udma_free_hwdesc(struct udma_chan *uc, struct udma_desc *d)
490{
491 if (uc->use_dma_pool) {
492 int i;
493
494 for (i = 0; i < d->hwdesc_count; i++) {
495 if (!d->hwdesc[i].cppi5_desc_vaddr)
496 continue;
497
498 dma_pool_free(uc->hdesc_pool,
499 d->hwdesc[i].cppi5_desc_vaddr,
500 d->hwdesc[i].cppi5_desc_paddr);
501
502 d->hwdesc[i].cppi5_desc_vaddr = NULL;
503 }
504 } else if (d->hwdesc[0].cppi5_desc_vaddr) {
Peter Ujfalusi01779472020-12-08 11:04:37 +0200505 dma_free_coherent(uc->dma_dev, d->hwdesc[0].cppi5_desc_size,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200506 d->hwdesc[0].cppi5_desc_vaddr,
507 d->hwdesc[0].cppi5_desc_paddr);
508
509 d->hwdesc[0].cppi5_desc_vaddr = NULL;
510 }
511}
512
513static void udma_purge_desc_work(struct work_struct *work)
514{
515 struct udma_dev *ud = container_of(work, typeof(*ud), purge_work);
516 struct virt_dma_desc *vd, *_vd;
517 unsigned long flags;
518 LIST_HEAD(head);
519
520 spin_lock_irqsave(&ud->lock, flags);
521 list_splice_tail_init(&ud->desc_to_purge, &head);
522 spin_unlock_irqrestore(&ud->lock, flags);
523
524 list_for_each_entry_safe(vd, _vd, &head, node) {
525 struct udma_chan *uc = to_udma_chan(vd->tx.chan);
526 struct udma_desc *d = to_udma_desc(&vd->tx);
527
528 udma_free_hwdesc(uc, d);
529 list_del(&vd->node);
530 kfree(d);
531 }
532
533 /* If more to purge, schedule the work again */
534 if (!list_empty(&ud->desc_to_purge))
535 schedule_work(&ud->purge_work);
536}
537
538static void udma_desc_free(struct virt_dma_desc *vd)
539{
540 struct udma_dev *ud = to_udma_dev(vd->tx.chan->device);
541 struct udma_chan *uc = to_udma_chan(vd->tx.chan);
542 struct udma_desc *d = to_udma_desc(&vd->tx);
543 unsigned long flags;
544
545 if (uc->terminated_desc == d)
546 uc->terminated_desc = NULL;
547
548 if (uc->use_dma_pool) {
549 udma_free_hwdesc(uc, d);
550 kfree(d);
551 return;
552 }
553
554 spin_lock_irqsave(&ud->lock, flags);
555 list_add_tail(&vd->node, &ud->desc_to_purge);
556 spin_unlock_irqrestore(&ud->lock, flags);
557
558 schedule_work(&ud->purge_work);
559}
560
561static bool udma_is_chan_running(struct udma_chan *uc)
562{
563 u32 trt_ctl = 0;
564 u32 rrt_ctl = 0;
565
566 if (uc->tchan)
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300567 trt_ctl = udma_tchanrt_read(uc, UDMA_CHAN_RT_CTL_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200568 if (uc->rchan)
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300569 rrt_ctl = udma_rchanrt_read(uc, UDMA_CHAN_RT_CTL_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200570
571 if (trt_ctl & UDMA_CHAN_RT_CTL_EN || rrt_ctl & UDMA_CHAN_RT_CTL_EN)
572 return true;
573
574 return false;
575}
576
577static bool udma_is_chan_paused(struct udma_chan *uc)
578{
579 u32 val, pause_mask;
580
Peter Ujfalusic7450bb2020-02-14 11:14:40 +0200581 switch (uc->config.dir) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200582 case DMA_DEV_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300583 val = udma_rchanrt_read(uc, UDMA_CHAN_RT_PEER_RT_EN_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200584 pause_mask = UDMA_PEER_RT_EN_PAUSE;
585 break;
586 case DMA_MEM_TO_DEV:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300587 val = udma_tchanrt_read(uc, UDMA_CHAN_RT_PEER_RT_EN_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200588 pause_mask = UDMA_PEER_RT_EN_PAUSE;
589 break;
590 case DMA_MEM_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300591 val = udma_tchanrt_read(uc, UDMA_CHAN_RT_CTL_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200592 pause_mask = UDMA_CHAN_RT_CTL_PAUSE;
593 break;
594 default:
595 return false;
596 }
597
598 if (val & pause_mask)
599 return true;
600
601 return false;
602}
603
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200604static inline dma_addr_t udma_get_rx_flush_hwdesc_paddr(struct udma_chan *uc)
605{
606 return uc->ud->rx_flush.hwdescs[uc->config.pkt_mode].cppi5_desc_paddr;
607}
608
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200609static int udma_push_to_ring(struct udma_chan *uc, int idx)
610{
611 struct udma_desc *d = uc->desc;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200612 struct k3_ring *ring = NULL;
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200613 dma_addr_t paddr;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200614
615 switch (uc->config.dir) {
616 case DMA_DEV_TO_MEM:
617 ring = uc->rflow->fd_ring;
618 break;
619 case DMA_MEM_TO_DEV:
620 case DMA_MEM_TO_MEM:
621 ring = uc->tchan->t_ring;
622 break;
623 default:
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200624 return -EINVAL;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200625 }
626
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200627 /* RX flush packet: idx == -1 is only passed in case of DEV_TO_MEM */
628 if (idx == -1) {
629 paddr = udma_get_rx_flush_hwdesc_paddr(uc);
630 } else {
631 paddr = udma_curr_cppi5_desc_paddr(d, idx);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200632
633 wmb(); /* Ensure that writes are not moved over this point */
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200634 }
635
Peter Ujfalusi6fea8732020-05-12 16:46:11 +0300636 return k3_ringacc_ring_push(ring, &paddr);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200637}
638
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200639static bool udma_desc_is_rx_flush(struct udma_chan *uc, dma_addr_t addr)
640{
641 if (uc->config.dir != DMA_DEV_TO_MEM)
642 return false;
643
644 if (addr == udma_get_rx_flush_hwdesc_paddr(uc))
645 return true;
646
647 return false;
648}
649
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200650static int udma_pop_from_ring(struct udma_chan *uc, dma_addr_t *addr)
651{
652 struct k3_ring *ring = NULL;
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300653 int ret;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200654
655 switch (uc->config.dir) {
656 case DMA_DEV_TO_MEM:
657 ring = uc->rflow->r_ring;
658 break;
659 case DMA_MEM_TO_DEV:
660 case DMA_MEM_TO_MEM:
661 ring = uc->tchan->tc_ring;
662 break;
663 default:
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300664 return -ENOENT;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200665 }
666
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300667 ret = k3_ringacc_ring_pop(ring, addr);
668 if (ret)
669 return ret;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200670
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300671 rmb(); /* Ensure that reads are not moved before this point */
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200672
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300673 /* Teardown completion */
674 if (cppi5_desc_is_tdcm(*addr))
675 return 0;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200676
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300677 /* Check for flush descriptor */
678 if (udma_desc_is_rx_flush(uc, *addr))
679 return -ENOENT;
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200680
Peter Ujfalusi3b8bee22020-07-07 13:23:49 +0300681 return 0;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200682}
683
684static void udma_reset_rings(struct udma_chan *uc)
685{
686 struct k3_ring *ring1 = NULL;
687 struct k3_ring *ring2 = NULL;
688
689 switch (uc->config.dir) {
690 case DMA_DEV_TO_MEM:
691 if (uc->rchan) {
692 ring1 = uc->rflow->fd_ring;
693 ring2 = uc->rflow->r_ring;
694 }
695 break;
696 case DMA_MEM_TO_DEV:
697 case DMA_MEM_TO_MEM:
698 if (uc->tchan) {
699 ring1 = uc->tchan->t_ring;
700 ring2 = uc->tchan->tc_ring;
701 }
702 break;
703 default:
704 break;
705 }
706
707 if (ring1)
708 k3_ringacc_ring_reset_dma(ring1,
709 k3_ringacc_ring_get_occ(ring1));
710 if (ring2)
711 k3_ringacc_ring_reset(ring2);
712
713 /* make sure we are not leaking memory by stalled descriptor */
714 if (uc->terminated_desc) {
715 udma_desc_free(&uc->terminated_desc->vd);
716 uc->terminated_desc = NULL;
717 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200718}
719
720static void udma_reset_counters(struct udma_chan *uc)
721{
722 u32 val;
723
724 if (uc->tchan) {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300725 val = udma_tchanrt_read(uc, UDMA_CHAN_RT_BCNT_REG);
726 udma_tchanrt_write(uc, UDMA_CHAN_RT_BCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200727
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300728 val = udma_tchanrt_read(uc, UDMA_CHAN_RT_SBCNT_REG);
729 udma_tchanrt_write(uc, UDMA_CHAN_RT_SBCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200730
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300731 val = udma_tchanrt_read(uc, UDMA_CHAN_RT_PCNT_REG);
732 udma_tchanrt_write(uc, UDMA_CHAN_RT_PCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200733
Peter Ujfalusi01779472020-12-08 11:04:37 +0200734 if (!uc->bchan) {
735 val = udma_tchanrt_read(uc, UDMA_CHAN_RT_PEER_BCNT_REG);
736 udma_tchanrt_write(uc, UDMA_CHAN_RT_PEER_BCNT_REG, val);
737 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200738 }
739
740 if (uc->rchan) {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300741 val = udma_rchanrt_read(uc, UDMA_CHAN_RT_BCNT_REG);
742 udma_rchanrt_write(uc, UDMA_CHAN_RT_BCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200743
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300744 val = udma_rchanrt_read(uc, UDMA_CHAN_RT_SBCNT_REG);
745 udma_rchanrt_write(uc, UDMA_CHAN_RT_SBCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200746
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300747 val = udma_rchanrt_read(uc, UDMA_CHAN_RT_PCNT_REG);
748 udma_rchanrt_write(uc, UDMA_CHAN_RT_PCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200749
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300750 val = udma_rchanrt_read(uc, UDMA_CHAN_RT_PEER_BCNT_REG);
751 udma_rchanrt_write(uc, UDMA_CHAN_RT_PEER_BCNT_REG, val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200752 }
753
754 uc->bcnt = 0;
755}
756
757static int udma_reset_chan(struct udma_chan *uc, bool hard)
758{
759 switch (uc->config.dir) {
760 case DMA_DEV_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300761 udma_rchanrt_write(uc, UDMA_CHAN_RT_PEER_RT_EN_REG, 0);
762 udma_rchanrt_write(uc, UDMA_CHAN_RT_CTL_REG, 0);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200763 break;
764 case DMA_MEM_TO_DEV:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300765 udma_tchanrt_write(uc, UDMA_CHAN_RT_CTL_REG, 0);
766 udma_tchanrt_write(uc, UDMA_CHAN_RT_PEER_RT_EN_REG, 0);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200767 break;
768 case DMA_MEM_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300769 udma_rchanrt_write(uc, UDMA_CHAN_RT_CTL_REG, 0);
770 udma_tchanrt_write(uc, UDMA_CHAN_RT_CTL_REG, 0);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200771 break;
772 default:
773 return -EINVAL;
774 }
775
776 /* Reset all counters */
777 udma_reset_counters(uc);
778
779 /* Hard reset: re-initialize the channel to reset */
780 if (hard) {
781 struct udma_chan_config ucc_backup;
782 int ret;
783
784 memcpy(&ucc_backup, &uc->config, sizeof(uc->config));
785 uc->ud->ddev.device_free_chan_resources(&uc->vc.chan);
786
787 /* restore the channel configuration */
788 memcpy(&uc->config, &ucc_backup, sizeof(uc->config));
789 ret = uc->ud->ddev.device_alloc_chan_resources(&uc->vc.chan);
790 if (ret)
791 return ret;
792
793 /*
794 * Setting forced teardown after forced reset helps recovering
795 * the rchan.
796 */
797 if (uc->config.dir == DMA_DEV_TO_MEM)
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300798 udma_rchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200799 UDMA_CHAN_RT_CTL_EN |
800 UDMA_CHAN_RT_CTL_TDOWN |
801 UDMA_CHAN_RT_CTL_FTDOWN);
802 }
803 uc->state = UDMA_CHAN_IS_IDLE;
804
805 return 0;
806}
807
808static void udma_start_desc(struct udma_chan *uc)
809{
810 struct udma_chan_config *ucc = &uc->config;
811
812 if (ucc->pkt_mode && (uc->cyclic || ucc->dir == DMA_DEV_TO_MEM)) {
813 int i;
814
815 /* Push all descriptors to ring for packet mode cyclic or RX */
816 for (i = 0; i < uc->desc->sglen; i++)
817 udma_push_to_ring(uc, i);
818 } else {
819 udma_push_to_ring(uc, 0);
820 }
821}
822
823static bool udma_chan_needs_reconfiguration(struct udma_chan *uc)
824{
825 /* Only PDMAs have staticTR */
826 if (uc->config.ep_type == PSIL_EP_NATIVE)
827 return false;
828
829 /* Check if the staticTR configuration has changed for TX */
830 if (memcmp(&uc->static_tr, &uc->desc->static_tr, sizeof(uc->static_tr)))
831 return true;
832
833 return false;
834}
835
836static int udma_start(struct udma_chan *uc)
837{
838 struct virt_dma_desc *vd = vchan_next_desc(&uc->vc);
839
840 if (!vd) {
841 uc->desc = NULL;
842 return -ENOENT;
843 }
844
845 list_del(&vd->node);
846
847 uc->desc = to_udma_desc(&vd->tx);
848
849 /* Channel is already running and does not need reconfiguration */
850 if (udma_is_chan_running(uc) && !udma_chan_needs_reconfiguration(uc)) {
851 udma_start_desc(uc);
852 goto out;
853 }
854
855 /* Make sure that we clear the teardown bit, if it is set */
856 udma_reset_chan(uc, false);
857
858 /* Push descriptors before we start the channel */
859 udma_start_desc(uc);
860
861 switch (uc->desc->dir) {
862 case DMA_DEV_TO_MEM:
863 /* Config remote TR */
864 if (uc->config.ep_type == PSIL_EP_PDMA_XY) {
865 u32 val = PDMA_STATIC_TR_Y(uc->desc->static_tr.elcnt) |
866 PDMA_STATIC_TR_X(uc->desc->static_tr.elsize);
867 const struct udma_match_data *match_data =
868 uc->ud->match_data;
869
870 if (uc->config.enable_acc32)
871 val |= PDMA_STATIC_TR_XY_ACC32;
872 if (uc->config.enable_burst)
873 val |= PDMA_STATIC_TR_XY_BURST;
874
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300875 udma_rchanrt_write(uc,
876 UDMA_CHAN_RT_PEER_STATIC_TR_XY_REG,
877 val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200878
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300879 udma_rchanrt_write(uc,
Peter Ujfalusibc7e5522020-07-07 13:23:50 +0300880 UDMA_CHAN_RT_PEER_STATIC_TR_Z_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200881 PDMA_STATIC_TR_Z(uc->desc->static_tr.bstcnt,
882 match_data->statictr_z_mask));
883
884 /* save the current staticTR configuration */
885 memcpy(&uc->static_tr, &uc->desc->static_tr,
886 sizeof(uc->static_tr));
887 }
888
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300889 udma_rchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200890 UDMA_CHAN_RT_CTL_EN);
891
892 /* Enable remote */
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300893 udma_rchanrt_write(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200894 UDMA_PEER_RT_EN_ENABLE);
895
896 break;
897 case DMA_MEM_TO_DEV:
898 /* Config remote TR */
899 if (uc->config.ep_type == PSIL_EP_PDMA_XY) {
900 u32 val = PDMA_STATIC_TR_Y(uc->desc->static_tr.elcnt) |
901 PDMA_STATIC_TR_X(uc->desc->static_tr.elsize);
902
903 if (uc->config.enable_acc32)
904 val |= PDMA_STATIC_TR_XY_ACC32;
905 if (uc->config.enable_burst)
906 val |= PDMA_STATIC_TR_XY_BURST;
907
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300908 udma_tchanrt_write(uc,
909 UDMA_CHAN_RT_PEER_STATIC_TR_XY_REG,
910 val);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200911
912 /* save the current staticTR configuration */
913 memcpy(&uc->static_tr, &uc->desc->static_tr,
914 sizeof(uc->static_tr));
915 }
916
917 /* Enable remote */
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300918 udma_tchanrt_write(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200919 UDMA_PEER_RT_EN_ENABLE);
920
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300921 udma_tchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200922 UDMA_CHAN_RT_CTL_EN);
923
924 break;
925 case DMA_MEM_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300926 udma_rchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200927 UDMA_CHAN_RT_CTL_EN);
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300928 udma_tchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200929 UDMA_CHAN_RT_CTL_EN);
930
931 break;
932 default:
933 return -EINVAL;
934 }
935
936 uc->state = UDMA_CHAN_IS_ACTIVE;
937out:
938
939 return 0;
940}
941
942static int udma_stop(struct udma_chan *uc)
943{
944 enum udma_chan_state old_state = uc->state;
945
946 uc->state = UDMA_CHAN_IS_TERMINATING;
947 reinit_completion(&uc->teardown_completed);
948
949 switch (uc->config.dir) {
950 case DMA_DEV_TO_MEM:
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +0200951 if (!uc->cyclic && !uc->desc)
952 udma_push_to_ring(uc, -1);
953
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300954 udma_rchanrt_write(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200955 UDMA_PEER_RT_EN_ENABLE |
956 UDMA_PEER_RT_EN_TEARDOWN);
957 break;
958 case DMA_MEM_TO_DEV:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300959 udma_tchanrt_write(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200960 UDMA_PEER_RT_EN_ENABLE |
961 UDMA_PEER_RT_EN_FLUSH);
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300962 udma_tchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200963 UDMA_CHAN_RT_CTL_EN |
964 UDMA_CHAN_RT_CTL_TDOWN);
965 break;
966 case DMA_MEM_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +0300967 udma_tchanrt_write(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +0200968 UDMA_CHAN_RT_CTL_EN |
969 UDMA_CHAN_RT_CTL_TDOWN);
970 break;
971 default:
972 uc->state = old_state;
973 complete_all(&uc->teardown_completed);
974 return -EINVAL;
975 }
976
977 return 0;
978}
979
980static void udma_cyclic_packet_elapsed(struct udma_chan *uc)
981{
982 struct udma_desc *d = uc->desc;
983 struct cppi5_host_desc_t *h_desc;
984
985 h_desc = d->hwdesc[d->desc_idx].cppi5_desc_vaddr;
986 cppi5_hdesc_reset_to_original(h_desc);
987 udma_push_to_ring(uc, d->desc_idx);
988 d->desc_idx = (d->desc_idx + 1) % d->sglen;
989}
990
991static inline void udma_fetch_epib(struct udma_chan *uc, struct udma_desc *d)
992{
993 struct cppi5_host_desc_t *h_desc = d->hwdesc[0].cppi5_desc_vaddr;
994
995 memcpy(d->metadata, h_desc->epib, d->metadata_size);
996}
997
998static bool udma_is_desc_really_done(struct udma_chan *uc, struct udma_desc *d)
999{
1000 u32 peer_bcnt, bcnt;
1001
1002 /* Only TX towards PDMA is affected */
1003 if (uc->config.ep_type == PSIL_EP_NATIVE ||
1004 uc->config.dir != DMA_MEM_TO_DEV)
1005 return true;
1006
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03001007 peer_bcnt = udma_tchanrt_read(uc, UDMA_CHAN_RT_PEER_BCNT_REG);
1008 bcnt = udma_tchanrt_read(uc, UDMA_CHAN_RT_BCNT_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001009
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02001010 /* Transfer is incomplete, store current residue and time stamp */
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001011 if (peer_bcnt < bcnt) {
1012 uc->tx_drain.residue = bcnt - peer_bcnt;
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02001013 uc->tx_drain.tstamp = ktime_get();
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001014 return false;
1015 }
1016
1017 return true;
1018}
1019
1020static void udma_check_tx_completion(struct work_struct *work)
1021{
1022 struct udma_chan *uc = container_of(work, typeof(*uc),
1023 tx_drain.work.work);
1024 bool desc_done = true;
1025 u32 residue_diff;
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02001026 ktime_t time_diff;
1027 unsigned long delay;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001028
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02001029 while (1) {
1030 if (uc->desc) {
1031 /* Get previous residue and time stamp */
1032 residue_diff = uc->tx_drain.residue;
1033 time_diff = uc->tx_drain.tstamp;
1034 /*
1035 * Get current residue and time stamp or see if
1036 * transfer is complete
1037 */
1038 desc_done = udma_is_desc_really_done(uc, uc->desc);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001039 }
1040
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02001041 if (!desc_done) {
1042 /*
1043 * Find the time delta and residue delta w.r.t
1044 * previous poll
1045 */
1046 time_diff = ktime_sub(uc->tx_drain.tstamp,
1047 time_diff) + 1;
1048 residue_diff -= uc->tx_drain.residue;
1049 if (residue_diff) {
1050 /*
1051 * Try to guess when we should check
1052 * next time by calculating rate at
1053 * which data is being drained at the
1054 * peer device
1055 */
1056 delay = (time_diff / residue_diff) *
1057 uc->tx_drain.residue;
1058 } else {
1059 /* No progress, check again in 1 second */
1060 schedule_delayed_work(&uc->tx_drain.work, HZ);
1061 break;
1062 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001063
Vignesh Raghavendra1c837672020-02-14 11:14:36 +02001064 usleep_range(ktime_to_us(delay),
1065 ktime_to_us(delay) + 10);
1066 continue;
1067 }
1068
1069 if (uc->desc) {
1070 struct udma_desc *d = uc->desc;
1071
1072 uc->bcnt += d->residue;
1073 udma_start(uc);
1074 vchan_cookie_complete(&d->vd);
1075 break;
1076 }
1077
1078 break;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001079 }
1080}
1081
1082static irqreturn_t udma_ring_irq_handler(int irq, void *data)
1083{
1084 struct udma_chan *uc = data;
1085 struct udma_desc *d;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001086 dma_addr_t paddr = 0;
1087
1088 if (udma_pop_from_ring(uc, &paddr) || !paddr)
1089 return IRQ_HANDLED;
1090
Barry Songe991c062020-10-28 10:52:44 +13001091 spin_lock(&uc->vc.lock);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001092
1093 /* Teardown completion message */
1094 if (cppi5_desc_is_tdcm(paddr)) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001095 complete_all(&uc->teardown_completed);
1096
1097 if (uc->terminated_desc) {
1098 udma_desc_free(&uc->terminated_desc->vd);
1099 uc->terminated_desc = NULL;
1100 }
1101
1102 if (!uc->desc)
1103 udma_start(uc);
1104
1105 goto out;
1106 }
1107
1108 d = udma_udma_desc_from_paddr(uc, paddr);
1109
1110 if (d) {
1111 dma_addr_t desc_paddr = udma_curr_cppi5_desc_paddr(d,
1112 d->desc_idx);
1113 if (desc_paddr != paddr) {
1114 dev_err(uc->ud->dev, "not matching descriptors!\n");
1115 goto out;
1116 }
1117
Peter Ujfalusi83903182020-02-14 11:14:41 +02001118 if (d == uc->desc) {
1119 /* active descriptor */
1120 if (uc->cyclic) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001121 udma_cyclic_packet_elapsed(uc);
1122 vchan_cyclic_callback(&d->vd);
Peter Ujfalusi83903182020-02-14 11:14:41 +02001123 } else {
1124 if (udma_is_desc_really_done(uc, d)) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001125 uc->bcnt += d->residue;
1126 udma_start(uc);
Peter Ujfalusi83903182020-02-14 11:14:41 +02001127 vchan_cookie_complete(&d->vd);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001128 } else {
1129 schedule_delayed_work(&uc->tx_drain.work,
1130 0);
1131 }
1132 }
Peter Ujfalusi83903182020-02-14 11:14:41 +02001133 } else {
1134 /*
1135 * terminated descriptor, mark the descriptor as
1136 * completed to update the channel's cookie marker
1137 */
1138 dma_cookie_complete(&d->vd.tx);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001139 }
1140 }
1141out:
Barry Songe991c062020-10-28 10:52:44 +13001142 spin_unlock(&uc->vc.lock);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001143
1144 return IRQ_HANDLED;
1145}
1146
1147static irqreturn_t udma_udma_irq_handler(int irq, void *data)
1148{
1149 struct udma_chan *uc = data;
1150 struct udma_desc *d;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001151
Barry Songe991c062020-10-28 10:52:44 +13001152 spin_lock(&uc->vc.lock);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001153 d = uc->desc;
1154 if (d) {
1155 d->tr_idx = (d->tr_idx + 1) % d->sglen;
1156
1157 if (uc->cyclic) {
1158 vchan_cyclic_callback(&d->vd);
1159 } else {
1160 /* TODO: figure out the real amount of data */
1161 uc->bcnt += d->residue;
1162 udma_start(uc);
1163 vchan_cookie_complete(&d->vd);
1164 }
1165 }
1166
Barry Songe991c062020-10-28 10:52:44 +13001167 spin_unlock(&uc->vc.lock);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001168
1169 return IRQ_HANDLED;
1170}
1171
Grygorii Strashkod7024192019-12-23 13:04:51 +02001172/**
1173 * __udma_alloc_gp_rflow_range - alloc range of GP RX flows
1174 * @ud: UDMA device
1175 * @from: Start the search from this flow id number
1176 * @cnt: Number of consecutive flow ids to allocate
1177 *
1178 * Allocate range of RX flow ids for future use, those flows can be requested
1179 * only using explicit flow id number. if @from is set to -1 it will try to find
1180 * first free range. if @from is positive value it will force allocation only
1181 * of the specified range of flows.
1182 *
1183 * Returns -ENOMEM if can't find free range.
1184 * -EEXIST if requested range is busy.
1185 * -EINVAL if wrong input values passed.
1186 * Returns flow id on success.
1187 */
1188static int __udma_alloc_gp_rflow_range(struct udma_dev *ud, int from, int cnt)
1189{
1190 int start, tmp_from;
1191 DECLARE_BITMAP(tmp, K3_UDMA_MAX_RFLOWS);
1192
1193 tmp_from = from;
1194 if (tmp_from < 0)
1195 tmp_from = ud->rchan_cnt;
1196 /* default flows can't be allocated and accessible only by id */
1197 if (tmp_from < ud->rchan_cnt)
1198 return -EINVAL;
1199
1200 if (tmp_from + cnt > ud->rflow_cnt)
1201 return -EINVAL;
1202
1203 bitmap_or(tmp, ud->rflow_gp_map, ud->rflow_gp_map_allocated,
1204 ud->rflow_cnt);
1205
1206 start = bitmap_find_next_zero_area(tmp,
1207 ud->rflow_cnt,
1208 tmp_from, cnt, 0);
1209 if (start >= ud->rflow_cnt)
1210 return -ENOMEM;
1211
1212 if (from >= 0 && start != from)
1213 return -EEXIST;
1214
1215 bitmap_set(ud->rflow_gp_map_allocated, start, cnt);
1216 return start;
1217}
1218
1219static int __udma_free_gp_rflow_range(struct udma_dev *ud, int from, int cnt)
1220{
1221 if (from < ud->rchan_cnt)
1222 return -EINVAL;
1223 if (from + cnt > ud->rflow_cnt)
1224 return -EINVAL;
1225
1226 bitmap_clear(ud->rflow_gp_map_allocated, from, cnt);
1227 return 0;
1228}
1229
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001230static struct udma_rflow *__udma_get_rflow(struct udma_dev *ud, int id)
1231{
1232 /*
1233 * Attempt to request rflow by ID can be made for any rflow
1234 * if not in use with assumption that caller knows what's doing.
1235 * TI-SCI FW will perform additional permission check ant way, it's
1236 * safe
1237 */
1238
1239 if (id < 0 || id >= ud->rflow_cnt)
1240 return ERR_PTR(-ENOENT);
1241
1242 if (test_bit(id, ud->rflow_in_use))
1243 return ERR_PTR(-ENOENT);
1244
1245 /* GP rflow has to be allocated first */
1246 if (!test_bit(id, ud->rflow_gp_map) &&
1247 !test_bit(id, ud->rflow_gp_map_allocated))
1248 return ERR_PTR(-EINVAL);
1249
1250 dev_dbg(ud->dev, "get rflow%d\n", id);
1251 set_bit(id, ud->rflow_in_use);
1252 return &ud->rflows[id];
1253}
1254
1255static void __udma_put_rflow(struct udma_dev *ud, struct udma_rflow *rflow)
1256{
1257 if (!test_bit(rflow->id, ud->rflow_in_use)) {
1258 dev_err(ud->dev, "attempt to put unused rflow%d\n", rflow->id);
1259 return;
1260 }
1261
1262 dev_dbg(ud->dev, "put rflow%d\n", rflow->id);
1263 clear_bit(rflow->id, ud->rflow_in_use);
1264}
1265
1266#define UDMA_RESERVE_RESOURCE(res) \
1267static struct udma_##res *__udma_reserve_##res(struct udma_dev *ud, \
1268 enum udma_tp_level tpl, \
1269 int id) \
1270{ \
1271 if (id >= 0) { \
1272 if (test_bit(id, ud->res##_map)) { \
1273 dev_err(ud->dev, "res##%d is in use\n", id); \
1274 return ERR_PTR(-ENOENT); \
1275 } \
1276 } else { \
1277 int start; \
1278 \
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +03001279 if (tpl >= ud->tpl_levels) \
1280 tpl = ud->tpl_levels - 1; \
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001281 \
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +03001282 start = ud->tpl_start_idx[tpl]; \
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001283 \
1284 id = find_next_zero_bit(ud->res##_map, ud->res##_cnt, \
1285 start); \
1286 if (id == ud->res##_cnt) { \
1287 return ERR_PTR(-ENOENT); \
1288 } \
1289 } \
1290 \
1291 set_bit(id, ud->res##_map); \
1292 return &ud->res##s[id]; \
1293}
1294
1295UDMA_RESERVE_RESOURCE(tchan);
1296UDMA_RESERVE_RESOURCE(rchan);
1297
Peter Ujfalusi01779472020-12-08 11:04:37 +02001298static struct udma_bchan *__bcdma_reserve_bchan(struct udma_dev *ud, int id)
1299{
1300 if (id >= 0) {
1301 if (test_bit(id, ud->bchan_map)) {
1302 dev_err(ud->dev, "bchan%d is in use\n", id);
1303 return ERR_PTR(-ENOENT);
1304 }
1305 } else {
1306 id = find_next_zero_bit(ud->bchan_map, ud->bchan_cnt, 0);
1307 if (id == ud->bchan_cnt)
1308 return ERR_PTR(-ENOENT);
1309 }
1310
1311 set_bit(id, ud->bchan_map);
1312 return &ud->bchans[id];
1313}
1314
1315static int bcdma_get_bchan(struct udma_chan *uc)
1316{
1317 struct udma_dev *ud = uc->ud;
1318
1319 if (uc->bchan) {
1320 dev_dbg(ud->dev, "chan%d: already have bchan%d allocated\n",
1321 uc->id, uc->bchan->id);
1322 return 0;
1323 }
1324
1325 uc->bchan = __bcdma_reserve_bchan(ud, -1);
1326 if (IS_ERR(uc->bchan))
1327 return PTR_ERR(uc->bchan);
1328
1329 uc->tchan = uc->bchan;
1330
1331 return 0;
1332}
1333
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001334static int udma_get_tchan(struct udma_chan *uc)
1335{
1336 struct udma_dev *ud = uc->ud;
1337
1338 if (uc->tchan) {
1339 dev_dbg(ud->dev, "chan%d: already have tchan%d allocated\n",
1340 uc->id, uc->tchan->id);
1341 return 0;
1342 }
1343
1344 uc->tchan = __udma_reserve_tchan(ud, uc->config.channel_tpl, -1);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001345
Samuel Zou214a0002020-05-06 17:25:46 +08001346 return PTR_ERR_OR_ZERO(uc->tchan);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001347}
1348
1349static int udma_get_rchan(struct udma_chan *uc)
1350{
1351 struct udma_dev *ud = uc->ud;
1352
1353 if (uc->rchan) {
1354 dev_dbg(ud->dev, "chan%d: already have rchan%d allocated\n",
1355 uc->id, uc->rchan->id);
1356 return 0;
1357 }
1358
1359 uc->rchan = __udma_reserve_rchan(ud, uc->config.channel_tpl, -1);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001360
Samuel Zou214a0002020-05-06 17:25:46 +08001361 return PTR_ERR_OR_ZERO(uc->rchan);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001362}
1363
1364static int udma_get_chan_pair(struct udma_chan *uc)
1365{
1366 struct udma_dev *ud = uc->ud;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001367 int chan_id, end;
1368
1369 if ((uc->tchan && uc->rchan) && uc->tchan->id == uc->rchan->id) {
1370 dev_info(ud->dev, "chan%d: already have %d pair allocated\n",
1371 uc->id, uc->tchan->id);
1372 return 0;
1373 }
1374
1375 if (uc->tchan) {
1376 dev_err(ud->dev, "chan%d: already have tchan%d allocated\n",
1377 uc->id, uc->tchan->id);
1378 return -EBUSY;
1379 } else if (uc->rchan) {
1380 dev_err(ud->dev, "chan%d: already have rchan%d allocated\n",
1381 uc->id, uc->rchan->id);
1382 return -EBUSY;
1383 }
1384
1385 /* Can be optimized, but let's have it like this for now */
1386 end = min(ud->tchan_cnt, ud->rchan_cnt);
1387 /* Try to use the highest TPL channel pair for MEM_TO_MEM channels */
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +03001388 chan_id = ud->tpl_start_idx[ud->tpl_levels - 1];
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001389 for (; chan_id < end; chan_id++) {
1390 if (!test_bit(chan_id, ud->tchan_map) &&
1391 !test_bit(chan_id, ud->rchan_map))
1392 break;
1393 }
1394
1395 if (chan_id == end)
1396 return -ENOENT;
1397
1398 set_bit(chan_id, ud->tchan_map);
1399 set_bit(chan_id, ud->rchan_map);
1400 uc->tchan = &ud->tchans[chan_id];
1401 uc->rchan = &ud->rchans[chan_id];
1402
1403 return 0;
1404}
1405
1406static int udma_get_rflow(struct udma_chan *uc, int flow_id)
1407{
1408 struct udma_dev *ud = uc->ud;
1409
1410 if (!uc->rchan) {
1411 dev_err(ud->dev, "chan%d: does not have rchan??\n", uc->id);
1412 return -EINVAL;
1413 }
1414
1415 if (uc->rflow) {
1416 dev_dbg(ud->dev, "chan%d: already have rflow%d allocated\n",
1417 uc->id, uc->rflow->id);
1418 return 0;
1419 }
1420
1421 uc->rflow = __udma_get_rflow(ud, flow_id);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001422
Samuel Zou214a0002020-05-06 17:25:46 +08001423 return PTR_ERR_OR_ZERO(uc->rflow);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001424}
1425
Peter Ujfalusi01779472020-12-08 11:04:37 +02001426static void bcdma_put_bchan(struct udma_chan *uc)
1427{
1428 struct udma_dev *ud = uc->ud;
1429
1430 if (uc->bchan) {
1431 dev_dbg(ud->dev, "chan%d: put bchan%d\n", uc->id,
1432 uc->bchan->id);
1433 clear_bit(uc->bchan->id, ud->bchan_map);
1434 uc->bchan = NULL;
1435 uc->tchan = NULL;
1436 }
1437}
1438
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001439static void udma_put_rchan(struct udma_chan *uc)
1440{
1441 struct udma_dev *ud = uc->ud;
1442
1443 if (uc->rchan) {
1444 dev_dbg(ud->dev, "chan%d: put rchan%d\n", uc->id,
1445 uc->rchan->id);
1446 clear_bit(uc->rchan->id, ud->rchan_map);
1447 uc->rchan = NULL;
1448 }
1449}
1450
1451static void udma_put_tchan(struct udma_chan *uc)
1452{
1453 struct udma_dev *ud = uc->ud;
1454
1455 if (uc->tchan) {
1456 dev_dbg(ud->dev, "chan%d: put tchan%d\n", uc->id,
1457 uc->tchan->id);
1458 clear_bit(uc->tchan->id, ud->tchan_map);
1459 uc->tchan = NULL;
1460 }
1461}
1462
1463static void udma_put_rflow(struct udma_chan *uc)
1464{
1465 struct udma_dev *ud = uc->ud;
1466
1467 if (uc->rflow) {
1468 dev_dbg(ud->dev, "chan%d: put rflow%d\n", uc->id,
1469 uc->rflow->id);
1470 __udma_put_rflow(ud, uc->rflow);
1471 uc->rflow = NULL;
1472 }
1473}
1474
Peter Ujfalusi01779472020-12-08 11:04:37 +02001475static void bcdma_free_bchan_resources(struct udma_chan *uc)
1476{
1477 if (!uc->bchan)
1478 return;
1479
1480 k3_ringacc_ring_free(uc->bchan->tc_ring);
1481 k3_ringacc_ring_free(uc->bchan->t_ring);
1482 uc->bchan->tc_ring = NULL;
1483 uc->bchan->t_ring = NULL;
1484 k3_configure_chan_coherency(&uc->vc.chan, 0);
1485
1486 bcdma_put_bchan(uc);
1487}
1488
1489static int bcdma_alloc_bchan_resources(struct udma_chan *uc)
1490{
1491 struct k3_ring_cfg ring_cfg;
1492 struct udma_dev *ud = uc->ud;
1493 int ret;
1494
1495 ret = bcdma_get_bchan(uc);
1496 if (ret)
1497 return ret;
1498
1499 ret = k3_ringacc_request_rings_pair(ud->ringacc, uc->bchan->id, -1,
1500 &uc->bchan->t_ring,
1501 &uc->bchan->tc_ring);
1502 if (ret) {
1503 ret = -EBUSY;
1504 goto err_ring;
1505 }
1506
1507 memset(&ring_cfg, 0, sizeof(ring_cfg));
1508 ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1509 ring_cfg.elm_size = K3_RINGACC_RING_ELSIZE_8;
1510 ring_cfg.mode = K3_RINGACC_RING_MODE_RING;
1511
1512 k3_configure_chan_coherency(&uc->vc.chan, ud->asel);
1513 ring_cfg.asel = ud->asel;
1514 ring_cfg.dma_dev = dmaengine_get_dma_device(&uc->vc.chan);
1515
1516 ret = k3_ringacc_ring_cfg(uc->bchan->t_ring, &ring_cfg);
1517 if (ret)
1518 goto err_ringcfg;
1519
1520 return 0;
1521
1522err_ringcfg:
1523 k3_ringacc_ring_free(uc->bchan->tc_ring);
1524 uc->bchan->tc_ring = NULL;
1525 k3_ringacc_ring_free(uc->bchan->t_ring);
1526 uc->bchan->t_ring = NULL;
1527 k3_configure_chan_coherency(&uc->vc.chan, 0);
1528err_ring:
1529 bcdma_put_bchan(uc);
1530
1531 return ret;
1532}
1533
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001534static void udma_free_tx_resources(struct udma_chan *uc)
1535{
1536 if (!uc->tchan)
1537 return;
1538
1539 k3_ringacc_ring_free(uc->tchan->t_ring);
1540 k3_ringacc_ring_free(uc->tchan->tc_ring);
1541 uc->tchan->t_ring = NULL;
1542 uc->tchan->tc_ring = NULL;
1543
1544 udma_put_tchan(uc);
1545}
1546
1547static int udma_alloc_tx_resources(struct udma_chan *uc)
1548{
1549 struct k3_ring_cfg ring_cfg;
1550 struct udma_dev *ud = uc->ud;
Peter Ujfalusi01779472020-12-08 11:04:37 +02001551 struct udma_tchan *tchan;
1552 int ring_idx, ret;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001553
1554 ret = udma_get_tchan(uc);
1555 if (ret)
1556 return ret;
1557
Peter Ujfalusi01779472020-12-08 11:04:37 +02001558 tchan = uc->tchan;
1559 ring_idx = ud->bchan_cnt + tchan->id;
1560
1561 ret = k3_ringacc_request_rings_pair(ud->ringacc, ring_idx, -1,
1562 &tchan->t_ring,
1563 &tchan->tc_ring);
Peter Ujfalusi4927b1a2020-07-24 14:20:24 -07001564 if (ret) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001565 ret = -EBUSY;
Peter Ujfalusi4927b1a2020-07-24 14:20:24 -07001566 goto err_ring;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001567 }
1568
1569 memset(&ring_cfg, 0, sizeof(ring_cfg));
1570 ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1571 ring_cfg.elm_size = K3_RINGACC_RING_ELSIZE_8;
Peter Ujfalusi01779472020-12-08 11:04:37 +02001572 if (ud->match_data->type == DMA_TYPE_UDMA) {
1573 ring_cfg.mode = K3_RINGACC_RING_MODE_MESSAGE;
1574 } else {
1575 ring_cfg.mode = K3_RINGACC_RING_MODE_RING;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001576
Peter Ujfalusi01779472020-12-08 11:04:37 +02001577 k3_configure_chan_coherency(&uc->vc.chan, uc->config.asel);
1578 ring_cfg.asel = uc->config.asel;
1579 ring_cfg.dma_dev = dmaengine_get_dma_device(&uc->vc.chan);
1580 }
1581
1582 ret = k3_ringacc_ring_cfg(tchan->t_ring, &ring_cfg);
1583 ret |= k3_ringacc_ring_cfg(tchan->tc_ring, &ring_cfg);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001584
1585 if (ret)
1586 goto err_ringcfg;
1587
1588 return 0;
1589
1590err_ringcfg:
1591 k3_ringacc_ring_free(uc->tchan->tc_ring);
1592 uc->tchan->tc_ring = NULL;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001593 k3_ringacc_ring_free(uc->tchan->t_ring);
1594 uc->tchan->t_ring = NULL;
Peter Ujfalusi4927b1a2020-07-24 14:20:24 -07001595err_ring:
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001596 udma_put_tchan(uc);
1597
1598 return ret;
1599}
1600
1601static void udma_free_rx_resources(struct udma_chan *uc)
1602{
1603 if (!uc->rchan)
1604 return;
1605
1606 if (uc->rflow) {
1607 struct udma_rflow *rflow = uc->rflow;
1608
1609 k3_ringacc_ring_free(rflow->fd_ring);
1610 k3_ringacc_ring_free(rflow->r_ring);
1611 rflow->fd_ring = NULL;
1612 rflow->r_ring = NULL;
1613
1614 udma_put_rflow(uc);
1615 }
1616
1617 udma_put_rchan(uc);
1618}
1619
1620static int udma_alloc_rx_resources(struct udma_chan *uc)
1621{
1622 struct udma_dev *ud = uc->ud;
1623 struct k3_ring_cfg ring_cfg;
1624 struct udma_rflow *rflow;
1625 int fd_ring_id;
1626 int ret;
1627
1628 ret = udma_get_rchan(uc);
1629 if (ret)
1630 return ret;
1631
1632 /* For MEM_TO_MEM we don't need rflow or rings */
1633 if (uc->config.dir == DMA_MEM_TO_MEM)
1634 return 0;
1635
1636 ret = udma_get_rflow(uc, uc->rchan->id);
1637 if (ret) {
1638 ret = -EBUSY;
1639 goto err_rflow;
1640 }
1641
1642 rflow = uc->rflow;
Peter Ujfalusi01779472020-12-08 11:04:37 +02001643 fd_ring_id = ud->bchan_cnt + ud->tchan_cnt + ud->echan_cnt +
1644 uc->rchan->id;
Peter Ujfalusi4927b1a2020-07-24 14:20:24 -07001645 ret = k3_ringacc_request_rings_pair(ud->ringacc, fd_ring_id, -1,
1646 &rflow->fd_ring, &rflow->r_ring);
1647 if (ret) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001648 ret = -EBUSY;
Peter Ujfalusi4927b1a2020-07-24 14:20:24 -07001649 goto err_ring;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001650 }
1651
1652 memset(&ring_cfg, 0, sizeof(ring_cfg));
1653
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001654 ring_cfg.elm_size = K3_RINGACC_RING_ELSIZE_8;
Peter Ujfalusi01779472020-12-08 11:04:37 +02001655 if (ud->match_data->type == DMA_TYPE_UDMA) {
1656 if (uc->config.pkt_mode)
1657 ring_cfg.size = SG_MAX_SEGMENTS;
1658 else
1659 ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1660
1661 ring_cfg.mode = K3_RINGACC_RING_MODE_MESSAGE;
1662 } else {
1663 ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1664 ring_cfg.mode = K3_RINGACC_RING_MODE_RING;
1665
1666 k3_configure_chan_coherency(&uc->vc.chan, uc->config.asel);
1667 ring_cfg.asel = uc->config.asel;
1668 ring_cfg.dma_dev = dmaengine_get_dma_device(&uc->vc.chan);
1669 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001670
1671 ret = k3_ringacc_ring_cfg(rflow->fd_ring, &ring_cfg);
Peter Ujfalusi01779472020-12-08 11:04:37 +02001672
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001673 ring_cfg.size = K3_UDMA_DEFAULT_RING_SIZE;
1674 ret |= k3_ringacc_ring_cfg(rflow->r_ring, &ring_cfg);
1675
1676 if (ret)
1677 goto err_ringcfg;
1678
1679 return 0;
1680
1681err_ringcfg:
1682 k3_ringacc_ring_free(rflow->r_ring);
1683 rflow->r_ring = NULL;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001684 k3_ringacc_ring_free(rflow->fd_ring);
1685 rflow->fd_ring = NULL;
Peter Ujfalusi4927b1a2020-07-24 14:20:24 -07001686err_ring:
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001687 udma_put_rflow(uc);
1688err_rflow:
1689 udma_put_rchan(uc);
1690
1691 return ret;
1692}
1693
Peter Ujfalusi01779472020-12-08 11:04:37 +02001694#define TISCI_BCDMA_BCHAN_VALID_PARAMS ( \
1695 TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID | \
1696 TI_SCI_MSG_VALUE_RM_UDMAP_CH_EXTENDED_CH_TYPE_VALID)
1697
1698#define TISCI_BCDMA_TCHAN_VALID_PARAMS ( \
1699 TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID | \
1700 TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_SUPR_TDPKT_VALID)
1701
1702#define TISCI_BCDMA_RCHAN_VALID_PARAMS ( \
1703 TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID)
1704
1705#define TISCI_UDMA_TCHAN_VALID_PARAMS ( \
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001706 TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID | \
1707 TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_FILT_EINFO_VALID | \
1708 TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_FILT_PSWORDS_VALID | \
1709 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CHAN_TYPE_VALID | \
1710 TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_SUPR_TDPKT_VALID | \
1711 TI_SCI_MSG_VALUE_RM_UDMAP_CH_FETCH_SIZE_VALID | \
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02001712 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CQ_QNUM_VALID | \
1713 TI_SCI_MSG_VALUE_RM_UDMAP_CH_ATYPE_VALID)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001714
Peter Ujfalusi01779472020-12-08 11:04:37 +02001715#define TISCI_UDMA_RCHAN_VALID_PARAMS ( \
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001716 TI_SCI_MSG_VALUE_RM_UDMAP_CH_PAUSE_ON_ERR_VALID | \
1717 TI_SCI_MSG_VALUE_RM_UDMAP_CH_FETCH_SIZE_VALID | \
1718 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CQ_QNUM_VALID | \
1719 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CHAN_TYPE_VALID | \
1720 TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_IGNORE_SHORT_VALID | \
1721 TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_IGNORE_LONG_VALID | \
1722 TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_FLOWID_START_VALID | \
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02001723 TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_FLOWID_CNT_VALID | \
1724 TI_SCI_MSG_VALUE_RM_UDMAP_CH_ATYPE_VALID)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001725
1726static int udma_tisci_m2m_channel_config(struct udma_chan *uc)
1727{
1728 struct udma_dev *ud = uc->ud;
1729 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1730 const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1731 struct udma_tchan *tchan = uc->tchan;
1732 struct udma_rchan *rchan = uc->rchan;
1733 int ret = 0;
1734
1735 /* Non synchronized - mem to mem type of transfer */
1736 int tc_ring = k3_ringacc_get_ring_id(tchan->tc_ring);
1737 struct ti_sci_msg_rm_udmap_tx_ch_cfg req_tx = { 0 };
1738 struct ti_sci_msg_rm_udmap_rx_ch_cfg req_rx = { 0 };
1739
Peter Ujfalusi01779472020-12-08 11:04:37 +02001740 req_tx.valid_params = TISCI_UDMA_TCHAN_VALID_PARAMS;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001741 req_tx.nav_id = tisci_rm->tisci_dev_id;
1742 req_tx.index = tchan->id;
1743 req_tx.tx_chan_type = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_BCOPY_PBRR;
1744 req_tx.tx_fetch_size = sizeof(struct cppi5_desc_hdr_t) >> 2;
1745 req_tx.txcq_qnum = tc_ring;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02001746 req_tx.tx_atype = ud->atype;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001747
1748 ret = tisci_ops->tx_ch_cfg(tisci_rm->tisci, &req_tx);
1749 if (ret) {
1750 dev_err(ud->dev, "tchan%d cfg failed %d\n", tchan->id, ret);
1751 return ret;
1752 }
1753
Peter Ujfalusi01779472020-12-08 11:04:37 +02001754 req_rx.valid_params = TISCI_UDMA_RCHAN_VALID_PARAMS;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001755 req_rx.nav_id = tisci_rm->tisci_dev_id;
1756 req_rx.index = rchan->id;
1757 req_rx.rx_fetch_size = sizeof(struct cppi5_desc_hdr_t) >> 2;
1758 req_rx.rxcq_qnum = tc_ring;
1759 req_rx.rx_chan_type = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_BCOPY_PBRR;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02001760 req_rx.rx_atype = ud->atype;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001761
1762 ret = tisci_ops->rx_ch_cfg(tisci_rm->tisci, &req_rx);
1763 if (ret)
1764 dev_err(ud->dev, "rchan%d alloc failed %d\n", rchan->id, ret);
1765
1766 return ret;
1767}
1768
Peter Ujfalusi01779472020-12-08 11:04:37 +02001769static int bcdma_tisci_m2m_channel_config(struct udma_chan *uc)
1770{
1771 struct udma_dev *ud = uc->ud;
1772 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1773 const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1774 struct ti_sci_msg_rm_udmap_tx_ch_cfg req_tx = { 0 };
1775 struct udma_bchan *bchan = uc->bchan;
1776 int ret = 0;
1777
1778 req_tx.valid_params = TISCI_BCDMA_BCHAN_VALID_PARAMS;
1779 req_tx.nav_id = tisci_rm->tisci_dev_id;
1780 req_tx.extended_ch_type = TI_SCI_RM_BCDMA_EXTENDED_CH_TYPE_BCHAN;
1781 req_tx.index = bchan->id;
1782
1783 ret = tisci_ops->tx_ch_cfg(tisci_rm->tisci, &req_tx);
1784 if (ret)
1785 dev_err(ud->dev, "bchan%d cfg failed %d\n", bchan->id, ret);
1786
1787 return ret;
1788}
1789
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001790static int udma_tisci_tx_channel_config(struct udma_chan *uc)
1791{
1792 struct udma_dev *ud = uc->ud;
1793 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1794 const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1795 struct udma_tchan *tchan = uc->tchan;
1796 int tc_ring = k3_ringacc_get_ring_id(tchan->tc_ring);
1797 struct ti_sci_msg_rm_udmap_tx_ch_cfg req_tx = { 0 };
1798 u32 mode, fetch_size;
1799 int ret = 0;
1800
1801 if (uc->config.pkt_mode) {
1802 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_PKT_PBRR;
1803 fetch_size = cppi5_hdesc_calc_size(uc->config.needs_epib,
1804 uc->config.psd_size, 0);
1805 } else {
1806 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_PBRR;
1807 fetch_size = sizeof(struct cppi5_desc_hdr_t);
1808 }
1809
Peter Ujfalusi01779472020-12-08 11:04:37 +02001810 req_tx.valid_params = TISCI_UDMA_TCHAN_VALID_PARAMS;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001811 req_tx.nav_id = tisci_rm->tisci_dev_id;
1812 req_tx.index = tchan->id;
1813 req_tx.tx_chan_type = mode;
1814 req_tx.tx_supr_tdpkt = uc->config.notdpkt;
1815 req_tx.tx_fetch_size = fetch_size >> 2;
1816 req_tx.txcq_qnum = tc_ring;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02001817 req_tx.tx_atype = uc->config.atype;
Peter Ujfalusi5e1cb1c2020-12-08 11:04:22 +02001818 if (uc->config.ep_type == PSIL_EP_PDMA_XY &&
1819 ud->match_data->flags & UDMA_FLAG_TDTYPE) {
1820 /* wait for peer to complete the teardown for PDMAs */
1821 req_tx.valid_params |=
1822 TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_TDTYPE_VALID;
1823 req_tx.tx_tdtype = 1;
1824 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001825
1826 ret = tisci_ops->tx_ch_cfg(tisci_rm->tisci, &req_tx);
1827 if (ret)
1828 dev_err(ud->dev, "tchan%d cfg failed %d\n", tchan->id, ret);
1829
1830 return ret;
1831}
1832
Peter Ujfalusi01779472020-12-08 11:04:37 +02001833static int bcdma_tisci_tx_channel_config(struct udma_chan *uc)
1834{
1835 struct udma_dev *ud = uc->ud;
1836 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1837 const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1838 struct udma_tchan *tchan = uc->tchan;
1839 struct ti_sci_msg_rm_udmap_tx_ch_cfg req_tx = { 0 };
1840 int ret = 0;
1841
1842 req_tx.valid_params = TISCI_BCDMA_TCHAN_VALID_PARAMS;
1843 req_tx.nav_id = tisci_rm->tisci_dev_id;
1844 req_tx.index = tchan->id;
1845 req_tx.tx_supr_tdpkt = uc->config.notdpkt;
1846 if (ud->match_data->flags & UDMA_FLAG_TDTYPE) {
1847 /* wait for peer to complete the teardown for PDMAs */
1848 req_tx.valid_params |=
1849 TI_SCI_MSG_VALUE_RM_UDMAP_CH_TX_TDTYPE_VALID;
1850 req_tx.tx_tdtype = 1;
1851 }
1852
1853 ret = tisci_ops->tx_ch_cfg(tisci_rm->tisci, &req_tx);
1854 if (ret)
1855 dev_err(ud->dev, "tchan%d cfg failed %d\n", tchan->id, ret);
1856
1857 return ret;
1858}
1859
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001860static int udma_tisci_rx_channel_config(struct udma_chan *uc)
1861{
1862 struct udma_dev *ud = uc->ud;
1863 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1864 const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1865 struct udma_rchan *rchan = uc->rchan;
1866 int fd_ring = k3_ringacc_get_ring_id(uc->rflow->fd_ring);
1867 int rx_ring = k3_ringacc_get_ring_id(uc->rflow->r_ring);
1868 struct ti_sci_msg_rm_udmap_rx_ch_cfg req_rx = { 0 };
1869 struct ti_sci_msg_rm_udmap_flow_cfg flow_req = { 0 };
1870 u32 mode, fetch_size;
1871 int ret = 0;
1872
1873 if (uc->config.pkt_mode) {
1874 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_PKT_PBRR;
1875 fetch_size = cppi5_hdesc_calc_size(uc->config.needs_epib,
1876 uc->config.psd_size, 0);
1877 } else {
1878 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_PBRR;
1879 fetch_size = sizeof(struct cppi5_desc_hdr_t);
1880 }
1881
Peter Ujfalusi01779472020-12-08 11:04:37 +02001882 req_rx.valid_params = TISCI_UDMA_RCHAN_VALID_PARAMS;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001883 req_rx.nav_id = tisci_rm->tisci_dev_id;
1884 req_rx.index = rchan->id;
1885 req_rx.rx_fetch_size = fetch_size >> 2;
1886 req_rx.rxcq_qnum = rx_ring;
1887 req_rx.rx_chan_type = mode;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02001888 req_rx.rx_atype = uc->config.atype;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001889
1890 ret = tisci_ops->rx_ch_cfg(tisci_rm->tisci, &req_rx);
1891 if (ret) {
1892 dev_err(ud->dev, "rchan%d cfg failed %d\n", rchan->id, ret);
1893 return ret;
1894 }
1895
1896 flow_req.valid_params =
1897 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_EINFO_PRESENT_VALID |
1898 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_PSINFO_PRESENT_VALID |
1899 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_ERROR_HANDLING_VALID |
1900 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DESC_TYPE_VALID |
1901 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_QNUM_VALID |
1902 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_SRC_TAG_HI_SEL_VALID |
1903 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_SRC_TAG_LO_SEL_VALID |
1904 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_TAG_HI_SEL_VALID |
1905 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_TAG_LO_SEL_VALID |
1906 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ0_SZ0_QNUM_VALID |
1907 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ1_QNUM_VALID |
1908 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ2_QNUM_VALID |
1909 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ3_QNUM_VALID;
1910
1911 flow_req.nav_id = tisci_rm->tisci_dev_id;
1912 flow_req.flow_index = rchan->id;
1913
1914 if (uc->config.needs_epib)
1915 flow_req.rx_einfo_present = 1;
1916 else
1917 flow_req.rx_einfo_present = 0;
1918 if (uc->config.psd_size)
1919 flow_req.rx_psinfo_present = 1;
1920 else
1921 flow_req.rx_psinfo_present = 0;
1922 flow_req.rx_error_handling = 1;
1923 flow_req.rx_dest_qnum = rx_ring;
1924 flow_req.rx_src_tag_hi_sel = UDMA_RFLOW_SRCTAG_NONE;
1925 flow_req.rx_src_tag_lo_sel = UDMA_RFLOW_SRCTAG_SRC_TAG;
1926 flow_req.rx_dest_tag_hi_sel = UDMA_RFLOW_DSTTAG_DST_TAG_HI;
1927 flow_req.rx_dest_tag_lo_sel = UDMA_RFLOW_DSTTAG_DST_TAG_LO;
1928 flow_req.rx_fdq0_sz0_qnum = fd_ring;
1929 flow_req.rx_fdq1_qnum = fd_ring;
1930 flow_req.rx_fdq2_qnum = fd_ring;
1931 flow_req.rx_fdq3_qnum = fd_ring;
1932
1933 ret = tisci_ops->rx_flow_cfg(tisci_rm->tisci, &flow_req);
1934
1935 if (ret)
1936 dev_err(ud->dev, "flow%d config failed: %d\n", rchan->id, ret);
1937
1938 return 0;
1939}
1940
Peter Ujfalusi01779472020-12-08 11:04:37 +02001941static int bcdma_tisci_rx_channel_config(struct udma_chan *uc)
1942{
1943 struct udma_dev *ud = uc->ud;
1944 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1945 const struct ti_sci_rm_udmap_ops *tisci_ops = tisci_rm->tisci_udmap_ops;
1946 struct udma_rchan *rchan = uc->rchan;
1947 struct ti_sci_msg_rm_udmap_rx_ch_cfg req_rx = { 0 };
1948 int ret = 0;
1949
1950 req_rx.valid_params = TISCI_BCDMA_RCHAN_VALID_PARAMS;
1951 req_rx.nav_id = tisci_rm->tisci_dev_id;
1952 req_rx.index = rchan->id;
1953
1954 ret = tisci_ops->rx_ch_cfg(tisci_rm->tisci, &req_rx);
1955 if (ret)
1956 dev_err(ud->dev, "rchan%d cfg failed %d\n", rchan->id, ret);
1957
1958 return ret;
1959}
1960
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001961static int udma_alloc_chan_resources(struct dma_chan *chan)
1962{
1963 struct udma_chan *uc = to_udma_chan(chan);
1964 struct udma_dev *ud = to_udma_dev(chan->device);
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03001965 const struct udma_soc_data *soc_data = ud->soc_data;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001966 struct k3_ring *irq_ring;
1967 u32 irq_udma_idx;
1968 int ret;
1969
Peter Ujfalusi01779472020-12-08 11:04:37 +02001970 uc->dma_dev = ud->dev;
1971
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001972 if (uc->config.pkt_mode || uc->config.dir == DMA_MEM_TO_MEM) {
1973 uc->use_dma_pool = true;
1974 /* in case of MEM_TO_MEM we have maximum of two TRs */
1975 if (uc->config.dir == DMA_MEM_TO_MEM) {
1976 uc->config.hdesc_size = cppi5_trdesc_calc_size(
1977 sizeof(struct cppi5_tr_type15_t), 2);
1978 uc->config.pkt_mode = false;
1979 }
1980 }
1981
1982 if (uc->use_dma_pool) {
1983 uc->hdesc_pool = dma_pool_create(uc->name, ud->ddev.dev,
1984 uc->config.hdesc_size,
1985 ud->desc_align,
1986 0);
1987 if (!uc->hdesc_pool) {
1988 dev_err(ud->ddev.dev,
1989 "Descriptor pool allocation failed\n");
1990 uc->use_dma_pool = false;
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03001991 ret = -ENOMEM;
1992 goto err_cleanup;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02001993 }
1994 }
1995
1996 /*
1997 * Make sure that the completion is in a known state:
1998 * No teardown, the channel is idle
1999 */
2000 reinit_completion(&uc->teardown_completed);
2001 complete_all(&uc->teardown_completed);
2002 uc->state = UDMA_CHAN_IS_IDLE;
2003
2004 switch (uc->config.dir) {
2005 case DMA_MEM_TO_MEM:
2006 /* Non synchronized - mem to mem type of transfer */
2007 dev_dbg(uc->ud->dev, "%s: chan%d as MEM-to-MEM\n", __func__,
2008 uc->id);
2009
2010 ret = udma_get_chan_pair(uc);
2011 if (ret)
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002012 goto err_cleanup;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002013
2014 ret = udma_alloc_tx_resources(uc);
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002015 if (ret) {
2016 udma_put_rchan(uc);
2017 goto err_cleanup;
2018 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002019
2020 ret = udma_alloc_rx_resources(uc);
2021 if (ret) {
2022 udma_free_tx_resources(uc);
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002023 goto err_cleanup;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002024 }
2025
2026 uc->config.src_thread = ud->psil_base + uc->tchan->id;
2027 uc->config.dst_thread = (ud->psil_base + uc->rchan->id) |
2028 K3_PSIL_DST_THREAD_ID_OFFSET;
2029
2030 irq_ring = uc->tchan->tc_ring;
2031 irq_udma_idx = uc->tchan->id;
2032
2033 ret = udma_tisci_m2m_channel_config(uc);
2034 break;
2035 case DMA_MEM_TO_DEV:
2036 /* Slave transfer synchronized - mem to dev (TX) trasnfer */
2037 dev_dbg(uc->ud->dev, "%s: chan%d as MEM-to-DEV\n", __func__,
2038 uc->id);
2039
2040 ret = udma_alloc_tx_resources(uc);
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002041 if (ret)
2042 goto err_cleanup;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002043
2044 uc->config.src_thread = ud->psil_base + uc->tchan->id;
2045 uc->config.dst_thread = uc->config.remote_thread_id;
2046 uc->config.dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
2047
2048 irq_ring = uc->tchan->tc_ring;
2049 irq_udma_idx = uc->tchan->id;
2050
2051 ret = udma_tisci_tx_channel_config(uc);
2052 break;
2053 case DMA_DEV_TO_MEM:
2054 /* Slave transfer synchronized - dev to mem (RX) trasnfer */
2055 dev_dbg(uc->ud->dev, "%s: chan%d as DEV-to-MEM\n", __func__,
2056 uc->id);
2057
2058 ret = udma_alloc_rx_resources(uc);
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002059 if (ret)
2060 goto err_cleanup;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002061
2062 uc->config.src_thread = uc->config.remote_thread_id;
2063 uc->config.dst_thread = (ud->psil_base + uc->rchan->id) |
2064 K3_PSIL_DST_THREAD_ID_OFFSET;
2065
2066 irq_ring = uc->rflow->r_ring;
Peter Ujfalusi01779472020-12-08 11:04:37 +02002067 irq_udma_idx = soc_data->oes.udma_rchan + uc->rchan->id;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002068
2069 ret = udma_tisci_rx_channel_config(uc);
2070 break;
2071 default:
2072 /* Can not happen */
2073 dev_err(uc->ud->dev, "%s: chan%d invalid direction (%u)\n",
2074 __func__, uc->id, uc->config.dir);
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002075 ret = -EINVAL;
2076 goto err_cleanup;
2077
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002078 }
2079
2080 /* check if the channel configuration was successful */
2081 if (ret)
2082 goto err_res_free;
2083
2084 if (udma_is_chan_running(uc)) {
2085 dev_warn(ud->dev, "chan%d: is running!\n", uc->id);
Peter Ujfalusib5b01802020-05-27 10:06:12 +03002086 udma_reset_chan(uc, false);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002087 if (udma_is_chan_running(uc)) {
2088 dev_err(ud->dev, "chan%d: won't stop!\n", uc->id);
Peter Ujfalusi7ae6d7b2020-05-12 16:45:19 +03002089 ret = -EBUSY;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002090 goto err_res_free;
2091 }
2092 }
2093
2094 /* PSI-L pairing */
2095 ret = navss_psil_pair(ud, uc->config.src_thread, uc->config.dst_thread);
2096 if (ret) {
2097 dev_err(ud->dev, "PSI-L pairing failed: 0x%04x -> 0x%04x\n",
2098 uc->config.src_thread, uc->config.dst_thread);
2099 goto err_res_free;
2100 }
2101
2102 uc->psil_paired = true;
2103
2104 uc->irq_num_ring = k3_ringacc_get_ring_irq_num(irq_ring);
2105 if (uc->irq_num_ring <= 0) {
2106 dev_err(ud->dev, "Failed to get ring irq (index: %u)\n",
2107 k3_ringacc_get_ring_id(irq_ring));
2108 ret = -EINVAL;
2109 goto err_psi_free;
2110 }
2111
2112 ret = request_irq(uc->irq_num_ring, udma_ring_irq_handler,
2113 IRQF_TRIGGER_HIGH, uc->name, uc);
2114 if (ret) {
2115 dev_err(ud->dev, "chan%d: ring irq request failed\n", uc->id);
2116 goto err_irq_free;
2117 }
2118
2119 /* Event from UDMA (TR events) only needed for slave TR mode channels */
2120 if (is_slave_direction(uc->config.dir) && !uc->config.pkt_mode) {
2121 uc->irq_num_udma = ti_sci_inta_msi_get_virq(ud->dev,
2122 irq_udma_idx);
2123 if (uc->irq_num_udma <= 0) {
2124 dev_err(ud->dev, "Failed to get udma irq (index: %u)\n",
2125 irq_udma_idx);
2126 free_irq(uc->irq_num_ring, uc);
2127 ret = -EINVAL;
2128 goto err_irq_free;
2129 }
2130
2131 ret = request_irq(uc->irq_num_udma, udma_udma_irq_handler, 0,
2132 uc->name, uc);
2133 if (ret) {
2134 dev_err(ud->dev, "chan%d: UDMA irq request failed\n",
2135 uc->id);
2136 free_irq(uc->irq_num_ring, uc);
2137 goto err_irq_free;
2138 }
2139 } else {
2140 uc->irq_num_udma = 0;
2141 }
2142
2143 udma_reset_rings(uc);
2144
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002145 return 0;
2146
2147err_irq_free:
2148 uc->irq_num_ring = 0;
2149 uc->irq_num_udma = 0;
2150err_psi_free:
2151 navss_psil_unpair(ud, uc->config.src_thread, uc->config.dst_thread);
2152 uc->psil_paired = false;
2153err_res_free:
2154 udma_free_tx_resources(uc);
2155 udma_free_rx_resources(uc);
Peter Ujfalusi5a9377c2020-05-27 10:06:11 +03002156err_cleanup:
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002157 udma_reset_uchan(uc);
2158
2159 if (uc->use_dma_pool) {
2160 dma_pool_destroy(uc->hdesc_pool);
2161 uc->use_dma_pool = false;
2162 }
2163
2164 return ret;
2165}
2166
Peter Ujfalusi01779472020-12-08 11:04:37 +02002167static int bcdma_alloc_chan_resources(struct dma_chan *chan)
2168{
2169 struct udma_chan *uc = to_udma_chan(chan);
2170 struct udma_dev *ud = to_udma_dev(chan->device);
2171 const struct udma_oes_offsets *oes = &ud->soc_data->oes;
2172 u32 irq_udma_idx, irq_ring_idx;
2173 int ret;
2174
2175 /* Only TR mode is supported */
2176 uc->config.pkt_mode = false;
2177
2178 /*
2179 * Make sure that the completion is in a known state:
2180 * No teardown, the channel is idle
2181 */
2182 reinit_completion(&uc->teardown_completed);
2183 complete_all(&uc->teardown_completed);
2184 uc->state = UDMA_CHAN_IS_IDLE;
2185
2186 switch (uc->config.dir) {
2187 case DMA_MEM_TO_MEM:
2188 /* Non synchronized - mem to mem type of transfer */
2189 dev_dbg(uc->ud->dev, "%s: chan%d as MEM-to-MEM\n", __func__,
2190 uc->id);
2191
2192 ret = bcdma_alloc_bchan_resources(uc);
2193 if (ret)
2194 return ret;
2195
2196 irq_ring_idx = uc->bchan->id + oes->bcdma_bchan_ring;
2197 irq_udma_idx = uc->bchan->id + oes->bcdma_bchan_data;
2198
2199 ret = bcdma_tisci_m2m_channel_config(uc);
2200 break;
2201 case DMA_MEM_TO_DEV:
2202 /* Slave transfer synchronized - mem to dev (TX) trasnfer */
2203 dev_dbg(uc->ud->dev, "%s: chan%d as MEM-to-DEV\n", __func__,
2204 uc->id);
2205
2206 ret = udma_alloc_tx_resources(uc);
2207 if (ret) {
2208 uc->config.remote_thread_id = -1;
2209 return ret;
2210 }
2211
2212 uc->config.src_thread = ud->psil_base + uc->tchan->id;
2213 uc->config.dst_thread = uc->config.remote_thread_id;
2214 uc->config.dst_thread |= K3_PSIL_DST_THREAD_ID_OFFSET;
2215
2216 irq_ring_idx = uc->tchan->id + oes->bcdma_tchan_ring;
2217 irq_udma_idx = uc->tchan->id + oes->bcdma_tchan_data;
2218
2219 ret = bcdma_tisci_tx_channel_config(uc);
2220 break;
2221 case DMA_DEV_TO_MEM:
2222 /* Slave transfer synchronized - dev to mem (RX) trasnfer */
2223 dev_dbg(uc->ud->dev, "%s: chan%d as DEV-to-MEM\n", __func__,
2224 uc->id);
2225
2226 ret = udma_alloc_rx_resources(uc);
2227 if (ret) {
2228 uc->config.remote_thread_id = -1;
2229 return ret;
2230 }
2231
2232 uc->config.src_thread = uc->config.remote_thread_id;
2233 uc->config.dst_thread = (ud->psil_base + uc->rchan->id) |
2234 K3_PSIL_DST_THREAD_ID_OFFSET;
2235
2236 irq_ring_idx = uc->rchan->id + oes->bcdma_rchan_ring;
2237 irq_udma_idx = uc->rchan->id + oes->bcdma_rchan_data;
2238
2239 ret = bcdma_tisci_rx_channel_config(uc);
2240 break;
2241 default:
2242 /* Can not happen */
2243 dev_err(uc->ud->dev, "%s: chan%d invalid direction (%u)\n",
2244 __func__, uc->id, uc->config.dir);
2245 return -EINVAL;
2246 }
2247
2248 /* check if the channel configuration was successful */
2249 if (ret)
2250 goto err_res_free;
2251
2252 if (udma_is_chan_running(uc)) {
2253 dev_warn(ud->dev, "chan%d: is running!\n", uc->id);
2254 udma_reset_chan(uc, false);
2255 if (udma_is_chan_running(uc)) {
2256 dev_err(ud->dev, "chan%d: won't stop!\n", uc->id);
2257 ret = -EBUSY;
2258 goto err_res_free;
2259 }
2260 }
2261
2262 uc->dma_dev = dmaengine_get_dma_device(chan);
2263 if (uc->config.dir == DMA_MEM_TO_MEM && !uc->config.tr_trigger_type) {
2264 uc->config.hdesc_size = cppi5_trdesc_calc_size(
2265 sizeof(struct cppi5_tr_type15_t), 2);
2266
2267 uc->hdesc_pool = dma_pool_create(uc->name, ud->ddev.dev,
2268 uc->config.hdesc_size,
2269 ud->desc_align,
2270 0);
2271 if (!uc->hdesc_pool) {
2272 dev_err(ud->ddev.dev,
2273 "Descriptor pool allocation failed\n");
2274 uc->use_dma_pool = false;
2275 return -ENOMEM;
2276 }
2277
2278 uc->use_dma_pool = true;
2279 } else if (uc->config.dir != DMA_MEM_TO_MEM) {
2280 /* PSI-L pairing */
2281 ret = navss_psil_pair(ud, uc->config.src_thread,
2282 uc->config.dst_thread);
2283 if (ret) {
2284 dev_err(ud->dev,
2285 "PSI-L pairing failed: 0x%04x -> 0x%04x\n",
2286 uc->config.src_thread, uc->config.dst_thread);
2287 goto err_res_free;
2288 }
2289
2290 uc->psil_paired = true;
2291 }
2292
2293 uc->irq_num_ring = ti_sci_inta_msi_get_virq(ud->dev, irq_ring_idx);
2294 if (uc->irq_num_ring <= 0) {
2295 dev_err(ud->dev, "Failed to get ring irq (index: %u)\n",
2296 irq_ring_idx);
2297 ret = -EINVAL;
2298 goto err_psi_free;
2299 }
2300
2301 ret = request_irq(uc->irq_num_ring, udma_ring_irq_handler,
2302 IRQF_TRIGGER_HIGH, uc->name, uc);
2303 if (ret) {
2304 dev_err(ud->dev, "chan%d: ring irq request failed\n", uc->id);
2305 goto err_irq_free;
2306 }
2307
2308 /* Event from BCDMA (TR events) only needed for slave channels */
2309 if (is_slave_direction(uc->config.dir)) {
2310 uc->irq_num_udma = ti_sci_inta_msi_get_virq(ud->dev,
2311 irq_udma_idx);
2312 if (uc->irq_num_udma <= 0) {
2313 dev_err(ud->dev, "Failed to get bcdma irq (index: %u)\n",
2314 irq_udma_idx);
2315 free_irq(uc->irq_num_ring, uc);
2316 ret = -EINVAL;
2317 goto err_irq_free;
2318 }
2319
2320 ret = request_irq(uc->irq_num_udma, udma_udma_irq_handler, 0,
2321 uc->name, uc);
2322 if (ret) {
2323 dev_err(ud->dev, "chan%d: BCDMA irq request failed\n",
2324 uc->id);
2325 free_irq(uc->irq_num_ring, uc);
2326 goto err_irq_free;
2327 }
2328 } else {
2329 uc->irq_num_udma = 0;
2330 }
2331
2332 udma_reset_rings(uc);
2333
2334 INIT_DELAYED_WORK_ONSTACK(&uc->tx_drain.work,
2335 udma_check_tx_completion);
2336 return 0;
2337
2338err_irq_free:
2339 uc->irq_num_ring = 0;
2340 uc->irq_num_udma = 0;
2341err_psi_free:
2342 if (uc->psil_paired)
2343 navss_psil_unpair(ud, uc->config.src_thread,
2344 uc->config.dst_thread);
2345 uc->psil_paired = false;
2346err_res_free:
2347 bcdma_free_bchan_resources(uc);
2348 udma_free_tx_resources(uc);
2349 udma_free_rx_resources(uc);
2350
2351 udma_reset_uchan(uc);
2352
2353 if (uc->use_dma_pool) {
2354 dma_pool_destroy(uc->hdesc_pool);
2355 uc->use_dma_pool = false;
2356 }
2357
2358 return ret;
2359}
2360
2361static int bcdma_router_config(struct dma_chan *chan)
2362{
2363 struct k3_event_route_data *router_data = chan->route_data;
2364 struct udma_chan *uc = to_udma_chan(chan);
2365 u32 trigger_event;
2366
2367 if (!uc->bchan)
2368 return -EINVAL;
2369
2370 if (uc->config.tr_trigger_type != 1 && uc->config.tr_trigger_type != 2)
2371 return -EINVAL;
2372
2373 trigger_event = uc->ud->soc_data->bcdma_trigger_event_offset;
2374 trigger_event += (uc->bchan->id * 2) + uc->config.tr_trigger_type - 1;
2375
2376 return router_data->set_event(router_data->priv, trigger_event);
2377}
2378
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002379static int udma_slave_config(struct dma_chan *chan,
2380 struct dma_slave_config *cfg)
2381{
2382 struct udma_chan *uc = to_udma_chan(chan);
2383
2384 memcpy(&uc->cfg, cfg, sizeof(uc->cfg));
2385
2386 return 0;
2387}
2388
2389static struct udma_desc *udma_alloc_tr_desc(struct udma_chan *uc,
2390 size_t tr_size, int tr_count,
2391 enum dma_transfer_direction dir)
2392{
2393 struct udma_hwdesc *hwdesc;
2394 struct cppi5_desc_hdr_t *tr_desc;
2395 struct udma_desc *d;
2396 u32 reload_count = 0;
2397 u32 ring_id;
2398
2399 switch (tr_size) {
2400 case 16:
2401 case 32:
2402 case 64:
2403 case 128:
2404 break;
2405 default:
2406 dev_err(uc->ud->dev, "Unsupported TR size of %zu\n", tr_size);
2407 return NULL;
2408 }
2409
2410 /* We have only one descriptor containing multiple TRs */
2411 d = kzalloc(sizeof(*d) + sizeof(d->hwdesc[0]), GFP_NOWAIT);
2412 if (!d)
2413 return NULL;
2414
2415 d->sglen = tr_count;
2416
2417 d->hwdesc_count = 1;
2418 hwdesc = &d->hwdesc[0];
2419
2420 /* Allocate memory for DMA ring descriptor */
2421 if (uc->use_dma_pool) {
2422 hwdesc->cppi5_desc_size = uc->config.hdesc_size;
2423 hwdesc->cppi5_desc_vaddr = dma_pool_zalloc(uc->hdesc_pool,
2424 GFP_NOWAIT,
2425 &hwdesc->cppi5_desc_paddr);
2426 } else {
2427 hwdesc->cppi5_desc_size = cppi5_trdesc_calc_size(tr_size,
2428 tr_count);
2429 hwdesc->cppi5_desc_size = ALIGN(hwdesc->cppi5_desc_size,
2430 uc->ud->desc_align);
2431 hwdesc->cppi5_desc_vaddr = dma_alloc_coherent(uc->ud->dev,
2432 hwdesc->cppi5_desc_size,
2433 &hwdesc->cppi5_desc_paddr,
2434 GFP_NOWAIT);
2435 }
2436
2437 if (!hwdesc->cppi5_desc_vaddr) {
2438 kfree(d);
2439 return NULL;
2440 }
2441
2442 /* Start of the TR req records */
2443 hwdesc->tr_req_base = hwdesc->cppi5_desc_vaddr + tr_size;
2444 /* Start address of the TR response array */
2445 hwdesc->tr_resp_base = hwdesc->tr_req_base + tr_size * tr_count;
2446
2447 tr_desc = hwdesc->cppi5_desc_vaddr;
2448
2449 if (uc->cyclic)
2450 reload_count = CPPI5_INFO0_TRDESC_RLDCNT_INFINITE;
2451
2452 if (dir == DMA_DEV_TO_MEM)
2453 ring_id = k3_ringacc_get_ring_id(uc->rflow->r_ring);
2454 else
2455 ring_id = k3_ringacc_get_ring_id(uc->tchan->tc_ring);
2456
2457 cppi5_trdesc_init(tr_desc, tr_count, tr_size, 0, reload_count);
2458 cppi5_desc_set_pktids(tr_desc, uc->id,
2459 CPPI5_INFO1_DESC_FLOWID_DEFAULT);
2460 cppi5_desc_set_retpolicy(tr_desc, 0, ring_id);
2461
2462 return d;
2463}
2464
Peter Ujfalusia9793402020-02-14 11:14:38 +02002465/**
2466 * udma_get_tr_counters - calculate TR counters for a given length
2467 * @len: Length of the trasnfer
2468 * @align_to: Preferred alignment
2469 * @tr0_cnt0: First TR icnt0
2470 * @tr0_cnt1: First TR icnt1
2471 * @tr1_cnt0: Second (if used) TR icnt0
2472 *
2473 * For len < SZ_64K only one TR is enough, tr1_cnt0 is not updated
2474 * For len >= SZ_64K two TRs are used in a simple way:
2475 * First TR: SZ_64K-alignment blocks (tr0_cnt0, tr0_cnt1)
2476 * Second TR: the remaining length (tr1_cnt0)
2477 *
2478 * Returns the number of TRs the length needs (1 or 2)
2479 * -EINVAL if the length can not be supported
2480 */
2481static int udma_get_tr_counters(size_t len, unsigned long align_to,
2482 u16 *tr0_cnt0, u16 *tr0_cnt1, u16 *tr1_cnt0)
2483{
2484 if (len < SZ_64K) {
2485 *tr0_cnt0 = len;
2486 *tr0_cnt1 = 1;
2487
2488 return 1;
2489 }
2490
2491 if (align_to > 3)
2492 align_to = 3;
2493
2494realign:
2495 *tr0_cnt0 = SZ_64K - BIT(align_to);
2496 if (len / *tr0_cnt0 >= SZ_64K) {
2497 if (align_to) {
2498 align_to--;
2499 goto realign;
2500 }
2501 return -EINVAL;
2502 }
2503
2504 *tr0_cnt1 = len / *tr0_cnt0;
2505 *tr1_cnt0 = len % *tr0_cnt0;
2506
2507 return 2;
2508}
2509
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002510static struct udma_desc *
2511udma_prep_slave_sg_tr(struct udma_chan *uc, struct scatterlist *sgl,
2512 unsigned int sglen, enum dma_transfer_direction dir,
2513 unsigned long tx_flags, void *context)
2514{
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002515 struct scatterlist *sgent;
2516 struct udma_desc *d;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002517 struct cppi5_tr_type1_t *tr_req = NULL;
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02002518 u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002519 unsigned int i;
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02002520 size_t tr_size;
2521 int num_tr = 0;
2522 int tr_idx = 0;
Peter Ujfalusi01779472020-12-08 11:04:37 +02002523 u64 asel;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002524
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02002525 /* estimate the number of TRs we will need */
2526 for_each_sg(sgl, sgent, sglen, i) {
2527 if (sg_dma_len(sgent) < SZ_64K)
2528 num_tr++;
2529 else
2530 num_tr += 2;
2531 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002532
2533 /* Now allocate and setup the descriptor. */
2534 tr_size = sizeof(struct cppi5_tr_type1_t);
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02002535 d = udma_alloc_tr_desc(uc, tr_size, num_tr, dir);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002536 if (!d)
2537 return NULL;
2538
2539 d->sglen = sglen;
2540
Peter Ujfalusi01779472020-12-08 11:04:37 +02002541 if (uc->ud->match_data->type == DMA_TYPE_UDMA)
2542 asel = 0;
2543 else
2544 asel = (u64)uc->config.asel << K3_ADDRESS_ASEL_SHIFT;
2545
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002546 tr_req = d->hwdesc[0].tr_req_base;
2547 for_each_sg(sgl, sgent, sglen, i) {
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02002548 dma_addr_t sg_addr = sg_dma_address(sgent);
2549
2550 num_tr = udma_get_tr_counters(sg_dma_len(sgent), __ffs(sg_addr),
2551 &tr0_cnt0, &tr0_cnt1, &tr1_cnt0);
2552 if (num_tr < 0) {
2553 dev_err(uc->ud->dev, "size %u is not supported\n",
2554 sg_dma_len(sgent));
2555 udma_free_hwdesc(uc, d);
2556 kfree(d);
2557 return NULL;
2558 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002559
Peter Ujfalusi33ebffa2020-08-24 15:01:08 +03002560 cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1, false,
2561 false, CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2562 cppi5_tr_csf_set(&tr_req[tr_idx].flags, CPPI5_TR_CSF_SUPR_EVT);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002563
Peter Ujfalusi01779472020-12-08 11:04:37 +02002564 sg_addr |= asel;
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02002565 tr_req[tr_idx].addr = sg_addr;
2566 tr_req[tr_idx].icnt0 = tr0_cnt0;
2567 tr_req[tr_idx].icnt1 = tr0_cnt1;
2568 tr_req[tr_idx].dim1 = tr0_cnt0;
2569 tr_idx++;
2570
2571 if (num_tr == 2) {
2572 cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1,
2573 false, false,
2574 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2575 cppi5_tr_csf_set(&tr_req[tr_idx].flags,
2576 CPPI5_TR_CSF_SUPR_EVT);
2577
2578 tr_req[tr_idx].addr = sg_addr + tr0_cnt1 * tr0_cnt0;
2579 tr_req[tr_idx].icnt0 = tr1_cnt0;
2580 tr_req[tr_idx].icnt1 = 1;
2581 tr_req[tr_idx].dim1 = tr1_cnt0;
2582 tr_idx++;
2583 }
2584
2585 d->residue += sg_dma_len(sgent);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002586 }
2587
Peter Ujfalusibe4054b2020-05-12 16:45:31 +03002588 cppi5_tr_csf_set(&tr_req[tr_idx - 1].flags,
2589 CPPI5_TR_CSF_SUPR_EVT | CPPI5_TR_CSF_EOP);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002590
2591 return d;
2592}
2593
Peter Ujfalusi01779472020-12-08 11:04:37 +02002594static struct udma_desc *
2595udma_prep_slave_sg_triggered_tr(struct udma_chan *uc, struct scatterlist *sgl,
2596 unsigned int sglen,
2597 enum dma_transfer_direction dir,
2598 unsigned long tx_flags, void *context)
2599{
2600 struct scatterlist *sgent;
2601 struct cppi5_tr_type15_t *tr_req = NULL;
2602 enum dma_slave_buswidth dev_width;
2603 u16 tr_cnt0, tr_cnt1;
2604 dma_addr_t dev_addr;
2605 struct udma_desc *d;
2606 unsigned int i;
2607 size_t tr_size, sg_len;
2608 int num_tr = 0;
2609 int tr_idx = 0;
2610 u32 burst, trigger_size, port_window;
2611 u64 asel;
2612
2613 if (dir == DMA_DEV_TO_MEM) {
2614 dev_addr = uc->cfg.src_addr;
2615 dev_width = uc->cfg.src_addr_width;
2616 burst = uc->cfg.src_maxburst;
2617 port_window = uc->cfg.src_port_window_size;
2618 } else if (dir == DMA_MEM_TO_DEV) {
2619 dev_addr = uc->cfg.dst_addr;
2620 dev_width = uc->cfg.dst_addr_width;
2621 burst = uc->cfg.dst_maxburst;
2622 port_window = uc->cfg.dst_port_window_size;
2623 } else {
2624 dev_err(uc->ud->dev, "%s: bad direction?\n", __func__);
2625 return NULL;
2626 }
2627
2628 if (!burst)
2629 burst = 1;
2630
2631 if (port_window) {
2632 if (port_window != burst) {
2633 dev_err(uc->ud->dev,
2634 "The burst must be equal to port_window\n");
2635 return NULL;
2636 }
2637
2638 tr_cnt0 = dev_width * port_window;
2639 tr_cnt1 = 1;
2640 } else {
2641 tr_cnt0 = dev_width;
2642 tr_cnt1 = burst;
2643 }
2644 trigger_size = tr_cnt0 * tr_cnt1;
2645
2646 /* estimate the number of TRs we will need */
2647 for_each_sg(sgl, sgent, sglen, i) {
2648 sg_len = sg_dma_len(sgent);
2649
2650 if (sg_len % trigger_size) {
2651 dev_err(uc->ud->dev,
2652 "Not aligned SG entry (%zu for %u)\n", sg_len,
2653 trigger_size);
2654 return NULL;
2655 }
2656
2657 if (sg_len / trigger_size < SZ_64K)
2658 num_tr++;
2659 else
2660 num_tr += 2;
2661 }
2662
2663 /* Now allocate and setup the descriptor. */
2664 tr_size = sizeof(struct cppi5_tr_type15_t);
2665 d = udma_alloc_tr_desc(uc, tr_size, num_tr, dir);
2666 if (!d)
2667 return NULL;
2668
2669 d->sglen = sglen;
2670
2671 if (uc->ud->match_data->type == DMA_TYPE_UDMA) {
2672 asel = 0;
2673 } else {
2674 asel = (u64)uc->config.asel << K3_ADDRESS_ASEL_SHIFT;
2675 dev_addr |= asel;
2676 }
2677
2678 tr_req = d->hwdesc[0].tr_req_base;
2679 for_each_sg(sgl, sgent, sglen, i) {
2680 u16 tr0_cnt2, tr0_cnt3, tr1_cnt2;
2681 dma_addr_t sg_addr = sg_dma_address(sgent);
2682
2683 sg_len = sg_dma_len(sgent);
2684 num_tr = udma_get_tr_counters(sg_len / trigger_size, 0,
2685 &tr0_cnt2, &tr0_cnt3, &tr1_cnt2);
2686 if (num_tr < 0) {
2687 dev_err(uc->ud->dev, "size %zu is not supported\n",
2688 sg_len);
2689 udma_free_hwdesc(uc, d);
2690 kfree(d);
2691 return NULL;
2692 }
2693
2694 cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE15, false,
2695 true, CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2696 cppi5_tr_csf_set(&tr_req[tr_idx].flags, CPPI5_TR_CSF_SUPR_EVT);
2697 cppi5_tr_set_trigger(&tr_req[tr_idx].flags,
2698 uc->config.tr_trigger_type,
2699 CPPI5_TR_TRIGGER_TYPE_ICNT2_DEC, 0, 0);
2700
2701 sg_addr |= asel;
2702 if (dir == DMA_DEV_TO_MEM) {
2703 tr_req[tr_idx].addr = dev_addr;
2704 tr_req[tr_idx].icnt0 = tr_cnt0;
2705 tr_req[tr_idx].icnt1 = tr_cnt1;
2706 tr_req[tr_idx].icnt2 = tr0_cnt2;
2707 tr_req[tr_idx].icnt3 = tr0_cnt3;
2708 tr_req[tr_idx].dim1 = (-1) * tr_cnt0;
2709
2710 tr_req[tr_idx].daddr = sg_addr;
2711 tr_req[tr_idx].dicnt0 = tr_cnt0;
2712 tr_req[tr_idx].dicnt1 = tr_cnt1;
2713 tr_req[tr_idx].dicnt2 = tr0_cnt2;
2714 tr_req[tr_idx].dicnt3 = tr0_cnt3;
2715 tr_req[tr_idx].ddim1 = tr_cnt0;
2716 tr_req[tr_idx].ddim2 = trigger_size;
2717 tr_req[tr_idx].ddim3 = trigger_size * tr0_cnt2;
2718 } else {
2719 tr_req[tr_idx].addr = sg_addr;
2720 tr_req[tr_idx].icnt0 = tr_cnt0;
2721 tr_req[tr_idx].icnt1 = tr_cnt1;
2722 tr_req[tr_idx].icnt2 = tr0_cnt2;
2723 tr_req[tr_idx].icnt3 = tr0_cnt3;
2724 tr_req[tr_idx].dim1 = tr_cnt0;
2725 tr_req[tr_idx].dim2 = trigger_size;
2726 tr_req[tr_idx].dim3 = trigger_size * tr0_cnt2;
2727
2728 tr_req[tr_idx].daddr = dev_addr;
2729 tr_req[tr_idx].dicnt0 = tr_cnt0;
2730 tr_req[tr_idx].dicnt1 = tr_cnt1;
2731 tr_req[tr_idx].dicnt2 = tr0_cnt2;
2732 tr_req[tr_idx].dicnt3 = tr0_cnt3;
2733 tr_req[tr_idx].ddim1 = (-1) * tr_cnt0;
2734 }
2735
2736 tr_idx++;
2737
2738 if (num_tr == 2) {
2739 cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE15,
2740 false, true,
2741 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
2742 cppi5_tr_csf_set(&tr_req[tr_idx].flags,
2743 CPPI5_TR_CSF_SUPR_EVT);
2744 cppi5_tr_set_trigger(&tr_req[tr_idx].flags,
2745 uc->config.tr_trigger_type,
2746 CPPI5_TR_TRIGGER_TYPE_ICNT2_DEC,
2747 0, 0);
2748
2749 sg_addr += trigger_size * tr0_cnt2 * tr0_cnt3;
2750 if (dir == DMA_DEV_TO_MEM) {
2751 tr_req[tr_idx].addr = dev_addr;
2752 tr_req[tr_idx].icnt0 = tr_cnt0;
2753 tr_req[tr_idx].icnt1 = tr_cnt1;
2754 tr_req[tr_idx].icnt2 = tr1_cnt2;
2755 tr_req[tr_idx].icnt3 = 1;
2756 tr_req[tr_idx].dim1 = (-1) * tr_cnt0;
2757
2758 tr_req[tr_idx].daddr = sg_addr;
2759 tr_req[tr_idx].dicnt0 = tr_cnt0;
2760 tr_req[tr_idx].dicnt1 = tr_cnt1;
2761 tr_req[tr_idx].dicnt2 = tr1_cnt2;
2762 tr_req[tr_idx].dicnt3 = 1;
2763 tr_req[tr_idx].ddim1 = tr_cnt0;
2764 tr_req[tr_idx].ddim2 = trigger_size;
2765 } else {
2766 tr_req[tr_idx].addr = sg_addr;
2767 tr_req[tr_idx].icnt0 = tr_cnt0;
2768 tr_req[tr_idx].icnt1 = tr_cnt1;
2769 tr_req[tr_idx].icnt2 = tr1_cnt2;
2770 tr_req[tr_idx].icnt3 = 1;
2771 tr_req[tr_idx].dim1 = tr_cnt0;
2772 tr_req[tr_idx].dim2 = trigger_size;
2773
2774 tr_req[tr_idx].daddr = dev_addr;
2775 tr_req[tr_idx].dicnt0 = tr_cnt0;
2776 tr_req[tr_idx].dicnt1 = tr_cnt1;
2777 tr_req[tr_idx].dicnt2 = tr1_cnt2;
2778 tr_req[tr_idx].dicnt3 = 1;
2779 tr_req[tr_idx].ddim1 = (-1) * tr_cnt0;
2780 }
2781 tr_idx++;
2782 }
2783
2784 d->residue += sg_len;
2785 }
2786
2787 cppi5_tr_csf_set(&tr_req[tr_idx - 1].flags,
2788 CPPI5_TR_CSF_SUPR_EVT | CPPI5_TR_CSF_EOP);
2789
2790 return d;
2791}
2792
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002793static int udma_configure_statictr(struct udma_chan *uc, struct udma_desc *d,
2794 enum dma_slave_buswidth dev_width,
2795 u16 elcnt)
2796{
2797 if (uc->config.ep_type != PSIL_EP_PDMA_XY)
2798 return 0;
2799
2800 /* Bus width translates to the element size (ES) */
2801 switch (dev_width) {
2802 case DMA_SLAVE_BUSWIDTH_1_BYTE:
2803 d->static_tr.elsize = 0;
2804 break;
2805 case DMA_SLAVE_BUSWIDTH_2_BYTES:
2806 d->static_tr.elsize = 1;
2807 break;
2808 case DMA_SLAVE_BUSWIDTH_3_BYTES:
2809 d->static_tr.elsize = 2;
2810 break;
2811 case DMA_SLAVE_BUSWIDTH_4_BYTES:
2812 d->static_tr.elsize = 3;
2813 break;
2814 case DMA_SLAVE_BUSWIDTH_8_BYTES:
2815 d->static_tr.elsize = 4;
2816 break;
2817 default: /* not reached */
2818 return -EINVAL;
2819 }
2820
2821 d->static_tr.elcnt = elcnt;
2822
2823 /*
2824 * PDMA must to close the packet when the channel is in packet mode.
2825 * For TR mode when the channel is not cyclic we also need PDMA to close
2826 * the packet otherwise the transfer will stall because PDMA holds on
2827 * the data it has received from the peripheral.
2828 */
2829 if (uc->config.pkt_mode || !uc->cyclic) {
2830 unsigned int div = dev_width * elcnt;
2831
2832 if (uc->cyclic)
2833 d->static_tr.bstcnt = d->residue / d->sglen / div;
2834 else
2835 d->static_tr.bstcnt = d->residue / div;
2836
2837 if (uc->config.dir == DMA_DEV_TO_MEM &&
2838 d->static_tr.bstcnt > uc->ud->match_data->statictr_z_mask)
2839 return -EINVAL;
2840 } else {
2841 d->static_tr.bstcnt = 0;
2842 }
2843
2844 return 0;
2845}
2846
2847static struct udma_desc *
2848udma_prep_slave_sg_pkt(struct udma_chan *uc, struct scatterlist *sgl,
2849 unsigned int sglen, enum dma_transfer_direction dir,
2850 unsigned long tx_flags, void *context)
2851{
2852 struct scatterlist *sgent;
2853 struct cppi5_host_desc_t *h_desc = NULL;
2854 struct udma_desc *d;
2855 u32 ring_id;
2856 unsigned int i;
2857
Gustavo A. R. Silvaace52a8c2020-06-19 17:43:34 -05002858 d = kzalloc(struct_size(d, hwdesc, sglen), GFP_NOWAIT);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02002859 if (!d)
2860 return NULL;
2861
2862 d->sglen = sglen;
2863 d->hwdesc_count = sglen;
2864
2865 if (dir == DMA_DEV_TO_MEM)
2866 ring_id = k3_ringacc_get_ring_id(uc->rflow->r_ring);
2867 else
2868 ring_id = k3_ringacc_get_ring_id(uc->tchan->tc_ring);
2869
2870 for_each_sg(sgl, sgent, sglen, i) {
2871 struct udma_hwdesc *hwdesc = &d->hwdesc[i];
2872 dma_addr_t sg_addr = sg_dma_address(sgent);
2873 struct cppi5_host_desc_t *desc;
2874 size_t sg_len = sg_dma_len(sgent);
2875
2876 hwdesc->cppi5_desc_vaddr = dma_pool_zalloc(uc->hdesc_pool,
2877 GFP_NOWAIT,
2878 &hwdesc->cppi5_desc_paddr);
2879 if (!hwdesc->cppi5_desc_vaddr) {
2880 dev_err(uc->ud->dev,
2881 "descriptor%d allocation failed\n", i);
2882
2883 udma_free_hwdesc(uc, d);
2884 kfree(d);
2885 return NULL;
2886 }
2887
2888 d->residue += sg_len;
2889 hwdesc->cppi5_desc_size = uc->config.hdesc_size;
2890 desc = hwdesc->cppi5_desc_vaddr;
2891
2892 if (i == 0) {
2893 cppi5_hdesc_init(desc, 0, 0);
2894 /* Flow and Packed ID */
2895 cppi5_desc_set_pktids(&desc->hdr, uc->id,
2896 CPPI5_INFO1_DESC_FLOWID_DEFAULT);
2897 cppi5_desc_set_retpolicy(&desc->hdr, 0, ring_id);
2898 } else {
2899 cppi5_hdesc_reset_hbdesc(desc);
2900 cppi5_desc_set_retpolicy(&desc->hdr, 0, 0xffff);
2901 }
2902
2903 /* attach the sg buffer to the descriptor */
2904 cppi5_hdesc_attach_buf(desc, sg_addr, sg_len, sg_addr, sg_len);
2905
2906 /* Attach link as host buffer descriptor */
2907 if (h_desc)
2908 cppi5_hdesc_link_hbdesc(h_desc,
2909 hwdesc->cppi5_desc_paddr);
2910
2911 if (dir == DMA_MEM_TO_DEV)
2912 h_desc = desc;
2913 }
2914
2915 if (d->residue >= SZ_4M) {
2916 dev_err(uc->ud->dev,
2917 "%s: Transfer size %u is over the supported 4M range\n",
2918 __func__, d->residue);
2919 udma_free_hwdesc(uc, d);
2920 kfree(d);
2921 return NULL;
2922 }
2923
2924 h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2925 cppi5_hdesc_set_pktlen(h_desc, d->residue);
2926
2927 return d;
2928}
2929
2930static int udma_attach_metadata(struct dma_async_tx_descriptor *desc,
2931 void *data, size_t len)
2932{
2933 struct udma_desc *d = to_udma_desc(desc);
2934 struct udma_chan *uc = to_udma_chan(desc->chan);
2935 struct cppi5_host_desc_t *h_desc;
2936 u32 psd_size = len;
2937 u32 flags = 0;
2938
2939 if (!uc->config.pkt_mode || !uc->config.metadata_size)
2940 return -ENOTSUPP;
2941
2942 if (!data || len > uc->config.metadata_size)
2943 return -EINVAL;
2944
2945 if (uc->config.needs_epib && len < CPPI5_INFO0_HDESC_EPIB_SIZE)
2946 return -EINVAL;
2947
2948 h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2949 if (d->dir == DMA_MEM_TO_DEV)
2950 memcpy(h_desc->epib, data, len);
2951
2952 if (uc->config.needs_epib)
2953 psd_size -= CPPI5_INFO0_HDESC_EPIB_SIZE;
2954
2955 d->metadata = data;
2956 d->metadata_size = len;
2957 if (uc->config.needs_epib)
2958 flags |= CPPI5_INFO0_HDESC_EPIB_PRESENT;
2959
2960 cppi5_hdesc_update_flags(h_desc, flags);
2961 cppi5_hdesc_update_psdata_size(h_desc, psd_size);
2962
2963 return 0;
2964}
2965
2966static void *udma_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
2967 size_t *payload_len, size_t *max_len)
2968{
2969 struct udma_desc *d = to_udma_desc(desc);
2970 struct udma_chan *uc = to_udma_chan(desc->chan);
2971 struct cppi5_host_desc_t *h_desc;
2972
2973 if (!uc->config.pkt_mode || !uc->config.metadata_size)
2974 return ERR_PTR(-ENOTSUPP);
2975
2976 h_desc = d->hwdesc[0].cppi5_desc_vaddr;
2977
2978 *max_len = uc->config.metadata_size;
2979
2980 *payload_len = cppi5_hdesc_epib_present(&h_desc->hdr) ?
2981 CPPI5_INFO0_HDESC_EPIB_SIZE : 0;
2982 *payload_len += cppi5_hdesc_get_psdata_size(h_desc);
2983
2984 return h_desc->epib;
2985}
2986
2987static int udma_set_metadata_len(struct dma_async_tx_descriptor *desc,
2988 size_t payload_len)
2989{
2990 struct udma_desc *d = to_udma_desc(desc);
2991 struct udma_chan *uc = to_udma_chan(desc->chan);
2992 struct cppi5_host_desc_t *h_desc;
2993 u32 psd_size = payload_len;
2994 u32 flags = 0;
2995
2996 if (!uc->config.pkt_mode || !uc->config.metadata_size)
2997 return -ENOTSUPP;
2998
2999 if (payload_len > uc->config.metadata_size)
3000 return -EINVAL;
3001
3002 if (uc->config.needs_epib && payload_len < CPPI5_INFO0_HDESC_EPIB_SIZE)
3003 return -EINVAL;
3004
3005 h_desc = d->hwdesc[0].cppi5_desc_vaddr;
3006
3007 if (uc->config.needs_epib) {
3008 psd_size -= CPPI5_INFO0_HDESC_EPIB_SIZE;
3009 flags |= CPPI5_INFO0_HDESC_EPIB_PRESENT;
3010 }
3011
3012 cppi5_hdesc_update_flags(h_desc, flags);
3013 cppi5_hdesc_update_psdata_size(h_desc, psd_size);
3014
3015 return 0;
3016}
3017
3018static struct dma_descriptor_metadata_ops metadata_ops = {
3019 .attach = udma_attach_metadata,
3020 .get_ptr = udma_get_metadata_ptr,
3021 .set_len = udma_set_metadata_len,
3022};
3023
3024static struct dma_async_tx_descriptor *
3025udma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
3026 unsigned int sglen, enum dma_transfer_direction dir,
3027 unsigned long tx_flags, void *context)
3028{
3029 struct udma_chan *uc = to_udma_chan(chan);
3030 enum dma_slave_buswidth dev_width;
3031 struct udma_desc *d;
3032 u32 burst;
3033
Peter Ujfalusi01779472020-12-08 11:04:37 +02003034 if (dir != uc->config.dir &&
3035 (uc->config.dir == DMA_MEM_TO_MEM && !uc->config.tr_trigger_type)) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003036 dev_err(chan->device->dev,
3037 "%s: chan%d is for %s, not supporting %s\n",
3038 __func__, uc->id,
3039 dmaengine_get_direction_text(uc->config.dir),
3040 dmaengine_get_direction_text(dir));
3041 return NULL;
3042 }
3043
3044 if (dir == DMA_DEV_TO_MEM) {
3045 dev_width = uc->cfg.src_addr_width;
3046 burst = uc->cfg.src_maxburst;
3047 } else if (dir == DMA_MEM_TO_DEV) {
3048 dev_width = uc->cfg.dst_addr_width;
3049 burst = uc->cfg.dst_maxburst;
3050 } else {
3051 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
3052 return NULL;
3053 }
3054
3055 if (!burst)
3056 burst = 1;
3057
3058 if (uc->config.pkt_mode)
3059 d = udma_prep_slave_sg_pkt(uc, sgl, sglen, dir, tx_flags,
3060 context);
Peter Ujfalusi01779472020-12-08 11:04:37 +02003061 else if (is_slave_direction(uc->config.dir))
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003062 d = udma_prep_slave_sg_tr(uc, sgl, sglen, dir, tx_flags,
3063 context);
Peter Ujfalusi01779472020-12-08 11:04:37 +02003064 else
3065 d = udma_prep_slave_sg_triggered_tr(uc, sgl, sglen, dir,
3066 tx_flags, context);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003067
3068 if (!d)
3069 return NULL;
3070
3071 d->dir = dir;
3072 d->desc_idx = 0;
3073 d->tr_idx = 0;
3074
3075 /* static TR for remote PDMA */
3076 if (udma_configure_statictr(uc, d, dev_width, burst)) {
3077 dev_err(uc->ud->dev,
Colin Ian King6c0157b2020-01-22 09:38:18 +00003078 "%s: StaticTR Z is limited to maximum 4095 (%u)\n",
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003079 __func__, d->static_tr.bstcnt);
3080
3081 udma_free_hwdesc(uc, d);
3082 kfree(d);
3083 return NULL;
3084 }
3085
3086 if (uc->config.metadata_size)
3087 d->vd.tx.metadata_ops = &metadata_ops;
3088
3089 return vchan_tx_prep(&uc->vc, &d->vd, tx_flags);
3090}
3091
3092static struct udma_desc *
3093udma_prep_dma_cyclic_tr(struct udma_chan *uc, dma_addr_t buf_addr,
3094 size_t buf_len, size_t period_len,
3095 enum dma_transfer_direction dir, unsigned long flags)
3096{
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003097 struct udma_desc *d;
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003098 size_t tr_size, period_addr;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003099 struct cppi5_tr_type1_t *tr_req;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003100 unsigned int periods = buf_len / period_len;
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003101 u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
3102 unsigned int i;
3103 int num_tr;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003104
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003105 num_tr = udma_get_tr_counters(period_len, __ffs(buf_addr), &tr0_cnt0,
3106 &tr0_cnt1, &tr1_cnt0);
3107 if (num_tr < 0) {
3108 dev_err(uc->ud->dev, "size %zu is not supported\n",
3109 period_len);
3110 return NULL;
3111 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003112
3113 /* Now allocate and setup the descriptor. */
3114 tr_size = sizeof(struct cppi5_tr_type1_t);
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003115 d = udma_alloc_tr_desc(uc, tr_size, periods * num_tr, dir);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003116 if (!d)
3117 return NULL;
3118
3119 tr_req = d->hwdesc[0].tr_req_base;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003120 if (uc->ud->match_data->type == DMA_TYPE_UDMA)
3121 period_addr = buf_addr;
3122 else
3123 period_addr = buf_addr |
3124 ((u64)uc->config.asel << K3_ADDRESS_ASEL_SHIFT);
3125
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003126 for (i = 0; i < periods; i++) {
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003127 int tr_idx = i * num_tr;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003128
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003129 cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1, false,
3130 false, CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
3131
3132 tr_req[tr_idx].addr = period_addr;
3133 tr_req[tr_idx].icnt0 = tr0_cnt0;
3134 tr_req[tr_idx].icnt1 = tr0_cnt1;
3135 tr_req[tr_idx].dim1 = tr0_cnt0;
3136
3137 if (num_tr == 2) {
3138 cppi5_tr_csf_set(&tr_req[tr_idx].flags,
3139 CPPI5_TR_CSF_SUPR_EVT);
3140 tr_idx++;
3141
3142 cppi5_tr_init(&tr_req[tr_idx].flags, CPPI5_TR_TYPE1,
3143 false, false,
3144 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
3145
3146 tr_req[tr_idx].addr = period_addr + tr0_cnt1 * tr0_cnt0;
3147 tr_req[tr_idx].icnt0 = tr1_cnt0;
3148 tr_req[tr_idx].icnt1 = 1;
3149 tr_req[tr_idx].dim1 = tr1_cnt0;
3150 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003151
3152 if (!(flags & DMA_PREP_INTERRUPT))
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003153 cppi5_tr_csf_set(&tr_req[tr_idx].flags,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003154 CPPI5_TR_CSF_SUPR_EVT);
Peter Ujfalusi6cf668a2020-02-14 11:14:39 +02003155
3156 period_addr += period_len;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003157 }
3158
3159 return d;
3160}
3161
3162static struct udma_desc *
3163udma_prep_dma_cyclic_pkt(struct udma_chan *uc, dma_addr_t buf_addr,
3164 size_t buf_len, size_t period_len,
3165 enum dma_transfer_direction dir, unsigned long flags)
3166{
3167 struct udma_desc *d;
3168 u32 ring_id;
3169 int i;
3170 int periods = buf_len / period_len;
3171
3172 if (periods > (K3_UDMA_DEFAULT_RING_SIZE - 1))
3173 return NULL;
3174
3175 if (period_len >= SZ_4M)
3176 return NULL;
3177
Gustavo A. R. Silvaace52a8c2020-06-19 17:43:34 -05003178 d = kzalloc(struct_size(d, hwdesc, periods), GFP_NOWAIT);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003179 if (!d)
3180 return NULL;
3181
3182 d->hwdesc_count = periods;
3183
3184 /* TODO: re-check this... */
3185 if (dir == DMA_DEV_TO_MEM)
3186 ring_id = k3_ringacc_get_ring_id(uc->rflow->r_ring);
3187 else
3188 ring_id = k3_ringacc_get_ring_id(uc->tchan->tc_ring);
3189
3190 for (i = 0; i < periods; i++) {
3191 struct udma_hwdesc *hwdesc = &d->hwdesc[i];
3192 dma_addr_t period_addr = buf_addr + (period_len * i);
3193 struct cppi5_host_desc_t *h_desc;
3194
3195 hwdesc->cppi5_desc_vaddr = dma_pool_zalloc(uc->hdesc_pool,
3196 GFP_NOWAIT,
3197 &hwdesc->cppi5_desc_paddr);
3198 if (!hwdesc->cppi5_desc_vaddr) {
3199 dev_err(uc->ud->dev,
3200 "descriptor%d allocation failed\n", i);
3201
3202 udma_free_hwdesc(uc, d);
3203 kfree(d);
3204 return NULL;
3205 }
3206
3207 hwdesc->cppi5_desc_size = uc->config.hdesc_size;
3208 h_desc = hwdesc->cppi5_desc_vaddr;
3209
3210 cppi5_hdesc_init(h_desc, 0, 0);
3211 cppi5_hdesc_set_pktlen(h_desc, period_len);
3212
3213 /* Flow and Packed ID */
3214 cppi5_desc_set_pktids(&h_desc->hdr, uc->id,
3215 CPPI5_INFO1_DESC_FLOWID_DEFAULT);
3216 cppi5_desc_set_retpolicy(&h_desc->hdr, 0, ring_id);
3217
3218 /* attach each period to a new descriptor */
3219 cppi5_hdesc_attach_buf(h_desc,
3220 period_addr, period_len,
3221 period_addr, period_len);
3222 }
3223
3224 return d;
3225}
3226
3227static struct dma_async_tx_descriptor *
3228udma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
3229 size_t period_len, enum dma_transfer_direction dir,
3230 unsigned long flags)
3231{
3232 struct udma_chan *uc = to_udma_chan(chan);
3233 enum dma_slave_buswidth dev_width;
3234 struct udma_desc *d;
3235 u32 burst;
3236
3237 if (dir != uc->config.dir) {
3238 dev_err(chan->device->dev,
3239 "%s: chan%d is for %s, not supporting %s\n",
3240 __func__, uc->id,
3241 dmaengine_get_direction_text(uc->config.dir),
3242 dmaengine_get_direction_text(dir));
3243 return NULL;
3244 }
3245
3246 uc->cyclic = true;
3247
3248 if (dir == DMA_DEV_TO_MEM) {
3249 dev_width = uc->cfg.src_addr_width;
3250 burst = uc->cfg.src_maxburst;
3251 } else if (dir == DMA_MEM_TO_DEV) {
3252 dev_width = uc->cfg.dst_addr_width;
3253 burst = uc->cfg.dst_maxburst;
3254 } else {
3255 dev_err(uc->ud->dev, "%s: bad direction?\n", __func__);
3256 return NULL;
3257 }
3258
3259 if (!burst)
3260 burst = 1;
3261
3262 if (uc->config.pkt_mode)
3263 d = udma_prep_dma_cyclic_pkt(uc, buf_addr, buf_len, period_len,
3264 dir, flags);
3265 else
3266 d = udma_prep_dma_cyclic_tr(uc, buf_addr, buf_len, period_len,
3267 dir, flags);
3268
3269 if (!d)
3270 return NULL;
3271
3272 d->sglen = buf_len / period_len;
3273
3274 d->dir = dir;
3275 d->residue = buf_len;
3276
3277 /* static TR for remote PDMA */
3278 if (udma_configure_statictr(uc, d, dev_width, burst)) {
3279 dev_err(uc->ud->dev,
Colin Ian King6c0157b2020-01-22 09:38:18 +00003280 "%s: StaticTR Z is limited to maximum 4095 (%u)\n",
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003281 __func__, d->static_tr.bstcnt);
3282
3283 udma_free_hwdesc(uc, d);
3284 kfree(d);
3285 return NULL;
3286 }
3287
3288 if (uc->config.metadata_size)
3289 d->vd.tx.metadata_ops = &metadata_ops;
3290
3291 return vchan_tx_prep(&uc->vc, &d->vd, flags);
3292}
3293
3294static struct dma_async_tx_descriptor *
3295udma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
3296 size_t len, unsigned long tx_flags)
3297{
3298 struct udma_chan *uc = to_udma_chan(chan);
3299 struct udma_desc *d;
3300 struct cppi5_tr_type15_t *tr_req;
3301 int num_tr;
3302 size_t tr_size = sizeof(struct cppi5_tr_type15_t);
3303 u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
3304
3305 if (uc->config.dir != DMA_MEM_TO_MEM) {
3306 dev_err(chan->device->dev,
3307 "%s: chan%d is for %s, not supporting %s\n",
3308 __func__, uc->id,
3309 dmaengine_get_direction_text(uc->config.dir),
3310 dmaengine_get_direction_text(DMA_MEM_TO_MEM));
3311 return NULL;
3312 }
3313
Peter Ujfalusia9793402020-02-14 11:14:38 +02003314 num_tr = udma_get_tr_counters(len, __ffs(src | dest), &tr0_cnt0,
3315 &tr0_cnt1, &tr1_cnt0);
3316 if (num_tr < 0) {
3317 dev_err(uc->ud->dev, "size %zu is not supported\n",
3318 len);
3319 return NULL;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003320 }
3321
3322 d = udma_alloc_tr_desc(uc, tr_size, num_tr, DMA_MEM_TO_MEM);
3323 if (!d)
3324 return NULL;
3325
3326 d->dir = DMA_MEM_TO_MEM;
3327 d->desc_idx = 0;
3328 d->tr_idx = 0;
3329 d->residue = len;
3330
Peter Ujfalusi01779472020-12-08 11:04:37 +02003331 if (uc->ud->match_data->type != DMA_TYPE_UDMA) {
3332 src |= (u64)uc->ud->asel << K3_ADDRESS_ASEL_SHIFT;
3333 dest |= (u64)uc->ud->asel << K3_ADDRESS_ASEL_SHIFT;
3334 }
3335
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003336 tr_req = d->hwdesc[0].tr_req_base;
3337
3338 cppi5_tr_init(&tr_req[0].flags, CPPI5_TR_TYPE15, false, true,
3339 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
3340 cppi5_tr_csf_set(&tr_req[0].flags, CPPI5_TR_CSF_SUPR_EVT);
3341
3342 tr_req[0].addr = src;
3343 tr_req[0].icnt0 = tr0_cnt0;
3344 tr_req[0].icnt1 = tr0_cnt1;
3345 tr_req[0].icnt2 = 1;
3346 tr_req[0].icnt3 = 1;
3347 tr_req[0].dim1 = tr0_cnt0;
3348
3349 tr_req[0].daddr = dest;
3350 tr_req[0].dicnt0 = tr0_cnt0;
3351 tr_req[0].dicnt1 = tr0_cnt1;
3352 tr_req[0].dicnt2 = 1;
3353 tr_req[0].dicnt3 = 1;
3354 tr_req[0].ddim1 = tr0_cnt0;
3355
3356 if (num_tr == 2) {
3357 cppi5_tr_init(&tr_req[1].flags, CPPI5_TR_TYPE15, false, true,
3358 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
3359 cppi5_tr_csf_set(&tr_req[1].flags, CPPI5_TR_CSF_SUPR_EVT);
3360
3361 tr_req[1].addr = src + tr0_cnt1 * tr0_cnt0;
3362 tr_req[1].icnt0 = tr1_cnt0;
3363 tr_req[1].icnt1 = 1;
3364 tr_req[1].icnt2 = 1;
3365 tr_req[1].icnt3 = 1;
3366
3367 tr_req[1].daddr = dest + tr0_cnt1 * tr0_cnt0;
3368 tr_req[1].dicnt0 = tr1_cnt0;
3369 tr_req[1].dicnt1 = 1;
3370 tr_req[1].dicnt2 = 1;
3371 tr_req[1].dicnt3 = 1;
3372 }
3373
Peter Ujfalusibe4054b2020-05-12 16:45:31 +03003374 cppi5_tr_csf_set(&tr_req[num_tr - 1].flags,
3375 CPPI5_TR_CSF_SUPR_EVT | CPPI5_TR_CSF_EOP);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003376
3377 if (uc->config.metadata_size)
3378 d->vd.tx.metadata_ops = &metadata_ops;
3379
3380 return vchan_tx_prep(&uc->vc, &d->vd, tx_flags);
3381}
3382
3383static void udma_issue_pending(struct dma_chan *chan)
3384{
3385 struct udma_chan *uc = to_udma_chan(chan);
3386 unsigned long flags;
3387
3388 spin_lock_irqsave(&uc->vc.lock, flags);
3389
3390 /* If we have something pending and no active descriptor, then */
3391 if (vchan_issue_pending(&uc->vc) && !uc->desc) {
3392 /*
3393 * start a descriptor if the channel is NOT [marked as
3394 * terminating _and_ it is still running (teardown has not
3395 * completed yet)].
3396 */
3397 if (!(uc->state == UDMA_CHAN_IS_TERMINATING &&
3398 udma_is_chan_running(uc)))
3399 udma_start(uc);
3400 }
3401
3402 spin_unlock_irqrestore(&uc->vc.lock, flags);
3403}
3404
3405static enum dma_status udma_tx_status(struct dma_chan *chan,
3406 dma_cookie_t cookie,
3407 struct dma_tx_state *txstate)
3408{
3409 struct udma_chan *uc = to_udma_chan(chan);
3410 enum dma_status ret;
3411 unsigned long flags;
3412
3413 spin_lock_irqsave(&uc->vc.lock, flags);
3414
3415 ret = dma_cookie_status(chan, cookie, txstate);
3416
Peter Ujfalusi83903182020-02-14 11:14:41 +02003417 if (!udma_is_chan_running(uc))
3418 ret = DMA_COMPLETE;
3419
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003420 if (ret == DMA_IN_PROGRESS && udma_is_chan_paused(uc))
3421 ret = DMA_PAUSED;
3422
3423 if (ret == DMA_COMPLETE || !txstate)
3424 goto out;
3425
3426 if (uc->desc && uc->desc->vd.tx.cookie == cookie) {
3427 u32 peer_bcnt = 0;
3428 u32 bcnt = 0;
3429 u32 residue = uc->desc->residue;
3430 u32 delay = 0;
3431
3432 if (uc->desc->dir == DMA_MEM_TO_DEV) {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003433 bcnt = udma_tchanrt_read(uc, UDMA_CHAN_RT_SBCNT_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003434
3435 if (uc->config.ep_type != PSIL_EP_NATIVE) {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003436 peer_bcnt = udma_tchanrt_read(uc,
Peter Ujfalusibc7e5522020-07-07 13:23:50 +03003437 UDMA_CHAN_RT_PEER_BCNT_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003438
3439 if (bcnt > peer_bcnt)
3440 delay = bcnt - peer_bcnt;
3441 }
3442 } else if (uc->desc->dir == DMA_DEV_TO_MEM) {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003443 bcnt = udma_rchanrt_read(uc, UDMA_CHAN_RT_BCNT_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003444
3445 if (uc->config.ep_type != PSIL_EP_NATIVE) {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003446 peer_bcnt = udma_rchanrt_read(uc,
Peter Ujfalusibc7e5522020-07-07 13:23:50 +03003447 UDMA_CHAN_RT_PEER_BCNT_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003448
3449 if (peer_bcnt > bcnt)
3450 delay = peer_bcnt - bcnt;
3451 }
3452 } else {
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003453 bcnt = udma_tchanrt_read(uc, UDMA_CHAN_RT_BCNT_REG);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003454 }
3455
3456 bcnt -= uc->bcnt;
3457 if (bcnt && !(bcnt % uc->desc->residue))
3458 residue = 0;
3459 else
3460 residue -= bcnt % uc->desc->residue;
3461
3462 if (!residue && (uc->config.dir == DMA_DEV_TO_MEM || !delay)) {
3463 ret = DMA_COMPLETE;
3464 delay = 0;
3465 }
3466
3467 dma_set_residue(txstate, residue);
3468 dma_set_in_flight_bytes(txstate, delay);
3469
3470 } else {
3471 ret = DMA_COMPLETE;
3472 }
3473
3474out:
3475 spin_unlock_irqrestore(&uc->vc.lock, flags);
3476 return ret;
3477}
3478
3479static int udma_pause(struct dma_chan *chan)
3480{
3481 struct udma_chan *uc = to_udma_chan(chan);
3482
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003483 /* pause the channel */
Peter Ujfalusic7450bb2020-02-14 11:14:40 +02003484 switch (uc->config.dir) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003485 case DMA_DEV_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003486 udma_rchanrt_update_bits(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003487 UDMA_PEER_RT_EN_PAUSE,
3488 UDMA_PEER_RT_EN_PAUSE);
3489 break;
3490 case DMA_MEM_TO_DEV:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003491 udma_tchanrt_update_bits(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003492 UDMA_PEER_RT_EN_PAUSE,
3493 UDMA_PEER_RT_EN_PAUSE);
3494 break;
3495 case DMA_MEM_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003496 udma_tchanrt_update_bits(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003497 UDMA_CHAN_RT_CTL_PAUSE,
3498 UDMA_CHAN_RT_CTL_PAUSE);
3499 break;
3500 default:
3501 return -EINVAL;
3502 }
3503
3504 return 0;
3505}
3506
3507static int udma_resume(struct dma_chan *chan)
3508{
3509 struct udma_chan *uc = to_udma_chan(chan);
3510
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003511 /* resume the channel */
Peter Ujfalusic7450bb2020-02-14 11:14:40 +02003512 switch (uc->config.dir) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003513 case DMA_DEV_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003514 udma_rchanrt_update_bits(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003515 UDMA_PEER_RT_EN_PAUSE, 0);
3516
3517 break;
3518 case DMA_MEM_TO_DEV:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003519 udma_tchanrt_update_bits(uc, UDMA_CHAN_RT_PEER_RT_EN_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003520 UDMA_PEER_RT_EN_PAUSE, 0);
3521 break;
3522 case DMA_MEM_TO_MEM:
Peter Ujfalusidb375dc2020-07-07 13:23:52 +03003523 udma_tchanrt_update_bits(uc, UDMA_CHAN_RT_CTL_REG,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003524 UDMA_CHAN_RT_CTL_PAUSE, 0);
3525 break;
3526 default:
3527 return -EINVAL;
3528 }
3529
3530 return 0;
3531}
3532
3533static int udma_terminate_all(struct dma_chan *chan)
3534{
3535 struct udma_chan *uc = to_udma_chan(chan);
3536 unsigned long flags;
3537 LIST_HEAD(head);
3538
3539 spin_lock_irqsave(&uc->vc.lock, flags);
3540
3541 if (udma_is_chan_running(uc))
3542 udma_stop(uc);
3543
3544 if (uc->desc) {
3545 uc->terminated_desc = uc->desc;
3546 uc->desc = NULL;
3547 uc->terminated_desc->terminated = true;
3548 cancel_delayed_work(&uc->tx_drain.work);
3549 }
3550
3551 uc->paused = false;
3552
3553 vchan_get_all_descriptors(&uc->vc, &head);
3554 spin_unlock_irqrestore(&uc->vc.lock, flags);
3555 vchan_dma_desc_free_list(&uc->vc, &head);
3556
3557 return 0;
3558}
3559
3560static void udma_synchronize(struct dma_chan *chan)
3561{
3562 struct udma_chan *uc = to_udma_chan(chan);
3563 unsigned long timeout = msecs_to_jiffies(1000);
3564
3565 vchan_synchronize(&uc->vc);
3566
3567 if (uc->state == UDMA_CHAN_IS_TERMINATING) {
3568 timeout = wait_for_completion_timeout(&uc->teardown_completed,
3569 timeout);
3570 if (!timeout) {
3571 dev_warn(uc->ud->dev, "chan%d teardown timeout!\n",
3572 uc->id);
3573 udma_dump_chan_stdata(uc);
3574 udma_reset_chan(uc, true);
3575 }
3576 }
3577
3578 udma_reset_chan(uc, false);
3579 if (udma_is_chan_running(uc))
3580 dev_warn(uc->ud->dev, "chan%d refused to stop!\n", uc->id);
3581
3582 cancel_delayed_work_sync(&uc->tx_drain.work);
3583 udma_reset_rings(uc);
3584}
3585
3586static void udma_desc_pre_callback(struct virt_dma_chan *vc,
3587 struct virt_dma_desc *vd,
3588 struct dmaengine_result *result)
3589{
3590 struct udma_chan *uc = to_udma_chan(&vc->chan);
3591 struct udma_desc *d;
3592
3593 if (!vd)
3594 return;
3595
3596 d = to_udma_desc(&vd->tx);
3597
3598 if (d->metadata_size)
3599 udma_fetch_epib(uc, d);
3600
3601 /* Provide residue information for the client */
3602 if (result) {
3603 void *desc_vaddr = udma_curr_cppi5_desc_vaddr(d, d->desc_idx);
3604
3605 if (cppi5_desc_get_type(desc_vaddr) ==
3606 CPPI5_INFO0_DESC_TYPE_VAL_HOST) {
3607 result->residue = d->residue -
3608 cppi5_hdesc_get_pktlen(desc_vaddr);
3609 if (result->residue)
3610 result->result = DMA_TRANS_ABORTED;
3611 else
3612 result->result = DMA_TRANS_NOERROR;
3613 } else {
3614 result->residue = 0;
3615 result->result = DMA_TRANS_NOERROR;
3616 }
3617 }
3618}
3619
3620/*
3621 * This tasklet handles the completion of a DMA descriptor by
3622 * calling its callback and freeing it.
3623 */
Allen Pais2fa9bc92020-08-31 16:05:42 +05303624static void udma_vchan_complete(struct tasklet_struct *t)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003625{
Allen Pais2fa9bc92020-08-31 16:05:42 +05303626 struct virt_dma_chan *vc = from_tasklet(vc, t, task);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003627 struct virt_dma_desc *vd, *_vd;
3628 struct dmaengine_desc_callback cb;
3629 LIST_HEAD(head);
3630
3631 spin_lock_irq(&vc->lock);
3632 list_splice_tail_init(&vc->desc_completed, &head);
3633 vd = vc->cyclic;
3634 if (vd) {
3635 vc->cyclic = NULL;
3636 dmaengine_desc_get_callback(&vd->tx, &cb);
3637 } else {
3638 memset(&cb, 0, sizeof(cb));
3639 }
3640 spin_unlock_irq(&vc->lock);
3641
3642 udma_desc_pre_callback(vc, vd, NULL);
3643 dmaengine_desc_callback_invoke(&cb, NULL);
3644
3645 list_for_each_entry_safe(vd, _vd, &head, node) {
3646 struct dmaengine_result result;
3647
3648 dmaengine_desc_get_callback(&vd->tx, &cb);
3649
3650 list_del(&vd->node);
3651
3652 udma_desc_pre_callback(vc, vd, &result);
3653 dmaengine_desc_callback_invoke(&cb, &result);
3654
3655 vchan_vdesc_fini(vd);
3656 }
3657}
3658
3659static void udma_free_chan_resources(struct dma_chan *chan)
3660{
3661 struct udma_chan *uc = to_udma_chan(chan);
3662 struct udma_dev *ud = to_udma_dev(chan->device);
3663
3664 udma_terminate_all(chan);
3665 if (uc->terminated_desc) {
3666 udma_reset_chan(uc, false);
3667 udma_reset_rings(uc);
3668 }
3669
3670 cancel_delayed_work_sync(&uc->tx_drain.work);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003671
3672 if (uc->irq_num_ring > 0) {
3673 free_irq(uc->irq_num_ring, uc);
3674
3675 uc->irq_num_ring = 0;
3676 }
3677 if (uc->irq_num_udma > 0) {
3678 free_irq(uc->irq_num_udma, uc);
3679
3680 uc->irq_num_udma = 0;
3681 }
3682
3683 /* Release PSI-L pairing */
3684 if (uc->psil_paired) {
3685 navss_psil_unpair(ud, uc->config.src_thread,
3686 uc->config.dst_thread);
3687 uc->psil_paired = false;
3688 }
3689
3690 vchan_free_chan_resources(&uc->vc);
3691 tasklet_kill(&uc->vc.task);
3692
Peter Ujfalusi01779472020-12-08 11:04:37 +02003693 bcdma_free_bchan_resources(uc);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003694 udma_free_tx_resources(uc);
3695 udma_free_rx_resources(uc);
3696 udma_reset_uchan(uc);
3697
3698 if (uc->use_dma_pool) {
3699 dma_pool_destroy(uc->hdesc_pool);
3700 uc->use_dma_pool = false;
3701 }
3702}
3703
3704static struct platform_driver udma_driver;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003705static struct platform_driver bcdma_driver;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003706
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003707struct udma_filter_param {
3708 int remote_thread_id;
3709 u32 atype;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003710 u32 asel;
3711 u32 tr_trigger_type;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003712};
3713
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003714static bool udma_dma_filter_fn(struct dma_chan *chan, void *param)
3715{
3716 struct udma_chan_config *ucc;
3717 struct psil_endpoint_config *ep_config;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003718 struct udma_filter_param *filter_param;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003719 struct udma_chan *uc;
3720 struct udma_dev *ud;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003721
Peter Ujfalusi01779472020-12-08 11:04:37 +02003722 if (chan->device->dev->driver != &udma_driver.driver &&
3723 chan->device->dev->driver != &bcdma_driver.driver)
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003724 return false;
3725
3726 uc = to_udma_chan(chan);
3727 ucc = &uc->config;
3728 ud = uc->ud;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003729 filter_param = param;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003730
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003731 if (filter_param->atype > 2) {
3732 dev_err(ud->dev, "Invalid channel atype: %u\n",
3733 filter_param->atype);
3734 return false;
3735 }
3736
Peter Ujfalusi01779472020-12-08 11:04:37 +02003737 if (filter_param->asel > 15) {
3738 dev_err(ud->dev, "Invalid channel asel: %u\n",
3739 filter_param->asel);
3740 return false;
3741 }
3742
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003743 ucc->remote_thread_id = filter_param->remote_thread_id;
3744 ucc->atype = filter_param->atype;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003745 ucc->asel = filter_param->asel;
3746 ucc->tr_trigger_type = filter_param->tr_trigger_type;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003747
Peter Ujfalusi01779472020-12-08 11:04:37 +02003748 if (ucc->tr_trigger_type) {
3749 ucc->dir = DMA_MEM_TO_MEM;
3750 goto triggered_bchan;
3751 } else if (ucc->remote_thread_id & K3_PSIL_DST_THREAD_ID_OFFSET) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003752 ucc->dir = DMA_MEM_TO_DEV;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003753 } else {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003754 ucc->dir = DMA_DEV_TO_MEM;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003755 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003756
3757 ep_config = psil_get_ep_config(ucc->remote_thread_id);
3758 if (IS_ERR(ep_config)) {
3759 dev_err(ud->dev, "No configuration for psi-l thread 0x%04x\n",
3760 ucc->remote_thread_id);
3761 ucc->dir = DMA_MEM_TO_MEM;
3762 ucc->remote_thread_id = -1;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003763 ucc->atype = 0;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003764 ucc->asel = 0;
3765 return false;
3766 }
3767
3768 if (ud->match_data->type == DMA_TYPE_BCDMA &&
3769 ep_config->pkt_mode) {
3770 dev_err(ud->dev,
3771 "Only TR mode is supported (psi-l thread 0x%04x)\n",
3772 ucc->remote_thread_id);
3773 ucc->dir = DMA_MEM_TO_MEM;
3774 ucc->remote_thread_id = -1;
3775 ucc->atype = 0;
3776 ucc->asel = 0;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003777 return false;
3778 }
3779
3780 ucc->pkt_mode = ep_config->pkt_mode;
3781 ucc->channel_tpl = ep_config->channel_tpl;
3782 ucc->notdpkt = ep_config->notdpkt;
3783 ucc->ep_type = ep_config->ep_type;
3784
3785 if (ucc->ep_type != PSIL_EP_NATIVE) {
3786 const struct udma_match_data *match_data = ud->match_data;
3787
3788 if (match_data->flags & UDMA_FLAG_PDMA_ACC32)
3789 ucc->enable_acc32 = ep_config->pdma_acc32;
3790 if (match_data->flags & UDMA_FLAG_PDMA_BURST)
3791 ucc->enable_burst = ep_config->pdma_burst;
3792 }
3793
3794 ucc->needs_epib = ep_config->needs_epib;
3795 ucc->psd_size = ep_config->psd_size;
3796 ucc->metadata_size =
3797 (ucc->needs_epib ? CPPI5_INFO0_HDESC_EPIB_SIZE : 0) +
3798 ucc->psd_size;
3799
3800 if (ucc->pkt_mode)
3801 ucc->hdesc_size = ALIGN(sizeof(struct cppi5_host_desc_t) +
3802 ucc->metadata_size, ud->desc_align);
3803
3804 dev_dbg(ud->dev, "chan%d: Remote thread: 0x%04x (%s)\n", uc->id,
3805 ucc->remote_thread_id, dmaengine_get_direction_text(ucc->dir));
3806
3807 return true;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003808
3809triggered_bchan:
3810 dev_dbg(ud->dev, "chan%d: triggered channel (type: %u)\n", uc->id,
3811 ucc->tr_trigger_type);
3812
3813 return true;
3814
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003815}
3816
3817static struct dma_chan *udma_of_xlate(struct of_phandle_args *dma_spec,
3818 struct of_dma *ofdma)
3819{
3820 struct udma_dev *ud = ofdma->of_dma_data;
3821 dma_cap_mask_t mask = ud->ddev.cap_mask;
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003822 struct udma_filter_param filter_param;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003823 struct dma_chan *chan;
3824
Peter Ujfalusi01779472020-12-08 11:04:37 +02003825 if (ud->match_data->type == DMA_TYPE_BCDMA) {
3826 if (dma_spec->args_count != 3)
3827 return NULL;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003828
Peter Ujfalusi01779472020-12-08 11:04:37 +02003829 filter_param.tr_trigger_type = dma_spec->args[0];
3830 filter_param.remote_thread_id = dma_spec->args[1];
3831 filter_param.asel = dma_spec->args[2];
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003832 filter_param.atype = 0;
Peter Ujfalusi01779472020-12-08 11:04:37 +02003833 } else {
3834 if (dma_spec->args_count != 1 && dma_spec->args_count != 2)
3835 return NULL;
3836
3837 filter_param.remote_thread_id = dma_spec->args[0];
3838 filter_param.tr_trigger_type = 0;
3839 if (dma_spec->args_count == 2) {
3840 if (ud->match_data->type == DMA_TYPE_UDMA) {
3841 filter_param.atype = dma_spec->args[1];
3842 filter_param.asel = 0;
3843 } else {
3844 filter_param.atype = 0;
3845 filter_param.asel = dma_spec->args[1];
3846 }
3847 } else {
3848 filter_param.atype = 0;
3849 filter_param.asel = 0;
3850 }
3851 }
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02003852
3853 chan = __dma_request_channel(&mask, udma_dma_filter_fn, &filter_param,
3854 ofdma->of_node);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003855 if (!chan) {
3856 dev_err(ud->dev, "get channel fail in %s.\n", __func__);
3857 return ERR_PTR(-EINVAL);
3858 }
3859
3860 return chan;
3861}
3862
3863static struct udma_match_data am654_main_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003864 .type = DMA_TYPE_UDMA,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003865 .psil_base = 0x1000,
3866 .enable_memcpy_support = true,
3867 .statictr_z_mask = GENMASK(11, 0),
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003868};
3869
3870static struct udma_match_data am654_mcu_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003871 .type = DMA_TYPE_UDMA,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003872 .psil_base = 0x6000,
Peter Ujfalusia4e68852020-03-27 16:42:28 +02003873 .enable_memcpy_support = false,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003874 .statictr_z_mask = GENMASK(11, 0),
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003875};
3876
3877static struct udma_match_data j721e_main_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003878 .type = DMA_TYPE_UDMA,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003879 .psil_base = 0x1000,
3880 .enable_memcpy_support = true,
Peter Ujfalusi5e1cb1c2020-12-08 11:04:22 +02003881 .flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST | UDMA_FLAG_TDTYPE,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003882 .statictr_z_mask = GENMASK(23, 0),
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003883};
3884
3885static struct udma_match_data j721e_mcu_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003886 .type = DMA_TYPE_UDMA,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003887 .psil_base = 0x6000,
3888 .enable_memcpy_support = false, /* MEM_TO_MEM is slow via MCU UDMA */
Peter Ujfalusi5e1cb1c2020-12-08 11:04:22 +02003889 .flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST | UDMA_FLAG_TDTYPE,
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003890 .statictr_z_mask = GENMASK(23, 0),
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003891};
3892
Peter Ujfalusi01779472020-12-08 11:04:37 +02003893static struct udma_match_data am64_bcdma_data = {
3894 .type = DMA_TYPE_BCDMA,
3895 .psil_base = 0x2000, /* for tchan and rchan, not applicable to bchan */
3896 .enable_memcpy_support = true, /* Supported via bchan */
3897 .flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST | UDMA_FLAG_TDTYPE,
3898 .statictr_z_mask = GENMASK(23, 0),
3899};
3900
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003901static const struct of_device_id udma_of_match[] = {
3902 {
3903 .compatible = "ti,am654-navss-main-udmap",
3904 .data = &am654_main_data,
3905 },
3906 {
3907 .compatible = "ti,am654-navss-mcu-udmap",
3908 .data = &am654_mcu_data,
3909 }, {
3910 .compatible = "ti,j721e-navss-main-udmap",
3911 .data = &j721e_main_data,
3912 }, {
3913 .compatible = "ti,j721e-navss-mcu-udmap",
3914 .data = &j721e_mcu_data,
3915 },
3916 { /* Sentinel */ },
3917};
3918
Peter Ujfalusi01779472020-12-08 11:04:37 +02003919static const struct of_device_id bcdma_of_match[] = {
3920 {
3921 .compatible = "ti,am64-dmss-bcdma",
3922 .data = &am64_bcdma_data,
3923 },
3924 { /* Sentinel */ },
3925};
3926
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03003927static struct udma_soc_data am654_soc_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003928 .oes = {
3929 .udma_rchan = 0x200,
3930 },
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03003931};
3932
3933static struct udma_soc_data j721e_soc_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003934 .oes = {
3935 .udma_rchan = 0x400,
3936 },
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03003937};
3938
3939static struct udma_soc_data j7200_soc_data = {
Peter Ujfalusi01779472020-12-08 11:04:37 +02003940 .oes = {
3941 .udma_rchan = 0x80,
3942 },
3943};
3944
3945static struct udma_soc_data am64_soc_data = {
3946 .oes = {
3947 .bcdma_bchan_data = 0x2200,
3948 .bcdma_bchan_ring = 0x2400,
3949 .bcdma_tchan_data = 0x2800,
3950 .bcdma_tchan_ring = 0x2a00,
3951 .bcdma_rchan_data = 0x2e00,
3952 .bcdma_rchan_ring = 0x3000,
3953 },
3954 .bcdma_trigger_event_offset = 0xc400,
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03003955};
3956
3957static const struct soc_device_attribute k3_soc_devices[] = {
3958 { .family = "AM65X", .data = &am654_soc_data },
3959 { .family = "J721E", .data = &j721e_soc_data },
3960 { .family = "J7200", .data = &j7200_soc_data },
Peter Ujfalusi01779472020-12-08 11:04:37 +02003961 { .family = "AM64X", .data = &am64_soc_data },
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03003962 { /* sentinel */ }
3963};
3964
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003965static int udma_get_mmrs(struct platform_device *pdev, struct udma_dev *ud)
3966{
Peter Ujfalusi01779472020-12-08 11:04:37 +02003967 u32 cap2, cap3;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02003968 int i;
3969
Peter Ujfalusi01779472020-12-08 11:04:37 +02003970 ud->mmrs[MMR_GCFG] = devm_platform_ioremap_resource_byname(pdev, mmr_names[MMR_GCFG]);
3971 if (IS_ERR(ud->mmrs[MMR_GCFG]))
3972 return PTR_ERR(ud->mmrs[MMR_GCFG]);
3973
3974 cap2 = udma_read(ud->mmrs[MMR_GCFG], 0x28);
3975 cap3 = udma_read(ud->mmrs[MMR_GCFG], 0x2c);
3976
3977 switch (ud->match_data->type) {
3978 case DMA_TYPE_UDMA:
3979 ud->rflow_cnt = UDMA_CAP3_RFLOW_CNT(cap3);
3980 ud->tchan_cnt = UDMA_CAP2_TCHAN_CNT(cap2);
3981 ud->echan_cnt = UDMA_CAP2_ECHAN_CNT(cap2);
3982 ud->rchan_cnt = UDMA_CAP2_RCHAN_CNT(cap2);
3983 break;
3984 case DMA_TYPE_BCDMA:
3985 ud->bchan_cnt = BCDMA_CAP2_BCHAN_CNT(cap2);
3986 ud->tchan_cnt = BCDMA_CAP2_TCHAN_CNT(cap2);
3987 ud->rchan_cnt = BCDMA_CAP2_RCHAN_CNT(cap2);
3988 break;
3989 default:
3990 return -EINVAL;
3991 }
3992
3993 for (i = 1; i < MMR_LAST; i++) {
3994 if (i == MMR_BCHANRT && ud->bchan_cnt == 0)
3995 continue;
3996 if (i == MMR_TCHANRT && ud->tchan_cnt == 0)
3997 continue;
3998 if (i == MMR_RCHANRT && ud->rchan_cnt == 0)
3999 continue;
4000
Zhang Qilongea275002020-09-21 17:37:01 +08004001 ud->mmrs[i] = devm_platform_ioremap_resource_byname(pdev, mmr_names[i]);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004002 if (IS_ERR(ud->mmrs[i]))
4003 return PTR_ERR(ud->mmrs[i]);
4004 }
4005
4006 return 0;
4007}
4008
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004009static void udma_mark_resource_ranges(struct udma_dev *ud, unsigned long *map,
4010 struct ti_sci_resource_desc *rm_desc,
4011 char *name)
4012{
4013 bitmap_clear(map, rm_desc->start, rm_desc->num);
4014 bitmap_clear(map, rm_desc->start_sec, rm_desc->num_sec);
4015 dev_dbg(ud->dev, "ti_sci resource range for %s: %d:%d | %d:%d\n", name,
4016 rm_desc->start, rm_desc->num, rm_desc->start_sec,
4017 rm_desc->num_sec);
4018}
4019
Peter Ujfalusi01779472020-12-08 11:04:37 +02004020static const char * const range_names[] = {
4021 [RM_RANGE_BCHAN] = "ti,sci-rm-range-bchan",
4022 [RM_RANGE_TCHAN] = "ti,sci-rm-range-tchan",
4023 [RM_RANGE_RCHAN] = "ti,sci-rm-range-rchan",
4024 [RM_RANGE_RFLOW] = "ti,sci-rm-range-rflow"
4025};
4026
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004027static int udma_setup_resources(struct udma_dev *ud)
4028{
Peter Ujfalusi01779472020-12-08 11:04:37 +02004029 int ret, i, j;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004030 struct device *dev = ud->dev;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004031 struct ti_sci_resource *rm_res, irq_res;
4032 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
Peter Ujfalusi01779472020-12-08 11:04:37 +02004033 u32 cap3;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004034
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +03004035 /* Set up the throughput level start indexes */
Peter Ujfalusi01779472020-12-08 11:04:37 +02004036 cap3 = udma_read(ud->mmrs[MMR_GCFG], 0x2c);
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +03004037 if (of_device_is_compatible(dev->of_node,
4038 "ti,am654-navss-main-udmap")) {
4039 ud->tpl_levels = 2;
4040 ud->tpl_start_idx[0] = 8;
4041 } else if (of_device_is_compatible(dev->of_node,
4042 "ti,am654-navss-mcu-udmap")) {
4043 ud->tpl_levels = 2;
4044 ud->tpl_start_idx[0] = 2;
4045 } else if (UDMA_CAP3_UCHAN_CNT(cap3)) {
4046 ud->tpl_levels = 3;
4047 ud->tpl_start_idx[1] = UDMA_CAP3_UCHAN_CNT(cap3);
Peter Ujfalusie2de9252020-12-08 11:04:21 +02004048 ud->tpl_start_idx[0] = UDMA_CAP3_HCHAN_CNT(cap3);
Peter Ujfalusidaf4ad02020-07-17 15:09:03 +03004049 } else if (UDMA_CAP3_HCHAN_CNT(cap3)) {
4050 ud->tpl_levels = 2;
4051 ud->tpl_start_idx[0] = UDMA_CAP3_HCHAN_CNT(cap3);
4052 } else {
4053 ud->tpl_levels = 1;
4054 }
4055
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004056 ud->tchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->tchan_cnt),
4057 sizeof(unsigned long), GFP_KERNEL);
4058 ud->tchans = devm_kcalloc(dev, ud->tchan_cnt, sizeof(*ud->tchans),
4059 GFP_KERNEL);
4060 ud->rchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rchan_cnt),
4061 sizeof(unsigned long), GFP_KERNEL);
4062 ud->rchans = devm_kcalloc(dev, ud->rchan_cnt, sizeof(*ud->rchans),
4063 GFP_KERNEL);
4064 ud->rflow_gp_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rflow_cnt),
4065 sizeof(unsigned long),
4066 GFP_KERNEL);
4067 ud->rflow_gp_map_allocated = devm_kcalloc(dev,
4068 BITS_TO_LONGS(ud->rflow_cnt),
4069 sizeof(unsigned long),
4070 GFP_KERNEL);
4071 ud->rflow_in_use = devm_kcalloc(dev, BITS_TO_LONGS(ud->rflow_cnt),
4072 sizeof(unsigned long),
4073 GFP_KERNEL);
4074 ud->rflows = devm_kcalloc(dev, ud->rflow_cnt, sizeof(*ud->rflows),
4075 GFP_KERNEL);
4076
4077 if (!ud->tchan_map || !ud->rchan_map || !ud->rflow_gp_map ||
4078 !ud->rflow_gp_map_allocated || !ud->tchans || !ud->rchans ||
4079 !ud->rflows || !ud->rflow_in_use)
4080 return -ENOMEM;
4081
4082 /*
4083 * RX flows with the same Ids as RX channels are reserved to be used
4084 * as default flows if remote HW can't generate flow_ids. Those
4085 * RX flows can be requested only explicitly by id.
4086 */
4087 bitmap_set(ud->rflow_gp_map_allocated, 0, ud->rchan_cnt);
4088
4089 /* by default no GP rflows are assigned to Linux */
4090 bitmap_set(ud->rflow_gp_map, 0, ud->rflow_cnt);
4091
4092 /* Get resource ranges from tisci */
Peter Ujfalusi01779472020-12-08 11:04:37 +02004093 for (i = 0; i < RM_RANGE_LAST; i++) {
4094 if (i == RM_RANGE_BCHAN)
4095 continue;
4096
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004097 tisci_rm->rm_ranges[i] =
4098 devm_ti_sci_get_of_resource(tisci_rm->tisci, dev,
4099 tisci_rm->tisci_dev_id,
4100 (char *)range_names[i]);
Peter Ujfalusi01779472020-12-08 11:04:37 +02004101 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004102
4103 /* tchan ranges */
4104 rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
4105 if (IS_ERR(rm_res)) {
4106 bitmap_zero(ud->tchan_map, ud->tchan_cnt);
4107 } else {
4108 bitmap_fill(ud->tchan_map, ud->tchan_cnt);
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004109 for (i = 0; i < rm_res->sets; i++)
4110 udma_mark_resource_ranges(ud, ud->tchan_map,
4111 &rm_res->desc[i], "tchan");
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004112 }
4113 irq_res.sets = rm_res->sets;
4114
4115 /* rchan and matching default flow ranges */
4116 rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
4117 if (IS_ERR(rm_res)) {
4118 bitmap_zero(ud->rchan_map, ud->rchan_cnt);
4119 } else {
4120 bitmap_fill(ud->rchan_map, ud->rchan_cnt);
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004121 for (i = 0; i < rm_res->sets; i++)
4122 udma_mark_resource_ranges(ud, ud->rchan_map,
4123 &rm_res->desc[i], "rchan");
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004124 }
4125
4126 irq_res.sets += rm_res->sets;
4127 irq_res.desc = kcalloc(irq_res.sets, sizeof(*irq_res.desc), GFP_KERNEL);
4128 rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
4129 for (i = 0; i < rm_res->sets; i++) {
4130 irq_res.desc[i].start = rm_res->desc[i].start;
4131 irq_res.desc[i].num = rm_res->desc[i].num;
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004132 irq_res.desc[i].start_sec = rm_res->desc[i].start_sec;
4133 irq_res.desc[i].num_sec = rm_res->desc[i].num_sec;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004134 }
4135 rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
4136 for (j = 0; j < rm_res->sets; j++, i++) {
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004137 if (rm_res->desc[j].num) {
4138 irq_res.desc[i].start = rm_res->desc[j].start +
Peter Ujfalusi01779472020-12-08 11:04:37 +02004139 ud->soc_data->oes.udma_rchan;
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004140 irq_res.desc[i].num = rm_res->desc[j].num;
4141 }
4142 if (rm_res->desc[j].num_sec) {
4143 irq_res.desc[i].start_sec = rm_res->desc[j].start_sec +
Peter Ujfalusi01779472020-12-08 11:04:37 +02004144 ud->soc_data->oes.udma_rchan;
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004145 irq_res.desc[i].num_sec = rm_res->desc[j].num_sec;
4146 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004147 }
4148 ret = ti_sci_inta_msi_domain_alloc_irqs(ud->dev, &irq_res);
4149 kfree(irq_res.desc);
4150 if (ret) {
4151 dev_err(ud->dev, "Failed to allocate MSI interrupts\n");
4152 return ret;
4153 }
4154
4155 /* GP rflow ranges */
4156 rm_res = tisci_rm->rm_ranges[RM_RANGE_RFLOW];
4157 if (IS_ERR(rm_res)) {
4158 /* all gp flows are assigned exclusively to Linux */
4159 bitmap_clear(ud->rflow_gp_map, ud->rchan_cnt,
4160 ud->rflow_cnt - ud->rchan_cnt);
4161 } else {
Peter Ujfalusi1609c152020-12-08 11:04:23 +02004162 for (i = 0; i < rm_res->sets; i++)
4163 udma_mark_resource_ranges(ud, ud->rflow_gp_map,
4164 &rm_res->desc[i], "gp-rflow");
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004165 }
4166
Peter Ujfalusi01779472020-12-08 11:04:37 +02004167 return 0;
4168}
4169
4170static int bcdma_setup_resources(struct udma_dev *ud)
4171{
4172 int ret, i, j;
4173 struct device *dev = ud->dev;
4174 struct ti_sci_resource *rm_res, irq_res;
4175 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
4176 const struct udma_oes_offsets *oes = &ud->soc_data->oes;
4177
4178 ud->bchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->bchan_cnt),
4179 sizeof(unsigned long), GFP_KERNEL);
4180 ud->bchans = devm_kcalloc(dev, ud->bchan_cnt, sizeof(*ud->bchans),
4181 GFP_KERNEL);
4182 ud->tchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->tchan_cnt),
4183 sizeof(unsigned long), GFP_KERNEL);
4184 ud->tchans = devm_kcalloc(dev, ud->tchan_cnt, sizeof(*ud->tchans),
4185 GFP_KERNEL);
4186 ud->rchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rchan_cnt),
4187 sizeof(unsigned long), GFP_KERNEL);
4188 ud->rchans = devm_kcalloc(dev, ud->rchan_cnt, sizeof(*ud->rchans),
4189 GFP_KERNEL);
4190 /* BCDMA do not really have flows, but the driver expect it */
4191 ud->rflow_in_use = devm_kcalloc(dev, BITS_TO_LONGS(ud->rchan_cnt),
4192 sizeof(unsigned long),
4193 GFP_KERNEL);
4194 ud->rflows = devm_kcalloc(dev, ud->rchan_cnt, sizeof(*ud->rflows),
4195 GFP_KERNEL);
4196
4197 if (!ud->bchan_map || !ud->tchan_map || !ud->rchan_map ||
4198 !ud->rflow_in_use || !ud->bchans || !ud->tchans || !ud->rchans ||
4199 !ud->rflows)
4200 return -ENOMEM;
4201
4202 /* TPL is not yet supported for BCDMA */
4203 ud->tpl_levels = 1;
4204
4205 /* Get resource ranges from tisci */
4206 for (i = 0; i < RM_RANGE_LAST; i++) {
4207 if (i == RM_RANGE_RFLOW)
4208 continue;
4209 if (i == RM_RANGE_BCHAN && ud->bchan_cnt == 0)
4210 continue;
4211 if (i == RM_RANGE_TCHAN && ud->tchan_cnt == 0)
4212 continue;
4213 if (i == RM_RANGE_RCHAN && ud->rchan_cnt == 0)
4214 continue;
4215
4216 tisci_rm->rm_ranges[i] =
4217 devm_ti_sci_get_of_resource(tisci_rm->tisci, dev,
4218 tisci_rm->tisci_dev_id,
4219 (char *)range_names[i]);
4220 }
4221
4222 irq_res.sets = 0;
4223
4224 /* bchan ranges */
4225 if (ud->bchan_cnt) {
4226 rm_res = tisci_rm->rm_ranges[RM_RANGE_BCHAN];
4227 if (IS_ERR(rm_res)) {
4228 bitmap_zero(ud->bchan_map, ud->bchan_cnt);
4229 } else {
4230 bitmap_fill(ud->bchan_map, ud->bchan_cnt);
4231 for (i = 0; i < rm_res->sets; i++)
4232 udma_mark_resource_ranges(ud, ud->bchan_map,
4233 &rm_res->desc[i],
4234 "bchan");
4235 }
4236 irq_res.sets += rm_res->sets;
4237 }
4238
4239 /* tchan ranges */
4240 if (ud->tchan_cnt) {
4241 rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
4242 if (IS_ERR(rm_res)) {
4243 bitmap_zero(ud->tchan_map, ud->tchan_cnt);
4244 } else {
4245 bitmap_fill(ud->tchan_map, ud->tchan_cnt);
4246 for (i = 0; i < rm_res->sets; i++)
4247 udma_mark_resource_ranges(ud, ud->tchan_map,
4248 &rm_res->desc[i],
4249 "tchan");
4250 }
4251 irq_res.sets += rm_res->sets * 2;
4252 }
4253
4254 /* rchan ranges */
4255 if (ud->rchan_cnt) {
4256 rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
4257 if (IS_ERR(rm_res)) {
4258 bitmap_zero(ud->rchan_map, ud->rchan_cnt);
4259 } else {
4260 bitmap_fill(ud->rchan_map, ud->rchan_cnt);
4261 for (i = 0; i < rm_res->sets; i++)
4262 udma_mark_resource_ranges(ud, ud->rchan_map,
4263 &rm_res->desc[i],
4264 "rchan");
4265 }
4266 irq_res.sets += rm_res->sets * 2;
4267 }
4268
4269 irq_res.desc = kcalloc(irq_res.sets, sizeof(*irq_res.desc), GFP_KERNEL);
4270 if (ud->bchan_cnt) {
4271 rm_res = tisci_rm->rm_ranges[RM_RANGE_BCHAN];
4272 for (i = 0; i < rm_res->sets; i++) {
4273 irq_res.desc[i].start = rm_res->desc[i].start +
4274 oes->bcdma_bchan_ring;
4275 irq_res.desc[i].num = rm_res->desc[i].num;
4276 }
4277 }
4278 if (ud->tchan_cnt) {
4279 rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
4280 for (j = 0; j < rm_res->sets; j++, i += 2) {
4281 irq_res.desc[i].start = rm_res->desc[j].start +
4282 oes->bcdma_tchan_data;
4283 irq_res.desc[i].num = rm_res->desc[j].num;
4284
4285 irq_res.desc[i + 1].start = rm_res->desc[j].start +
4286 oes->bcdma_tchan_ring;
4287 irq_res.desc[i + 1].num = rm_res->desc[j].num;
4288 }
4289 }
4290 if (ud->rchan_cnt) {
4291 rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
4292 for (j = 0; j < rm_res->sets; j++, i += 2) {
4293 irq_res.desc[i].start = rm_res->desc[j].start +
4294 oes->bcdma_rchan_data;
4295 irq_res.desc[i].num = rm_res->desc[j].num;
4296
4297 irq_res.desc[i + 1].start = rm_res->desc[j].start +
4298 oes->bcdma_rchan_ring;
4299 irq_res.desc[i + 1].num = rm_res->desc[j].num;
4300 }
4301 }
4302
4303 ret = ti_sci_inta_msi_domain_alloc_irqs(ud->dev, &irq_res);
4304 kfree(irq_res.desc);
4305 if (ret) {
4306 dev_err(ud->dev, "Failed to allocate MSI interrupts\n");
4307 return ret;
4308 }
4309
4310 return 0;
4311}
4312
4313static int setup_resources(struct udma_dev *ud)
4314{
4315 struct device *dev = ud->dev;
4316 int ch_count, ret;
4317
4318 switch (ud->match_data->type) {
4319 case DMA_TYPE_UDMA:
4320 ret = udma_setup_resources(ud);
4321 break;
4322 case DMA_TYPE_BCDMA:
4323 ret = bcdma_setup_resources(ud);
4324 break;
4325 default:
4326 return -EINVAL;
4327 }
4328
4329 if (ret)
4330 return ret;
4331
4332 ch_count = ud->bchan_cnt + ud->tchan_cnt + ud->rchan_cnt;
4333 if (ud->bchan_cnt)
4334 ch_count -= bitmap_weight(ud->bchan_map, ud->bchan_cnt);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004335 ch_count -= bitmap_weight(ud->tchan_map, ud->tchan_cnt);
4336 ch_count -= bitmap_weight(ud->rchan_map, ud->rchan_cnt);
4337 if (!ch_count)
4338 return -ENODEV;
4339
4340 ud->channels = devm_kcalloc(dev, ch_count, sizeof(*ud->channels),
4341 GFP_KERNEL);
4342 if (!ud->channels)
4343 return -ENOMEM;
4344
Peter Ujfalusi01779472020-12-08 11:04:37 +02004345 switch (ud->match_data->type) {
4346 case DMA_TYPE_UDMA:
4347 dev_info(dev,
4348 "Channels: %d (tchan: %u, rchan: %u, gp-rflow: %u)\n",
4349 ch_count,
4350 ud->tchan_cnt - bitmap_weight(ud->tchan_map,
4351 ud->tchan_cnt),
4352 ud->rchan_cnt - bitmap_weight(ud->rchan_map,
4353 ud->rchan_cnt),
4354 ud->rflow_cnt - bitmap_weight(ud->rflow_gp_map,
4355 ud->rflow_cnt));
4356 break;
4357 case DMA_TYPE_BCDMA:
4358 dev_info(dev,
4359 "Channels: %d (bchan: %u, tchan: %u, rchan: %u)\n",
4360 ch_count,
4361 ud->bchan_cnt - bitmap_weight(ud->bchan_map,
4362 ud->bchan_cnt),
4363 ud->tchan_cnt - bitmap_weight(ud->tchan_map,
4364 ud->tchan_cnt),
4365 ud->rchan_cnt - bitmap_weight(ud->rchan_map,
4366 ud->rchan_cnt));
4367 break;
4368 default:
4369 break;
4370 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004371
4372 return ch_count;
4373}
4374
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +02004375static int udma_setup_rx_flush(struct udma_dev *ud)
4376{
4377 struct udma_rx_flush *rx_flush = &ud->rx_flush;
4378 struct cppi5_desc_hdr_t *tr_desc;
4379 struct cppi5_tr_type1_t *tr_req;
4380 struct cppi5_host_desc_t *desc;
4381 struct device *dev = ud->dev;
4382 struct udma_hwdesc *hwdesc;
4383 size_t tr_size;
4384
4385 /* Allocate 1K buffer for discarded data on RX channel teardown */
4386 rx_flush->buffer_size = SZ_1K;
4387 rx_flush->buffer_vaddr = devm_kzalloc(dev, rx_flush->buffer_size,
4388 GFP_KERNEL);
4389 if (!rx_flush->buffer_vaddr)
4390 return -ENOMEM;
4391
4392 rx_flush->buffer_paddr = dma_map_single(dev, rx_flush->buffer_vaddr,
4393 rx_flush->buffer_size,
4394 DMA_TO_DEVICE);
4395 if (dma_mapping_error(dev, rx_flush->buffer_paddr))
4396 return -ENOMEM;
4397
4398 /* Set up descriptor to be used for TR mode */
4399 hwdesc = &rx_flush->hwdescs[0];
4400 tr_size = sizeof(struct cppi5_tr_type1_t);
4401 hwdesc->cppi5_desc_size = cppi5_trdesc_calc_size(tr_size, 1);
4402 hwdesc->cppi5_desc_size = ALIGN(hwdesc->cppi5_desc_size,
4403 ud->desc_align);
4404
4405 hwdesc->cppi5_desc_vaddr = devm_kzalloc(dev, hwdesc->cppi5_desc_size,
4406 GFP_KERNEL);
4407 if (!hwdesc->cppi5_desc_vaddr)
4408 return -ENOMEM;
4409
4410 hwdesc->cppi5_desc_paddr = dma_map_single(dev, hwdesc->cppi5_desc_vaddr,
4411 hwdesc->cppi5_desc_size,
4412 DMA_TO_DEVICE);
4413 if (dma_mapping_error(dev, hwdesc->cppi5_desc_paddr))
4414 return -ENOMEM;
4415
4416 /* Start of the TR req records */
4417 hwdesc->tr_req_base = hwdesc->cppi5_desc_vaddr + tr_size;
4418 /* Start address of the TR response array */
4419 hwdesc->tr_resp_base = hwdesc->tr_req_base + tr_size;
4420
4421 tr_desc = hwdesc->cppi5_desc_vaddr;
4422 cppi5_trdesc_init(tr_desc, 1, tr_size, 0, 0);
4423 cppi5_desc_set_pktids(tr_desc, 0, CPPI5_INFO1_DESC_FLOWID_DEFAULT);
4424 cppi5_desc_set_retpolicy(tr_desc, 0, 0);
4425
4426 tr_req = hwdesc->tr_req_base;
4427 cppi5_tr_init(&tr_req->flags, CPPI5_TR_TYPE1, false, false,
4428 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
4429 cppi5_tr_csf_set(&tr_req->flags, CPPI5_TR_CSF_SUPR_EVT);
4430
4431 tr_req->addr = rx_flush->buffer_paddr;
4432 tr_req->icnt0 = rx_flush->buffer_size;
4433 tr_req->icnt1 = 1;
4434
Peter Ujfalusi5bbeea32020-05-12 16:45:44 +03004435 dma_sync_single_for_device(dev, hwdesc->cppi5_desc_paddr,
4436 hwdesc->cppi5_desc_size, DMA_TO_DEVICE);
4437
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +02004438 /* Set up descriptor to be used for packet mode */
4439 hwdesc = &rx_flush->hwdescs[1];
4440 hwdesc->cppi5_desc_size = ALIGN(sizeof(struct cppi5_host_desc_t) +
4441 CPPI5_INFO0_HDESC_EPIB_SIZE +
4442 CPPI5_INFO0_HDESC_PSDATA_MAX_SIZE,
4443 ud->desc_align);
4444
4445 hwdesc->cppi5_desc_vaddr = devm_kzalloc(dev, hwdesc->cppi5_desc_size,
4446 GFP_KERNEL);
4447 if (!hwdesc->cppi5_desc_vaddr)
4448 return -ENOMEM;
4449
4450 hwdesc->cppi5_desc_paddr = dma_map_single(dev, hwdesc->cppi5_desc_vaddr,
4451 hwdesc->cppi5_desc_size,
4452 DMA_TO_DEVICE);
4453 if (dma_mapping_error(dev, hwdesc->cppi5_desc_paddr))
4454 return -ENOMEM;
4455
4456 desc = hwdesc->cppi5_desc_vaddr;
4457 cppi5_hdesc_init(desc, 0, 0);
4458 cppi5_desc_set_pktids(&desc->hdr, 0, CPPI5_INFO1_DESC_FLOWID_DEFAULT);
4459 cppi5_desc_set_retpolicy(&desc->hdr, 0, 0);
4460
4461 cppi5_hdesc_attach_buf(desc,
4462 rx_flush->buffer_paddr, rx_flush->buffer_size,
4463 rx_flush->buffer_paddr, rx_flush->buffer_size);
4464
4465 dma_sync_single_for_device(dev, hwdesc->cppi5_desc_paddr,
4466 hwdesc->cppi5_desc_size, DMA_TO_DEVICE);
4467 return 0;
4468}
4469
Peter Ujfalusidb8d9b42020-03-06 16:28:38 +02004470#ifdef CONFIG_DEBUG_FS
4471static void udma_dbg_summary_show_chan(struct seq_file *s,
4472 struct dma_chan *chan)
4473{
4474 struct udma_chan *uc = to_udma_chan(chan);
4475 struct udma_chan_config *ucc = &uc->config;
4476
4477 seq_printf(s, " %-13s| %s", dma_chan_name(chan),
4478 chan->dbg_client_name ?: "in-use");
Peter Ujfalusi01779472020-12-08 11:04:37 +02004479 if (ucc->tr_trigger_type)
4480 seq_puts(s, " (triggered, ");
4481 else
4482 seq_printf(s, " (%s, ",
4483 dmaengine_get_direction_text(uc->config.dir));
Peter Ujfalusidb8d9b42020-03-06 16:28:38 +02004484
4485 switch (uc->config.dir) {
4486 case DMA_MEM_TO_MEM:
Peter Ujfalusi01779472020-12-08 11:04:37 +02004487 if (uc->ud->match_data->type == DMA_TYPE_BCDMA) {
4488 seq_printf(s, "bchan%d)\n", uc->bchan->id);
4489 return;
4490 }
4491
Peter Ujfalusidb8d9b42020-03-06 16:28:38 +02004492 seq_printf(s, "chan%d pair [0x%04x -> 0x%04x], ", uc->tchan->id,
4493 ucc->src_thread, ucc->dst_thread);
4494 break;
4495 case DMA_DEV_TO_MEM:
4496 seq_printf(s, "rchan%d [0x%04x -> 0x%04x], ", uc->rchan->id,
4497 ucc->src_thread, ucc->dst_thread);
4498 break;
4499 case DMA_MEM_TO_DEV:
4500 seq_printf(s, "tchan%d [0x%04x -> 0x%04x], ", uc->tchan->id,
4501 ucc->src_thread, ucc->dst_thread);
4502 break;
4503 default:
4504 seq_printf(s, ")\n");
4505 return;
4506 }
4507
4508 if (ucc->ep_type == PSIL_EP_NATIVE) {
4509 seq_printf(s, "PSI-L Native");
4510 if (ucc->metadata_size) {
4511 seq_printf(s, "[%s", ucc->needs_epib ? " EPIB" : "");
4512 if (ucc->psd_size)
4513 seq_printf(s, " PSDsize:%u", ucc->psd_size);
4514 seq_printf(s, " ]");
4515 }
4516 } else {
4517 seq_printf(s, "PDMA");
4518 if (ucc->enable_acc32 || ucc->enable_burst)
4519 seq_printf(s, "[%s%s ]",
4520 ucc->enable_acc32 ? " ACC32" : "",
4521 ucc->enable_burst ? " BURST" : "");
4522 }
4523
4524 seq_printf(s, ", %s)\n", ucc->pkt_mode ? "Packet mode" : "TR mode");
4525}
4526
4527static void udma_dbg_summary_show(struct seq_file *s,
4528 struct dma_device *dma_dev)
4529{
4530 struct dma_chan *chan;
4531
4532 list_for_each_entry(chan, &dma_dev->channels, device_node) {
4533 if (chan->client_count)
4534 udma_dbg_summary_show_chan(s, chan);
4535 }
4536}
4537#endif /* CONFIG_DEBUG_FS */
4538
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004539#define TI_UDMAC_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
4540 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
4541 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
4542 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
4543 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
4544
4545static int udma_probe(struct platform_device *pdev)
4546{
4547 struct device_node *navss_node = pdev->dev.parent->of_node;
Peter Ujfalusif9b0366f52020-09-10 15:43:29 +03004548 const struct soc_device_attribute *soc;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004549 struct device *dev = &pdev->dev;
4550 struct udma_dev *ud;
4551 const struct of_device_id *match;
4552 int i, ret;
4553 int ch_count;
4554
4555 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(48));
4556 if (ret)
4557 dev_err(dev, "failed to set dma mask stuff\n");
4558
4559 ud = devm_kzalloc(dev, sizeof(*ud), GFP_KERNEL);
4560 if (!ud)
4561 return -ENOMEM;
4562
Peter Ujfalusi01779472020-12-08 11:04:37 +02004563 match = of_match_node(udma_of_match, dev->of_node);
4564 if (!match) {
4565 match = of_match_node(bcdma_of_match, dev->of_node);
4566 if (!match) {
4567 dev_err(dev, "No compatible match found\n");
4568 return -ENODEV;
4569 }
4570 }
4571 ud->match_data = match->data;
4572
4573 soc = soc_device_match(k3_soc_devices);
4574 if (!soc) {
4575 dev_err(dev, "No compatible SoC found\n");
4576 return -ENODEV;
4577 }
4578 ud->soc_data = soc->data;
4579
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004580 ret = udma_get_mmrs(pdev, ud);
4581 if (ret)
4582 return ret;
4583
4584 ud->tisci_rm.tisci = ti_sci_get_by_phandle(dev->of_node, "ti,sci");
4585 if (IS_ERR(ud->tisci_rm.tisci))
4586 return PTR_ERR(ud->tisci_rm.tisci);
4587
4588 ret = of_property_read_u32(dev->of_node, "ti,sci-dev-id",
4589 &ud->tisci_rm.tisci_dev_id);
4590 if (ret) {
4591 dev_err(dev, "ti,sci-dev-id read failure %d\n", ret);
4592 return ret;
4593 }
4594 pdev->id = ud->tisci_rm.tisci_dev_id;
4595
4596 ret = of_property_read_u32(navss_node, "ti,sci-dev-id",
4597 &ud->tisci_rm.tisci_navss_dev_id);
4598 if (ret) {
4599 dev_err(dev, "NAVSS ti,sci-dev-id read failure %d\n", ret);
4600 return ret;
4601 }
4602
Peter Ujfalusi01779472020-12-08 11:04:37 +02004603 if (ud->match_data->type == DMA_TYPE_UDMA) {
4604 ret = of_property_read_u32(dev->of_node, "ti,udma-atype",
4605 &ud->atype);
4606 if (!ret && ud->atype > 2) {
4607 dev_err(dev, "Invalid atype: %u\n", ud->atype);
4608 return -EINVAL;
4609 }
4610 } else {
4611 ret = of_property_read_u32(dev->of_node, "ti,asel",
4612 &ud->asel);
4613 if (!ret && ud->asel > 15) {
4614 dev_err(dev, "Invalid asel: %u\n", ud->asel);
4615 return -EINVAL;
4616 }
Peter Ujfalusi0ebcf1a2020-02-18 16:31:26 +02004617 }
4618
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004619 ud->tisci_rm.tisci_udmap_ops = &ud->tisci_rm.tisci->ops.rm_udmap_ops;
4620 ud->tisci_rm.tisci_psil_ops = &ud->tisci_rm.tisci->ops.rm_psil_ops;
4621
Peter Ujfalusi01779472020-12-08 11:04:37 +02004622 if (ud->match_data->type == DMA_TYPE_UDMA) {
4623 ud->ringacc = of_k3_ringacc_get_by_phandle(dev->of_node, "ti,ringacc");
4624 } else {
4625 struct k3_ringacc_init_data ring_init_data;
4626
4627 ring_init_data.tisci = ud->tisci_rm.tisci;
4628 ring_init_data.tisci_dev_id = ud->tisci_rm.tisci_dev_id;
4629 ring_init_data.num_rings = ud->bchan_cnt + ud->tchan_cnt +
4630 ud->rchan_cnt;
4631
4632 ud->ringacc = k3_ringacc_dmarings_init(pdev, &ring_init_data);
4633 }
4634
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004635 if (IS_ERR(ud->ringacc))
4636 return PTR_ERR(ud->ringacc);
4637
4638 dev->msi_domain = of_msi_get_domain(dev, dev->of_node,
4639 DOMAIN_BUS_TI_SCI_INTA_MSI);
4640 if (!dev->msi_domain) {
4641 dev_err(dev, "Failed to get MSI domain\n");
4642 return -EPROBE_DEFER;
4643 }
4644
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004645 dma_cap_set(DMA_SLAVE, ud->ddev.cap_mask);
4646 dma_cap_set(DMA_CYCLIC, ud->ddev.cap_mask);
4647
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004648 ud->ddev.device_config = udma_slave_config;
4649 ud->ddev.device_prep_slave_sg = udma_prep_slave_sg;
4650 ud->ddev.device_prep_dma_cyclic = udma_prep_dma_cyclic;
4651 ud->ddev.device_issue_pending = udma_issue_pending;
4652 ud->ddev.device_tx_status = udma_tx_status;
4653 ud->ddev.device_pause = udma_pause;
4654 ud->ddev.device_resume = udma_resume;
4655 ud->ddev.device_terminate_all = udma_terminate_all;
4656 ud->ddev.device_synchronize = udma_synchronize;
Peter Ujfalusidb8d9b42020-03-06 16:28:38 +02004657#ifdef CONFIG_DEBUG_FS
4658 ud->ddev.dbg_summary_show = udma_dbg_summary_show;
4659#endif
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004660
Peter Ujfalusi01779472020-12-08 11:04:37 +02004661 switch (ud->match_data->type) {
4662 case DMA_TYPE_UDMA:
4663 ud->ddev.device_alloc_chan_resources =
4664 udma_alloc_chan_resources;
4665 break;
4666 case DMA_TYPE_BCDMA:
4667 ud->ddev.device_alloc_chan_resources =
4668 bcdma_alloc_chan_resources;
4669 ud->ddev.device_router_config = bcdma_router_config;
4670 break;
4671 default:
4672 return -EINVAL;
4673 }
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004674 ud->ddev.device_free_chan_resources = udma_free_chan_resources;
Peter Ujfalusi01779472020-12-08 11:04:37 +02004675
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004676 ud->ddev.src_addr_widths = TI_UDMAC_BUSWIDTHS;
4677 ud->ddev.dst_addr_widths = TI_UDMAC_BUSWIDTHS;
4678 ud->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
4679 ud->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
4680 ud->ddev.copy_align = DMAENGINE_ALIGN_8_BYTES;
4681 ud->ddev.desc_metadata_modes = DESC_METADATA_CLIENT |
4682 DESC_METADATA_ENGINE;
Peter Ujfalusi01779472020-12-08 11:04:37 +02004683 if (ud->match_data->enable_memcpy_support &&
4684 !(ud->match_data->type == DMA_TYPE_BCDMA && ud->bchan_cnt == 0)) {
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004685 dma_cap_set(DMA_MEMCPY, ud->ddev.cap_mask);
4686 ud->ddev.device_prep_dma_memcpy = udma_prep_dma_memcpy;
4687 ud->ddev.directions |= BIT(DMA_MEM_TO_MEM);
4688 }
4689
4690 ud->ddev.dev = dev;
4691 ud->dev = dev;
4692 ud->psil_base = ud->match_data->psil_base;
4693
4694 INIT_LIST_HEAD(&ud->ddev.channels);
4695 INIT_LIST_HEAD(&ud->desc_to_purge);
4696
Peter Ujfalusi01779472020-12-08 11:04:37 +02004697 ch_count = setup_resources(ud);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004698 if (ch_count <= 0)
4699 return ch_count;
4700
4701 spin_lock_init(&ud->lock);
4702 INIT_WORK(&ud->purge_work, udma_purge_desc_work);
4703
4704 ud->desc_align = 64;
4705 if (ud->desc_align < dma_get_cache_alignment())
4706 ud->desc_align = dma_get_cache_alignment();
4707
Peter Ujfalusi16cd3c62020-02-14 11:14:37 +02004708 ret = udma_setup_rx_flush(ud);
4709 if (ret)
4710 return ret;
4711
Peter Ujfalusi01779472020-12-08 11:04:37 +02004712 for (i = 0; i < ud->bchan_cnt; i++) {
4713 struct udma_bchan *bchan = &ud->bchans[i];
4714
4715 bchan->id = i;
4716 bchan->reg_rt = ud->mmrs[MMR_BCHANRT] + i * 0x1000;
4717 }
4718
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004719 for (i = 0; i < ud->tchan_cnt; i++) {
4720 struct udma_tchan *tchan = &ud->tchans[i];
4721
4722 tchan->id = i;
4723 tchan->reg_rt = ud->mmrs[MMR_TCHANRT] + i * 0x1000;
4724 }
4725
4726 for (i = 0; i < ud->rchan_cnt; i++) {
4727 struct udma_rchan *rchan = &ud->rchans[i];
4728
4729 rchan->id = i;
4730 rchan->reg_rt = ud->mmrs[MMR_RCHANRT] + i * 0x1000;
4731 }
4732
4733 for (i = 0; i < ud->rflow_cnt; i++) {
4734 struct udma_rflow *rflow = &ud->rflows[i];
4735
4736 rflow->id = i;
4737 }
4738
4739 for (i = 0; i < ch_count; i++) {
4740 struct udma_chan *uc = &ud->channels[i];
4741
4742 uc->ud = ud;
4743 uc->vc.desc_free = udma_desc_free;
4744 uc->id = i;
Peter Ujfalusi01779472020-12-08 11:04:37 +02004745 uc->bchan = NULL;
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004746 uc->tchan = NULL;
4747 uc->rchan = NULL;
4748 uc->config.remote_thread_id = -1;
4749 uc->config.dir = DMA_MEM_TO_MEM;
4750 uc->name = devm_kasprintf(dev, GFP_KERNEL, "%s chan%d",
4751 dev_name(dev), i);
4752
4753 vchan_init(&uc->vc, &ud->ddev);
4754 /* Use custom vchan completion handling */
Allen Pais2fa9bc92020-08-31 16:05:42 +05304755 tasklet_setup(&uc->vc.task, udma_vchan_complete);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004756 init_completion(&uc->teardown_completed);
Peter Ujfalusid964d5f2020-06-18 14:40:04 +03004757 INIT_DELAYED_WORK(&uc->tx_drain.work, udma_check_tx_completion);
Peter Ujfalusi25dcb5d2019-12-23 13:04:50 +02004758 }
4759
4760 ret = dma_async_device_register(&ud->ddev);
4761 if (ret) {
4762 dev_err(dev, "failed to register slave DMA engine: %d\n", ret);
4763 return ret;
4764 }
4765
4766 platform_set_drvdata(pdev, ud);
4767
4768 ret = of_dma_controller_register(dev->of_node, udma_of_xlate, ud);
4769 if (ret) {
4770 dev_err(dev, "failed to register of_dma controller\n");
4771 dma_async_device_unregister(&ud->ddev);
4772 }
4773
4774 return ret;
4775}
4776
4777static struct platform_driver udma_driver = {
4778 .driver = {
4779 .name = "ti-udma",
4780 .of_match_table = udma_of_match,
4781 .suppress_bind_attrs = true,
4782 },
4783 .probe = udma_probe,
4784};
4785builtin_platform_driver(udma_driver);
Grygorii Strashkod7024192019-12-23 13:04:51 +02004786
Peter Ujfalusi01779472020-12-08 11:04:37 +02004787static struct platform_driver bcdma_driver = {
4788 .driver = {
4789 .name = "ti-bcdma",
4790 .of_match_table = bcdma_of_match,
4791 .suppress_bind_attrs = true,
4792 },
4793 .probe = udma_probe,
4794};
4795builtin_platform_driver(bcdma_driver);
4796
Grygorii Strashkod7024192019-12-23 13:04:51 +02004797/* Private interfaces to UDMA */
4798#include "k3-udma-private.c"