// SPDX-License-Identifier: GPL-2.0-or-later
/*
* DMA driver for Xilinx DMA/Bridge Subsystem
*
* Copyright (C) 2017-2020 Xilinx, Inc. All rights reserved.
* Copyright (C) 2022, Advanced Micro Devices, Inc.
*/
/*
* The DMA/Bridge Subsystem for PCI Express allows for the movement of data
* between Host memory and the DMA subsystem. It does this by operating on
* 'descriptors' that contain information about the source, destination and
* amount of data to transfer. These direct memory transfers can be both in
* the Host to Card (H2C) and Card to Host (C2H) transfers. The DMA can be
* configured to have a single AXI4 Master interface shared by all channels
* or one AXI4-Stream interface for each channel enabled. Memory transfers are
* specified on a per-channel basis in descriptor linked lists, which the DMA
* fetches from host memory and processes. Events such as descriptor completion
* and errors are signaled using interrupts. The core also provides up to 16
* user interrupt wires that generate interrupts to the host.
*/
#include <linux/mod_devicetable.h>
#include <linux/bitfield.h>
#include <linux/dmapool.h>
#include <linux/regmap.h>
#include <linux/dmaengine.h>
#include <linux/dma/amd_xdma.h>
#include <linux/platform_device.h>
#include <linux/platform_data/amd_xdma.h>
#include <linux/dma-mapping.h>
#include <linux/pci.h>
#include "../virt-dma.h"
#include "xdma-regs.h"
/* mmio regmap config for all XDMA registers */
static const struct regmap_config xdma_regmap_config = {
.reg_bits = 32,
.val_bits = 32,
.reg_stride = 4,
.max_register = XDMA_REG_SPACE_LEN,
};
/**
* struct xdma_desc_block - Descriptor block
* @virt_addr: Virtual address of block start
* @dma_addr: DMA address of block start
*/
struct xdma_desc_block {
void *virt_addr;
dma_addr_t dma_addr;
};
/**
* struct xdma_chan - Driver specific DMA channel structure
* @vchan: Virtual channel
* @xdev_hdl: Pointer to DMA device structure
* @base: Offset of channel registers
* @desc_pool: Descriptor pool
* @busy: Busy flag of the channel
* @dir: Transferring direction of the channel
* @cfg: Transferring config of the channel
* @irq: IRQ assigned to the channel
*/
struct xdma_chan {
struct virt_dma_chan vchan;
void *xdev_hdl;
u32 base;
struct dma_pool *desc_pool;
bool busy;
enum dma_transfer_direction dir;
struct dma_slave_config cfg;
u32 irq;
struct completion last_interrupt;
bool stop_requested;
};
/**
* struct xdma_desc - DMA desc structure
* @vdesc: Virtual DMA descriptor
* @chan: DMA channel pointer
* @dir: Transferring direction of the request
* @desc_blocks: Hardware descriptor blocks
* @dblk_num: Number of hardware descriptor blocks
* @desc_num: Number of hardware descriptors
* @completed_desc_num: Completed hardware descriptors
* @cyclic: Cyclic transfer vs. scatter-gather
* @interleaved_dma: Interleaved DMA transfer
* @periods: Number of periods in the cyclic transfer
* @period_size: Size of a period in bytes in cyclic transfers
* @frames_left: Number of frames left in interleaved DMA transfer
* @error: tx error flag
*/
struct xdma_desc {
struct virt_dma_desc vdesc;
struct xdma_chan *chan;
enum dma_transfer_direction dir;
struct xdma_desc_block *desc_blocks;
u32 dblk_num;
u32 desc_num;
u32 completed_desc_num;
bool cyclic;
bool interleaved_dma;
u32 periods;
u32 period_size;
u32 frames_left;
bool error;
};
#define XDMA_DEV_STATUS_REG_DMA BIT(0)
#define XDMA_DEV_STATUS_INIT_MSIX BIT(1)
/**
* struct xdma_device - DMA device structure
* @pdev: Platform device pointer
* @dma_dev: DMA device structure
* @rmap: MMIO regmap for DMA registers
* @h2c_chans: Host to Card channels
* @c2h_chans: Card to Host channels
* @h2c_chan_num: Number of H2C channels
* @c2h_chan_num: Number of C2H channels
* @irq_start: Start IRQ assigned to device
* @irq_num: Number of IRQ assigned to device
* @status: Initialization status
*/
struct xdma_device {
struct platform_device *pdev;
struct dma_device dma_dev;
struct regmap *rmap;
struct xdma_chan *h2c_chans;
struct xdma_chan *c2h_chans;
u32 h2c_chan_num;
u32 c2h_chan_num;
u32 irq_start;
u32 irq_num;
u32 status;
};
#define xdma_err(xdev, fmt, args...) \
dev_err(&(xdev)->pdev->dev, fmt, ##args)
#define XDMA_CHAN_NUM(_xd) ({ \
typeof(_xd) (xd) = (_xd); \
((xd)->h2c_chan_num + (xd