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