1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DMA driver for Xilinx DMA/Bridge Subsystem
5 * Copyright (C) 2017-2020 Xilinx, Inc. All rights reserved.
6 * Copyright (C) 2022, Advanced Micro Devices, Inc.
10 * The DMA/Bridge Subsystem for PCI Express allows for the movement of data
11 * between Host memory and the DMA subsystem. It does this by operating on
12 * 'descriptors' that contain information about the source, destination and
13 * amount of data to transfer. These direct memory transfers can be both in
14 * the Host to Card (H2C) and Card to Host (C2H) transfers. The DMA can be
15 * configured to have a single AXI4 Master interface shared by all channels
16 * or one AXI4-Stream interface for each channel enabled. Memory transfers are
17 * specified on a per-channel basis in descriptor linked lists, which the DMA
18 * fetches from host memory and processes. Events such as descriptor completion
19 * and errors are signaled using interrupts. The core also provides up to 16
20 * user interrupt wires that generate interrupts to the host.
23 #include <linux/mod_devicetable.h>
24 #include <linux/bitfield.h>
25 #include <linux/dmapool.h>
26 #include <linux/regmap.h>
27 #include <linux/dmaengine.h>
28 #include <linux/dma/amd_xdma.h>
29 #include <linux/platform_device.h>
30 #include <linux/platform_data/amd_xdma.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/pci.h>
33 #include "../virt-dma.h"
34 #include "xdma-regs.h"
36 /* mmio regmap config for all XDMA registers */
37 static const struct regmap_config xdma_regmap_config
= {
41 .max_register
= XDMA_REG_SPACE_LEN
,
45 * struct xdma_desc_block - Descriptor block
46 * @virt_addr: Virtual address of block start
47 * @dma_addr: DMA address of block start
49 struct xdma_desc_block
{
55 * struct xdma_chan - Driver specific DMA channel structure
56 * @vchan: Virtual channel
57 * @xdev_hdl: Pointer to DMA device structure
58 * @base: Offset of channel registers
59 * @desc_pool: Descriptor pool
60 * @busy: Busy flag of the channel
61 * @dir: Transferring direction of the channel
62 * @cfg: Transferring config of the channel
63 * @irq: IRQ assigned to the channel
66 struct virt_dma_chan vchan
;
69 struct dma_pool
*desc_pool
;
71 enum dma_transfer_direction dir
;
72 struct dma_slave_config cfg
;
74 struct completion last_interrupt
;
79 * struct xdma_desc - DMA desc structure
80 * @vdesc: Virtual DMA descriptor
81 * @chan: DMA channel pointer
82 * @dir: Transferring direction of the request
83 * @desc_blocks: Hardware descriptor blocks
84 * @dblk_num: Number of hardware descriptor blocks
85 * @desc_num: Number of hardware descriptors
86 * @completed_desc_num: Completed hardware descriptors
87 * @cyclic: Cyclic transfer vs. scatter-gather
88 * @interleaved_dma: Interleaved DMA transfer
89 * @periods: Number of periods in the cyclic transfer
90 * @period_size: Size of a period in bytes in cyclic transfers
91 * @frames_left: Number of frames left in interleaved DMA transfer
92 * @error: tx error flag
95 struct virt_dma_desc vdesc
;
96 struct xdma_chan
*chan
;
97 enum dma_transfer_direction dir
;
98 struct xdma_desc_block
*desc_blocks
;
101 u32 completed_desc_num
;
103 bool interleaved_dma
;
110 #define XDMA_DEV_STATUS_REG_DMA BIT(0)
111 #define XDMA_DEV_STATUS_INIT_MSIX BIT(1)
114 * struct xdma_device - DMA device structure
115 * @pdev: Platform device pointer
116 * @dma_dev: DMA device structure
117 * @rmap: MMIO regmap for DMA registers
118 * @h2c_chans: Host to Card channels
119 * @c2h_chans: Card to Host channels
120 * @h2c_chan_num: Number of H2C channels
121 * @c2h_chan_num: Number of C2H channels
122 * @irq_start: Start IRQ assigned to device
123 * @irq_num: Number of IRQ assigned to device
124 * @status: Initialization status
127 struct platform_device
*pdev
;
128 struct dma_device dma_dev
;
130 struct xdma_chan
*h2c_chans
;
131 struct xdma_chan
*c2h_chans
;
139 #define xdma_err(xdev, fmt, args...) \
140 dev_err(&(xdev)->pdev->dev, fmt, ##args)
141 #define XDMA_CHAN_NUM(_xd) ({ \
142 typeof(_xd) (xd) = (_xd); \
143 ((xd)->h2c_chan_num + (xd)->c2h_chan_num); })
145 /* Get the last desc in a desc block */
146 static inline void *xdma_blk_last_desc(struct xdma_desc_block
*block
)
148 return block
->virt_addr
+ (XDMA_DESC_ADJACENT
- 1) * XDMA_DESC_SIZE
;
152 * xdma_link_sg_desc_blocks - Link SG descriptor blocks for DMA transfer
153 * @sw_desc: Tx descriptor pointer
155 static void xdma_link_sg_desc_blocks(struct xdma_desc
*sw_desc
)
157 struct xdma_desc_block
*block
;
158 u32 last_blk_desc
, desc_control
;
159 struct xdma_hw_desc
*desc
;
162 desc_control
= XDMA_DESC_CONTROL(XDMA_DESC_ADJACENT
, 0);
163 for (i
= 1; i
< sw_desc
->dblk_num
; i
++) {
164 block
= &sw_desc
->desc_blocks
[i
- 1];
165 desc
= xdma_blk_last_desc(block
);
167 if (!(i
& XDMA_DESC_BLOCK_MASK
)) {
168 desc
->control
= cpu_to_le32(XDMA_DESC_CONTROL_LAST
);
171 desc
->control
= cpu_to_le32(desc_control
);
172 desc
->next_desc
= cpu_to_le64(block
[1].dma_addr
);
175 /* update the last block */
176 last_blk_desc
= (sw_desc
->desc_num
- 1) & XDMA_DESC_ADJACENT_MASK
;
177 if (((sw_desc
->dblk_num
- 1) & XDMA_DESC_BLOCK_MASK
) > 0) {
178 block
= &sw_desc
->desc_blocks
[sw_desc
->dblk_num
- 2];
179 desc
= xdma_blk_last_desc(block
);
180 desc_control
= XDMA_DESC_CONTROL(last_blk_desc
+ 1, 0);
181 desc
->control
= cpu_to_le32(desc_control
);
184 block
= &sw_desc
->desc_blocks
[sw_desc
->dblk_num
- 1];
185 desc
= block
->virt_addr
+ last_blk_desc
* XDMA_DESC_SIZE
;
186 desc
->control
= cpu_to_le32(XDMA_DESC_CONTROL_LAST
);
190 * xdma_link_cyclic_desc_blocks - Link cyclic descriptor blocks for DMA transfer
191 * @sw_desc: Tx descriptor pointer
193 static void xdma_link_cyclic_desc_blocks(struct xdma_desc
*sw_desc
)
195 struct xdma_desc_block
*block
;
196 struct xdma_hw_desc
*desc
;
199 block
= sw_desc
->desc_blocks
;
200 for (i
= 0; i
< sw_desc
->desc_num
- 1; i
++) {
201 desc
= block
->virt_addr
+ i
* XDMA_DESC_SIZE
;
202 desc
->next_desc
= cpu_to_le64(block
->dma_addr
+ ((i
+ 1) * XDMA_DESC_SIZE
));
204 desc
= block
->virt_addr
+ i
* XDMA_DESC_SIZE
;
205 desc
->next_desc
= cpu_to_le64(block
->dma_addr
);
208 static inline struct xdma_chan
*to_xdma_chan(struct dma_chan
*chan
)
210 return container_of(chan
, struct xdma_chan
, vchan
.chan
);
213 static inline struct xdma_desc
*to_xdma_desc(struct virt_dma_desc
*vdesc
)
215 return container_of(vdesc
, struct xdma_desc
, vdesc
);
219 * xdma_channel_init - Initialize DMA channel registers
220 * @chan: DMA channel pointer
222 static int xdma_channel_init(struct xdma_chan
*chan
)
224 struct xdma_device
*xdev
= chan
->xdev_hdl
;
227 ret
= regmap_write(xdev
->rmap
, chan
->base
+ XDMA_CHAN_CONTROL_W1C
,
228 CHAN_CTRL_NON_INCR_ADDR
);
232 ret
= regmap_write(xdev
->rmap
, chan
->base
+ XDMA_CHAN_INTR_ENABLE
,
241 * xdma_free_desc - Free descriptor
242 * @vdesc: Virtual DMA descriptor
244 static void xdma_free_desc(struct virt_dma_desc
*vdesc
)
246 struct xdma_desc
*sw_desc
;
249 sw_desc
= to_xdma_desc(vdesc
);
250 for (i
= 0; i
< sw_desc
->dblk_num
; i
++) {
251 if (!sw_desc
->desc_blocks
[i
].virt_addr
)
253 dma_pool_free(sw_desc
->chan
->desc_pool
,
254 sw_desc
->desc_blocks
[i
].virt_addr
,
255 sw_desc
->desc_blocks
[i
].dma_addr
);
257 kfree(sw_desc
->desc_blocks
);
262 * xdma_alloc_desc - Allocate descriptor
263 * @chan: DMA channel pointer
264 * @desc_num: Number of hardware descriptors
265 * @cyclic: Whether this is a cyclic transfer
267 static struct xdma_desc
*
268 xdma_alloc_desc(struct xdma_chan
*chan
, u32 desc_num
, bool cyclic
)
270 struct xdma_desc
*sw_desc
;
271 struct xdma_hw_desc
*desc
;
278 sw_desc
= kzalloc(sizeof(*sw_desc
), GFP_NOWAIT
);
282 sw_desc
->chan
= chan
;
283 sw_desc
->desc_num
= desc_num
;
284 sw_desc
->cyclic
= cyclic
;
285 sw_desc
->error
= false;
286 dblk_num
= DIV_ROUND_UP(desc_num
, XDMA_DESC_ADJACENT
);
287 sw_desc
->desc_blocks
= kcalloc(dblk_num
, sizeof(*sw_desc
->desc_blocks
),
289 if (!sw_desc
->desc_blocks
)
293 control
= XDMA_DESC_CONTROL_CYCLIC
;
295 control
= XDMA_DESC_CONTROL(1, 0);
297 sw_desc
->dblk_num
= dblk_num
;
298 for (i
= 0; i
< sw_desc
->dblk_num
; i
++) {
299 addr
= dma_pool_alloc(chan
->desc_pool
, GFP_NOWAIT
, &dma_addr
);
303 sw_desc
->desc_blocks
[i
].virt_addr
= addr
;
304 sw_desc
->desc_blocks
[i
].dma_addr
= dma_addr
;
305 for (j
= 0, desc
= addr
; j
< XDMA_DESC_ADJACENT
; j
++)
306 desc
[j
].control
= cpu_to_le32(control
);
310 xdma_link_cyclic_desc_blocks(sw_desc
);
312 xdma_link_sg_desc_blocks(sw_desc
);
317 xdma_free_desc(&sw_desc
->vdesc
);
322 * xdma_xfer_start - Start DMA transfer
323 * @xchan: DMA channel pointer
325 static int xdma_xfer_start(struct xdma_chan
*xchan
)
327 struct virt_dma_desc
*vd
= vchan_next_desc(&xchan
->vchan
);
328 struct xdma_device
*xdev
= xchan
->xdev_hdl
;
329 struct xdma_desc_block
*block
;
330 u32 val
, completed_blocks
;
331 struct xdma_desc
*desc
;
335 * check if there is not any submitted descriptor or channel is busy.
336 * vchan lock should be held where this function is called.
338 if (!vd
|| xchan
->busy
)
341 /* clear run stop bit to get ready for transfer */
342 ret
= regmap_write(xdev
->rmap
, xchan
->base
+ XDMA_CHAN_CONTROL_W1C
,
347 desc
= to_xdma_desc(vd
);
348 if (desc
->dir
!= xchan
->dir
) {
349 xdma_err(xdev
, "incorrect request direction");
353 /* set DMA engine to the first descriptor block */
354 completed_blocks
= desc
->completed_desc_num
/ XDMA_DESC_ADJACENT
;
355 block
= &desc
->desc_blocks
[completed_blocks
];
356 val
= lower_32_bits(block
->dma_addr
);
357 ret
= regmap_write(xdev
->rmap
, xchan
->base
+ XDMA_SGDMA_DESC_LO
, val
);
361 val
= upper_32_bits(block
->dma_addr
);
362 ret
= regmap_write(xdev
->rmap
, xchan
->base
+ XDMA_SGDMA_DESC_HI
, val
);
366 if (completed_blocks
+ 1 == desc
->dblk_num
)
367 val
= (desc
->desc_num
- 1) & XDMA_DESC_ADJACENT_MASK
;
369 val
= XDMA_DESC_ADJACENT
- 1;
370 ret
= regmap_write(xdev
->rmap
, xchan
->base
+ XDMA_SGDMA_DESC_ADJ
, val
);
374 /* kick off DMA transfer */
375 ret
= regmap_write(xdev
->rmap
, xchan
->base
+ XDMA_CHAN_CONTROL
,
381 xchan
->stop_requested
= false;
382 reinit_completion(&xchan
->last_interrupt
);
388 * xdma_xfer_stop - Stop DMA transfer
389 * @xchan: DMA channel pointer
391 static int xdma_xfer_stop(struct xdma_chan
*xchan
)
394 struct xdma_device
*xdev
= xchan
->xdev_hdl
;
396 /* clear run stop bit to prevent any further auto-triggering */
397 ret
= regmap_write(xdev
->rmap
, xchan
->base
+ XDMA_CHAN_CONTROL_W1C
,
405 * xdma_alloc_channels - Detect and allocate DMA channels
406 * @xdev: DMA device pointer
407 * @dir: Channel direction
409 static int xdma_alloc_channels(struct xdma_device
*xdev
,
410 enum dma_transfer_direction dir
)
412 struct xdma_platdata
*pdata
= dev_get_platdata(&xdev
->pdev
->dev
);
413 struct xdma_chan
**chans
, *xchan
;
414 u32 base
, identifier
, target
;
418 if (dir
== DMA_MEM_TO_DEV
) {
419 base
= XDMA_CHAN_H2C_OFFSET
;
420 target
= XDMA_CHAN_H2C_TARGET
;
421 chans
= &xdev
->h2c_chans
;
422 chan_num
= &xdev
->h2c_chan_num
;
423 } else if (dir
== DMA_DEV_TO_MEM
) {
424 base
= XDMA_CHAN_C2H_OFFSET
;
425 target
= XDMA_CHAN_C2H_TARGET
;
426 chans
= &xdev
->c2h_chans
;
427 chan_num
= &xdev
->c2h_chan_num
;
429 xdma_err(xdev
, "invalid direction specified");
433 /* detect number of available DMA channels */
434 for (i
= 0, *chan_num
= 0; i
< pdata
->max_dma_channels
; i
++) {
435 ret
= regmap_read(xdev
->rmap
, base
+ i
* XDMA_CHAN_STRIDE
,
440 /* check if it is available DMA channel */
441 if (XDMA_CHAN_CHECK_TARGET(identifier
, target
))
446 xdma_err(xdev
, "does not probe any channel");
450 *chans
= devm_kcalloc(&xdev
->pdev
->dev
, *chan_num
, sizeof(**chans
),
455 for (i
= 0, j
= 0; i
< pdata
->max_dma_channels
; i
++) {
456 ret
= regmap_read(xdev
->rmap
, base
+ i
* XDMA_CHAN_STRIDE
,
461 if (!XDMA_CHAN_CHECK_TARGET(identifier
, target
))
464 if (j
== *chan_num
) {
465 xdma_err(xdev
, "invalid channel number");
469 /* init channel structure and hardware */
470 xchan
= &(*chans
)[j
];
471 xchan
->xdev_hdl
= xdev
;
472 xchan
->base
= base
+ i
* XDMA_CHAN_STRIDE
;
474 xchan
->stop_requested
= false;
475 init_completion(&xchan
->last_interrupt
);
477 ret
= xdma_channel_init(xchan
);
480 xchan
->vchan
.desc_free
= xdma_free_desc
;
481 vchan_init(&xchan
->vchan
, &xdev
->dma_dev
);
486 dev_info(&xdev
->pdev
->dev
, "configured %d %s channels", j
,
487 (dir
== DMA_MEM_TO_DEV
) ? "H2C" : "C2H");
493 * xdma_issue_pending - Issue pending transactions
494 * @chan: DMA channel pointer
496 static void xdma_issue_pending(struct dma_chan
*chan
)
498 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
501 spin_lock_irqsave(&xdma_chan
->vchan
.lock
, flags
);
502 if (vchan_issue_pending(&xdma_chan
->vchan
))
503 xdma_xfer_start(xdma_chan
);
504 spin_unlock_irqrestore(&xdma_chan
->vchan
.lock
, flags
);
508 * xdma_terminate_all - Terminate all transactions
509 * @chan: DMA channel pointer
511 static int xdma_terminate_all(struct dma_chan
*chan
)
513 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
514 struct virt_dma_desc
*vd
;
518 xdma_xfer_stop(xdma_chan
);
520 spin_lock_irqsave(&xdma_chan
->vchan
.lock
, flags
);
522 xdma_chan
->busy
= false;
523 xdma_chan
->stop_requested
= true;
524 vd
= vchan_next_desc(&xdma_chan
->vchan
);
527 dma_cookie_complete(&vd
->tx
);
528 vchan_terminate_vdesc(vd
);
530 vchan_get_all_descriptors(&xdma_chan
->vchan
, &head
);
531 list_splice_tail(&head
, &xdma_chan
->vchan
.desc_terminated
);
533 spin_unlock_irqrestore(&xdma_chan
->vchan
.lock
, flags
);
539 * xdma_synchronize - Synchronize terminated transactions
540 * @chan: DMA channel pointer
542 static void xdma_synchronize(struct dma_chan
*chan
)
544 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
545 struct xdma_device
*xdev
= xdma_chan
->xdev_hdl
;
548 /* If the engine continues running, wait for the last interrupt */
549 regmap_read(xdev
->rmap
, xdma_chan
->base
+ XDMA_CHAN_STATUS
, &st
);
550 if (st
& XDMA_CHAN_STATUS_BUSY
)
551 wait_for_completion_timeout(&xdma_chan
->last_interrupt
, msecs_to_jiffies(1000));
553 vchan_synchronize(&xdma_chan
->vchan
);
557 * xdma_fill_descs() - Fill hardware descriptors for one contiguous memory chunk.
558 * More than one descriptor will be used if the size is bigger
559 * than XDMA_DESC_BLEN_MAX.
560 * @sw_desc: Descriptor container
561 * @src_addr: First value for the ->src_addr field
562 * @dst_addr: First value for the ->dst_addr field
563 * @size: Size of the contiguous memory block
564 * @filled_descs_num: Index of the first descriptor to take care of in @sw_desc
566 static inline u32
xdma_fill_descs(struct xdma_desc
*sw_desc
, u64 src_addr
,
567 u64 dst_addr
, u32 size
, u32 filled_descs_num
)
569 u32 left
= size
, len
, desc_num
= filled_descs_num
;
570 struct xdma_desc_block
*dblk
;
571 struct xdma_hw_desc
*desc
;
573 dblk
= sw_desc
->desc_blocks
+ (desc_num
/ XDMA_DESC_ADJACENT
);
574 desc
= dblk
->virt_addr
;
575 desc
+= desc_num
& XDMA_DESC_ADJACENT_MASK
;
577 len
= min_t(u32
, left
, XDMA_DESC_BLEN_MAX
);
578 /* set hardware descriptor */
579 desc
->bytes
= cpu_to_le32(len
);
580 desc
->src_addr
= cpu_to_le64(src_addr
);
581 desc
->dst_addr
= cpu_to_le64(dst_addr
);
582 if (!(++desc_num
& XDMA_DESC_ADJACENT_MASK
))
583 desc
= (++dblk
)->virt_addr
;
592 return desc_num
- filled_descs_num
;
596 * xdma_prep_device_sg - prepare a descriptor for a DMA transaction
597 * @chan: DMA channel pointer
598 * @sgl: Transfer scatter gather list
599 * @sg_len: Length of scatter gather list
600 * @dir: Transfer direction
601 * @flags: transfer ack flags
602 * @context: APP words of the descriptor
604 static struct dma_async_tx_descriptor
*
605 xdma_prep_device_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
606 unsigned int sg_len
, enum dma_transfer_direction dir
,
607 unsigned long flags
, void *context
)
609 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
610 struct dma_async_tx_descriptor
*tx_desc
;
611 struct xdma_desc
*sw_desc
;
613 u64 addr
, dev_addr
, *src
, *dst
;
614 struct scatterlist
*sg
;
616 for_each_sg(sgl
, sg
, sg_len
, i
)
617 desc_num
+= DIV_ROUND_UP(sg_dma_len(sg
), XDMA_DESC_BLEN_MAX
);
619 sw_desc
= xdma_alloc_desc(xdma_chan
, desc_num
, false);
623 sw_desc
->cyclic
= false;
624 sw_desc
->interleaved_dma
= false;
626 if (dir
== DMA_MEM_TO_DEV
) {
627 dev_addr
= xdma_chan
->cfg
.dst_addr
;
631 dev_addr
= xdma_chan
->cfg
.src_addr
;
637 for_each_sg(sgl
, sg
, sg_len
, i
) {
638 addr
= sg_dma_address(sg
);
639 desc_num
+= xdma_fill_descs(sw_desc
, *src
, *dst
, sg_dma_len(sg
), desc_num
);
640 dev_addr
+= sg_dma_len(sg
);
643 tx_desc
= vchan_tx_prep(&xdma_chan
->vchan
, &sw_desc
->vdesc
, flags
);
650 xdma_free_desc(&sw_desc
->vdesc
);
656 * xdma_prep_dma_cyclic - prepare for cyclic DMA transactions
657 * @chan: DMA channel pointer
658 * @address: Device DMA address to access
659 * @size: Total length to transfer
660 * @period_size: Period size to use for each transfer
661 * @dir: Transfer direction
662 * @flags: Transfer ack flags
664 static struct dma_async_tx_descriptor
*
665 xdma_prep_dma_cyclic(struct dma_chan
*chan
, dma_addr_t address
,
666 size_t size
, size_t period_size
,
667 enum dma_transfer_direction dir
,
670 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
671 struct xdma_device
*xdev
= xdma_chan
->xdev_hdl
;
672 unsigned int periods
= size
/ period_size
;
673 struct dma_async_tx_descriptor
*tx_desc
;
674 struct xdma_desc
*sw_desc
;
675 u64 addr
, dev_addr
, *src
, *dst
;
680 * Simplify the whole logic by preventing an abnormally high number of
681 * periods and periods size.
683 if (period_size
> XDMA_DESC_BLEN_MAX
) {
684 xdma_err(xdev
, "period size limited to %lu bytes\n", XDMA_DESC_BLEN_MAX
);
688 if (periods
> XDMA_DESC_ADJACENT
) {
689 xdma_err(xdev
, "number of periods limited to %u\n", XDMA_DESC_ADJACENT
);
693 sw_desc
= xdma_alloc_desc(xdma_chan
, periods
, true);
697 sw_desc
->periods
= periods
;
698 sw_desc
->period_size
= period_size
;
700 sw_desc
->interleaved_dma
= false;
703 if (dir
== DMA_MEM_TO_DEV
) {
704 dev_addr
= xdma_chan
->cfg
.dst_addr
;
708 dev_addr
= xdma_chan
->cfg
.src_addr
;
714 for (i
= 0; i
< periods
; i
++) {
715 desc_num
+= xdma_fill_descs(sw_desc
, *src
, *dst
, period_size
, desc_num
);
719 tx_desc
= vchan_tx_prep(&xdma_chan
->vchan
, &sw_desc
->vdesc
, flags
);
726 xdma_free_desc(&sw_desc
->vdesc
);
732 * xdma_prep_interleaved_dma - Prepare virtual descriptor for interleaved DMA transfers
734 * @xt: DMA transfer template
737 static struct dma_async_tx_descriptor
*
738 xdma_prep_interleaved_dma(struct dma_chan
*chan
,
739 struct dma_interleaved_template
*xt
,
743 u32 desc_num
= 0, period_size
= 0;
744 struct dma_async_tx_descriptor
*tx_desc
;
745 struct xdma_chan
*xchan
= to_xdma_chan(chan
);
746 struct xdma_desc
*sw_desc
;
747 u64 src_addr
, dst_addr
;
749 for (i
= 0; i
< xt
->frame_size
; ++i
)
750 desc_num
+= DIV_ROUND_UP(xt
->sgl
[i
].size
, XDMA_DESC_BLEN_MAX
);
752 sw_desc
= xdma_alloc_desc(xchan
, desc_num
, false);
755 sw_desc
->dir
= xt
->dir
;
756 sw_desc
->interleaved_dma
= true;
757 sw_desc
->cyclic
= flags
& DMA_PREP_REPEAT
;
758 sw_desc
->frames_left
= xt
->numf
;
759 sw_desc
->periods
= xt
->numf
;
762 src_addr
= xt
->src_start
;
763 dst_addr
= xt
->dst_start
;
764 for (i
= 0; i
< xt
->frame_size
; ++i
) {
765 desc_num
+= xdma_fill_descs(sw_desc
, src_addr
, dst_addr
, xt
->sgl
[i
].size
, desc_num
);
766 src_addr
+= dmaengine_get_src_icg(xt
, &xt
->sgl
[i
]) + (xt
->src_inc
?
767 xt
->sgl
[i
].size
: 0);
768 dst_addr
+= dmaengine_get_dst_icg(xt
, &xt
->sgl
[i
]) + (xt
->dst_inc
?
769 xt
->sgl
[i
].size
: 0);
770 period_size
+= xt
->sgl
[i
].size
;
772 sw_desc
->period_size
= period_size
;
774 tx_desc
= vchan_tx_prep(&xchan
->vchan
, &sw_desc
->vdesc
, flags
);
778 xdma_free_desc(&sw_desc
->vdesc
);
783 * xdma_device_config - Configure the DMA channel
785 * @cfg: channel configuration
787 static int xdma_device_config(struct dma_chan
*chan
,
788 struct dma_slave_config
*cfg
)
790 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
792 memcpy(&xdma_chan
->cfg
, cfg
, sizeof(*cfg
));
798 * xdma_free_chan_resources - Free channel resources
801 static void xdma_free_chan_resources(struct dma_chan
*chan
)
803 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
805 vchan_free_chan_resources(&xdma_chan
->vchan
);
806 dma_pool_destroy(xdma_chan
->desc_pool
);
807 xdma_chan
->desc_pool
= NULL
;
811 * xdma_alloc_chan_resources - Allocate channel resources
814 static int xdma_alloc_chan_resources(struct dma_chan
*chan
)
816 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
817 struct xdma_device
*xdev
= xdma_chan
->xdev_hdl
;
818 struct device
*dev
= xdev
->dma_dev
.dev
;
820 while (dev
&& !dev_is_pci(dev
))
823 xdma_err(xdev
, "unable to find pci device");
827 xdma_chan
->desc_pool
= dma_pool_create(dma_chan_name(chan
), dev
, XDMA_DESC_BLOCK_SIZE
,
828 XDMA_DESC_BLOCK_ALIGN
, XDMA_DESC_BLOCK_BOUNDARY
);
829 if (!xdma_chan
->desc_pool
) {
830 xdma_err(xdev
, "unable to allocate descriptor pool");
837 static enum dma_status
xdma_tx_status(struct dma_chan
*chan
, dma_cookie_t cookie
,
838 struct dma_tx_state
*state
)
840 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
841 struct xdma_desc
*desc
= NULL
;
842 struct virt_dma_desc
*vd
;
845 unsigned int period_idx
;
848 ret
= dma_cookie_status(chan
, cookie
, state
);
849 if (ret
== DMA_COMPLETE
)
852 spin_lock_irqsave(&xdma_chan
->vchan
.lock
, flags
);
854 vd
= vchan_find_desc(&xdma_chan
->vchan
, cookie
);
858 desc
= to_xdma_desc(vd
);
861 } else if (desc
->cyclic
) {
862 period_idx
= desc
->completed_desc_num
% desc
->periods
;
863 residue
= (desc
->periods
- period_idx
) * desc
->period_size
;
864 dma_set_residue(state
, residue
);
867 spin_unlock_irqrestore(&xdma_chan
->vchan
.lock
, flags
);
873 * xdma_channel_isr - XDMA channel interrupt handler
875 * @dev_id: Pointer to the DMA channel structure
877 static irqreturn_t
xdma_channel_isr(int irq
, void *dev_id
)
879 struct xdma_chan
*xchan
= dev_id
;
880 u32 complete_desc_num
= 0;
881 struct xdma_device
*xdev
= xchan
->xdev_hdl
;
882 struct virt_dma_desc
*vd
, *next_vd
;
883 struct xdma_desc
*desc
;
888 spin_lock(&xchan
->vchan
.lock
);
890 if (xchan
->stop_requested
)
891 complete(&xchan
->last_interrupt
);
893 /* get submitted request */
894 vd
= vchan_next_desc(&xchan
->vchan
);
898 /* Clear-on-read the status register */
899 ret
= regmap_read(xdev
->rmap
, xchan
->base
+ XDMA_CHAN_STATUS_RC
, &st
);
903 desc
= to_xdma_desc(vd
);
905 st
&= XDMA_CHAN_STATUS_MASK
;
906 if ((st
& XDMA_CHAN_ERROR_MASK
) ||
907 !(st
& (CHAN_CTRL_IE_DESC_COMPLETED
| CHAN_CTRL_IE_DESC_STOPPED
))) {
909 xdma_err(xdev
, "channel error, status register value: 0x%x", st
);
913 ret
= regmap_read(xdev
->rmap
, xchan
->base
+ XDMA_CHAN_COMPLETED_DESC
,
918 if (desc
->interleaved_dma
) {
920 desc
->completed_desc_num
+= complete_desc_num
;
921 if (complete_desc_num
== XDMA_DESC_BLOCK_NUM
* XDMA_DESC_ADJACENT
) {
922 xdma_xfer_start(xchan
);
926 /* last desc of any frame */
928 if (desc
->frames_left
)
931 /* last desc of the last frame */
932 repeat_tx
= vd
->tx
.flags
& DMA_PREP_REPEAT
;
933 next_vd
= list_first_entry_or_null(&vd
->node
, struct virt_dma_desc
, node
);
935 repeat_tx
= repeat_tx
&& !(next_vd
->tx
.flags
& DMA_PREP_LOAD_EOT
);
937 desc
->frames_left
= desc
->periods
;
938 desc
->completed_desc_num
= 0;
939 vchan_cyclic_callback(vd
);
942 vchan_cookie_complete(vd
);
944 /* start (or continue) the tx of a first desc on the vc.desc_issued list, if any */
945 xdma_xfer_start(xchan
);
946 } else if (!desc
->cyclic
) {
948 desc
->completed_desc_num
+= complete_desc_num
;
950 /* if all data blocks are transferred, remove and complete the request */
951 if (desc
->completed_desc_num
== desc
->desc_num
) {
953 vchan_cookie_complete(vd
);
957 if (desc
->completed_desc_num
> desc
->desc_num
||
958 complete_desc_num
!= XDMA_DESC_BLOCK_NUM
* XDMA_DESC_ADJACENT
)
961 /* transfer the rest of data */
962 xdma_xfer_start(xchan
);
964 desc
->completed_desc_num
= complete_desc_num
;
965 vchan_cyclic_callback(vd
);
969 spin_unlock(&xchan
->vchan
.lock
);
974 * xdma_irq_fini - Uninitialize IRQ
975 * @xdev: DMA device pointer
977 static void xdma_irq_fini(struct xdma_device
*xdev
)
981 /* disable interrupt */
982 regmap_write(xdev
->rmap
, XDMA_IRQ_CHAN_INT_EN_W1C
, ~0);
984 /* free irq handler */
985 for (i
= 0; i
< xdev
->h2c_chan_num
; i
++)
986 free_irq(xdev
->h2c_chans
[i
].irq
, &xdev
->h2c_chans
[i
]);
988 for (i
= 0; i
< xdev
->c2h_chan_num
; i
++)
989 free_irq(xdev
->c2h_chans
[i
].irq
, &xdev
->c2h_chans
[i
]);
993 * xdma_set_vector_reg - configure hardware IRQ registers
994 * @xdev: DMA device pointer
995 * @vec_tbl_start: Start of IRQ registers
996 * @irq_start: Start of IRQ
997 * @irq_num: Number of IRQ
999 static int xdma_set_vector_reg(struct xdma_device
*xdev
, u32 vec_tbl_start
,
1000 u32 irq_start
, u32 irq_num
)
1002 u32 shift
, i
, val
= 0;
1005 /* Each IRQ register is 32 bit and contains 4 IRQs */
1006 while (irq_num
> 0) {
1007 for (i
= 0; i
< 4; i
++) {
1008 shift
= XDMA_IRQ_VEC_SHIFT
* i
;
1009 val
|= irq_start
<< shift
;
1016 /* write IRQ register */
1017 ret
= regmap_write(xdev
->rmap
, vec_tbl_start
, val
);
1020 vec_tbl_start
+= sizeof(u32
);
1028 * xdma_irq_init - initialize IRQs
1029 * @xdev: DMA device pointer
1031 static int xdma_irq_init(struct xdma_device
*xdev
)
1033 u32 irq
= xdev
->irq_start
;
1037 /* return failure if there are not enough IRQs */
1038 if (xdev
->irq_num
< XDMA_CHAN_NUM(xdev
)) {
1039 xdma_err(xdev
, "not enough irq");
1043 /* setup H2C interrupt handler */
1044 for (i
= 0; i
< xdev
->h2c_chan_num
; i
++) {
1045 ret
= request_irq(irq
, xdma_channel_isr
, 0,
1046 "xdma-h2c-channel", &xdev
->h2c_chans
[i
]);
1048 xdma_err(xdev
, "H2C channel%d request irq%d failed: %d",
1050 goto failed_init_h2c
;
1052 xdev
->h2c_chans
[i
].irq
= irq
;
1056 /* setup C2H interrupt handler */
1057 for (j
= 0; j
< xdev
->c2h_chan_num
; j
++) {
1058 ret
= request_irq(irq
, xdma_channel_isr
, 0,
1059 "xdma-c2h-channel", &xdev
->c2h_chans
[j
]);
1061 xdma_err(xdev
, "C2H channel%d request irq%d failed: %d",
1063 goto failed_init_c2h
;
1065 xdev
->c2h_chans
[j
].irq
= irq
;
1069 /* config hardware IRQ registers */
1070 ret
= xdma_set_vector_reg(xdev
, XDMA_IRQ_CHAN_VEC_NUM
, 0,
1071 XDMA_CHAN_NUM(xdev
));
1073 xdma_err(xdev
, "failed to set channel vectors: %d", ret
);
1074 goto failed_init_c2h
;
1077 /* config user IRQ registers if needed */
1078 user_irq_start
= XDMA_CHAN_NUM(xdev
);
1079 if (xdev
->irq_num
> user_irq_start
) {
1080 ret
= xdma_set_vector_reg(xdev
, XDMA_IRQ_USER_VEC_NUM
,
1082 xdev
->irq_num
- user_irq_start
);
1084 xdma_err(xdev
, "failed to set user vectors: %d", ret
);
1085 goto failed_init_c2h
;
1089 /* enable interrupt */
1090 ret
= regmap_write(xdev
->rmap
, XDMA_IRQ_CHAN_INT_EN_W1S
, ~0);
1092 goto failed_init_c2h
;
1098 free_irq(xdev
->c2h_chans
[j
].irq
, &xdev
->c2h_chans
[j
]);
1101 free_irq(xdev
->h2c_chans
[i
].irq
, &xdev
->h2c_chans
[i
]);
1106 static bool xdma_filter_fn(struct dma_chan
*chan
, void *param
)
1108 struct xdma_chan
*xdma_chan
= to_xdma_chan(chan
);
1109 struct xdma_chan_info
*chan_info
= param
;
1111 return chan_info
->dir
== xdma_chan
->dir
;
1115 * xdma_disable_user_irq - Disable user interrupt
1116 * @pdev: Pointer to the platform_device structure
1117 * @irq_num: System IRQ number
1119 void xdma_disable_user_irq(struct platform_device
*pdev
, u32 irq_num
)
1121 struct xdma_device
*xdev
= platform_get_drvdata(pdev
);
1124 index
= irq_num
- xdev
->irq_start
;
1125 if (index
< XDMA_CHAN_NUM(xdev
) || index
>= xdev
->irq_num
) {
1126 xdma_err(xdev
, "invalid user irq number");
1129 index
-= XDMA_CHAN_NUM(xdev
);
1131 regmap_write(xdev
->rmap
, XDMA_IRQ_USER_INT_EN_W1C
, 1 << index
);
1133 EXPORT_SYMBOL(xdma_disable_user_irq
);
1136 * xdma_enable_user_irq - Enable user logic interrupt
1137 * @pdev: Pointer to the platform_device structure
1138 * @irq_num: System IRQ number
1140 int xdma_enable_user_irq(struct platform_device
*pdev
, u32 irq_num
)
1142 struct xdma_device
*xdev
= platform_get_drvdata(pdev
);
1146 index
= irq_num
- xdev
->irq_start
;
1147 if (index
< XDMA_CHAN_NUM(xdev
) || index
>= xdev
->irq_num
) {
1148 xdma_err(xdev
, "invalid user irq number");
1151 index
-= XDMA_CHAN_NUM(xdev
);
1153 ret
= regmap_write(xdev
->rmap
, XDMA_IRQ_USER_INT_EN_W1S
, 1 << index
);
1159 EXPORT_SYMBOL(xdma_enable_user_irq
);
1162 * xdma_get_user_irq - Get system IRQ number
1163 * @pdev: Pointer to the platform_device structure
1164 * @user_irq_index: User logic IRQ wire index
1166 * Return: The system IRQ number allocated for the given wire index.
1168 int xdma_get_user_irq(struct platform_device
*pdev
, u32 user_irq_index
)
1170 struct xdma_device
*xdev
= platform_get_drvdata(pdev
);
1172 if (XDMA_CHAN_NUM(xdev
) + user_irq_index
>= xdev
->irq_num
) {
1173 xdma_err(xdev
, "invalid user irq index");
1177 return xdev
->irq_start
+ XDMA_CHAN_NUM(xdev
) + user_irq_index
;
1179 EXPORT_SYMBOL(xdma_get_user_irq
);
1182 * xdma_remove - Driver remove function
1183 * @pdev: Pointer to the platform_device structure
1185 static void xdma_remove(struct platform_device
*pdev
)
1187 struct xdma_device
*xdev
= platform_get_drvdata(pdev
);
1189 if (xdev
->status
& XDMA_DEV_STATUS_INIT_MSIX
)
1190 xdma_irq_fini(xdev
);
1192 if (xdev
->status
& XDMA_DEV_STATUS_REG_DMA
)
1193 dma_async_device_unregister(&xdev
->dma_dev
);
1197 * xdma_probe - Driver probe function
1198 * @pdev: Pointer to the platform_device structure
1200 static int xdma_probe(struct platform_device
*pdev
)
1202 struct xdma_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
1203 struct xdma_device
*xdev
;
1204 void __iomem
*reg_base
;
1205 struct resource
*res
;
1208 if (pdata
->max_dma_channels
> XDMA_MAX_CHANNELS
) {
1209 dev_err(&pdev
->dev
, "invalid max dma channels %d",
1210 pdata
->max_dma_channels
);
1214 xdev
= devm_kzalloc(&pdev
->dev
, sizeof(*xdev
), GFP_KERNEL
);
1218 platform_set_drvdata(pdev
, xdev
);
1221 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1223 xdma_err(xdev
, "failed to get irq resource");
1226 xdev
->irq_start
= res
->start
;
1227 xdev
->irq_num
= resource_size(res
);
1229 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1231 xdma_err(xdev
, "failed to get io resource");
1235 reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1236 if (IS_ERR(reg_base
)) {
1237 xdma_err(xdev
, "ioremap failed");
1241 xdev
->rmap
= devm_regmap_init_mmio(&pdev
->dev
, reg_base
,
1242 &xdma_regmap_config
);
1244 xdma_err(xdev
, "config regmap failed: %d", ret
);
1247 INIT_LIST_HEAD(&xdev
->dma_dev
.channels
);
1249 ret
= xdma_alloc_channels(xdev
, DMA_MEM_TO_DEV
);
1251 xdma_err(xdev
, "config H2C channels failed: %d", ret
);
1255 ret
= xdma_alloc_channels(xdev
, DMA_DEV_TO_MEM
);
1257 xdma_err(xdev
, "config C2H channels failed: %d", ret
);
1261 dma_cap_set(DMA_SLAVE
, xdev
->dma_dev
.cap_mask
);
1262 dma_cap_set(DMA_PRIVATE
, xdev
->dma_dev
.cap_mask
);
1263 dma_cap_set(DMA_CYCLIC
, xdev
->dma_dev
.cap_mask
);
1264 dma_cap_set(DMA_INTERLEAVE
, xdev
->dma_dev
.cap_mask
);
1265 dma_cap_set(DMA_REPEAT
, xdev
->dma_dev
.cap_mask
);
1266 dma_cap_set(DMA_LOAD_EOT
, xdev
->dma_dev
.cap_mask
);
1268 xdev
->dma_dev
.dev
= &pdev
->dev
;
1269 xdev
->dma_dev
.residue_granularity
= DMA_RESIDUE_GRANULARITY_SEGMENT
;
1270 xdev
->dma_dev
.device_free_chan_resources
= xdma_free_chan_resources
;
1271 xdev
->dma_dev
.device_alloc_chan_resources
= xdma_alloc_chan_resources
;
1272 xdev
->dma_dev
.device_tx_status
= xdma_tx_status
;
1273 xdev
->dma_dev
.device_prep_slave_sg
= xdma_prep_device_sg
;
1274 xdev
->dma_dev
.device_config
= xdma_device_config
;
1275 xdev
->dma_dev
.device_issue_pending
= xdma_issue_pending
;
1276 xdev
->dma_dev
.device_terminate_all
= xdma_terminate_all
;
1277 xdev
->dma_dev
.device_synchronize
= xdma_synchronize
;
1278 xdev
->dma_dev
.filter
.map
= pdata
->device_map
;
1279 xdev
->dma_dev
.filter
.mapcnt
= pdata
->device_map_cnt
;
1280 xdev
->dma_dev
.filter
.fn
= xdma_filter_fn
;
1281 xdev
->dma_dev
.device_prep_dma_cyclic
= xdma_prep_dma_cyclic
;
1282 xdev
->dma_dev
.device_prep_interleaved_dma
= xdma_prep_interleaved_dma
;
1284 ret
= dma_async_device_register(&xdev
->dma_dev
);
1286 xdma_err(xdev
, "failed to register Xilinx XDMA: %d", ret
);
1289 xdev
->status
|= XDMA_DEV_STATUS_REG_DMA
;
1291 ret
= xdma_irq_init(xdev
);
1293 xdma_err(xdev
, "failed to init msix: %d", ret
);
1296 xdev
->status
|= XDMA_DEV_STATUS_INIT_MSIX
;
1306 static const struct platform_device_id xdma_id_table
[] = {
1310 MODULE_DEVICE_TABLE(platform
, xdma_id_table
);
1312 static struct platform_driver xdma_driver
= {
1316 .id_table
= xdma_id_table
,
1317 .probe
= xdma_probe
,
1318 .remove
= xdma_remove
,
1321 module_platform_driver(xdma_driver
);
1323 MODULE_DESCRIPTION("AMD XDMA driver");
1324 MODULE_AUTHOR("XRT Team <runtimeca39d@amd.com>");
1325 MODULE_LICENSE("GPL");