Skip to contentSkip to conversation

xilinx XDMA driver

PATCH Superseded Lizhi Hou 3 participants 12 messages Sep 28, 2022 – Oct 5, 2022

Subsystems (3)

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

v5 cover xilinx XDMA driver

Hello,

This V5 of patch series is to provide the platform driver to support the Xilinx XDMA subsystem. The XDMA subsystem is used in conjunction with the PCI Express IP block to provide high performance data transfer between host memory and the card's DMA subsystem. It also provides up to 16 user interrupt wires to user logic that generate interrupts to the host.

            +-------+       +-------+       +-----------+
   PCIe     |       |       |       |       |           |
   Tx/Rx    |       |       |       |  AXI  |           |
 <=======>  | PCIE  | <===> | XDMA  | <====>| User Logic|
            |       |       |       |       |           |
            +-------+       +-------+       +-----------+

The XDMA has been used for Xilinx Alveo PCIe devices.
And it is also integrated into Versal ACAP DMA and Bridge Subsystem.
https://www.xilinx.com/products/boards-and-kits/alveo.html
https://docs.xilinx.com/r/en-US/pg344-pcie-dma-versal/Introduction-to-the-DMA-and-Bridge-Subsystems

The device driver for any FPGA based PCIe device which leverages XDMA can call the standard dmaengine APIs to discover and use the XDMA subsystem without duplicating the XDMA driver code in its own driver.

Changes since v4:
- Modified user logic interrupt APIs.

Changes since v3:
- Added one patch to support user logic interrupt.

Changes since v2:
- Removed tasklet.
- Fixed regression bug introduced to V2.
- Test Robot warning.

Changes since v1:
- Moved filling hardware descriptor to xdma_prep_device_sg().
- Changed hardware descriptor enum to "struct xdma_hw_desc".
- Minor changes from code review comments.

Lizhi Hou (2):
dmaengine: xilinx: xdma: Add xilinx xdma driver
dmaengine: xilinx: xdma: Add user logic interrupt support

 MAINTAINERS                            |   11 +
 drivers/dma/Kconfig                    |   13 +
 drivers/dma/xilinx/Makefile            |    1 +
 drivers/dma/xilinx/xdma-regs.h         |  171 ++++
 drivers/dma/xilinx/xdma.c              | 1020 ++++++++++++++++++++++++
 include/linux/dma/amd_xdma.h           |   16 +
 include/linux/platform_data/amd_xdma.h |   34 +
 7 files changed, 1266 insertions(+)
 create mode 100644 drivers/dma/xilinx/xdma-regs.h
 create mode 100644 drivers/dma/xilinx/xdma.c
 create mode 100644 include/linux/dma/amd_xdma.h
 create mode 100644 include/linux/platform_data/amd_xdma.h
Signature · 1 line
2.27.0
author maintainer
#1 lore raw

PATCH v5 1/2 dmaengine: xilinx: xdma: Add xilinx xdma driver

Add driver to enable PCIe board which uses XDMA (the DMA/Bridge Subsystem for PCI Express). For example, Xilinx Alveo PCIe devices.
https://www.xilinx.com/products/boards-and-kits/alveo.html

The XDMA engine support up to 4 Host to Card (H2C) and 4 Card to Host (C2H) channels. 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 hardware detail is provided by

    https://docs.xilinx.com/r/en-US/pg195-pcie-dma/Introduction

This driver implements dmaengine APIs.
- probe the available DMA channels
- use dma_slave_map for channel lookup
- use virtual channel to manage dmaengine tx descriptors
- implement device_prep_slave_sg callback to handle host scatter gather list
- implement device_config to config device address for DMA transfer

---
 MAINTAINERS                            |  10 +
 drivers/dma/Kconfig                    |  13 +
 drivers/dma/xilinx/Makefile            |   1 +
 drivers/dma/xilinx/xdma-regs.h         | 171 +++++
 drivers/dma/xilinx/xdma.c              | 955 +++++++++++++++++++++++++
 include/linux/platform_data/amd_xdma.h |  34 +
 6 files changed, 1184 insertions(+)
 create mode 100644 drivers/dma/xilinx/xdma-regs.h
 create mode 100644 drivers/dma/xilinx/xdma.c
 create mode 100644 include/linux/platform_data/amd_xdma.h
MAINTAINERS +10 -0
index e8c52d0192a6..c1be0b2e378a 100644
@@ -21683,6 +21683,16 @@ F:	Documentation/devicetree/bindings/media/xilinx/
 F:	drivers/media/platform/xilinx/
 F:	include/uapi/linux/xilinx-v4l2-controls.h
 
+XILINX XDMA DRIVER
+M:	Lizhi Hou <[email protected]>
+M:	Brian Xu <[email protected]>
+M:	Raj Kumar Rampelli <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/dma/xilinx/xdma-regs.h
+F:	drivers/dma/xilinx/xdma.c
+F:	include/linux/platform_data/amd_xdma.h
+
 XILINX ZYNQMP DPDMA DRIVER
 M:	Hyun Kwon <[email protected]>
 M:	Laurent Pinchart <[email protected]>
drivers/dma/Kconfig +13 -0
index d5de3f77d3aa..4d90f2f51655 100644
@@ -733,6 +733,19 @@ config XILINX_ZYNQMP_DPDMA
 	  driver provides the dmaengine required by the DisplayPort subsystem
 	  display driver.
 
+config XILINX_XDMA
+	tristate "Xilinx DMA/Bridge Subsystem DMA Engine"
+	select DMA_ENGINE
+	select DMA_VIRTUAL_CHANNELS
+	select REGMAP_MMIO
+	help
+	  Enable support for Xilinx DMA/Bridge Subsystem DMA engine. The DMA
+	  provides high performance block data movement between Host memory
+	  and the DMA subsystem. These direct memory transfers can be both in
+	  the Host to Card (H2C) and Card to Host (C2H) transfers.
+	  The core also provides up to 16 user interrupt wires that generate
+	  interrupts to the host.
+
 # driver files
 source "drivers/dma/bestcomm/Kconfig"
 
drivers/dma/xilinx/Makefile +1 -0
index 767bb45f641f..c7a538a56643 100644
@@ -2,3 +2,4 @@
 obj-$(CONFIG_XILINX_DMA) += xilinx_dma.o
 obj-$(CONFIG_XILINX_ZYNQMP_DMA) += zynqmp_dma.o
 obj-$(CONFIG_XILINX_ZYNQMP_DPDMA) += xilinx_dpdma.o
+obj-$(CONFIG_XILINX_XDMA) += xdma.o
drivers/dma/xilinx/xdma-regs.h new file +171 -0

171 lines not on this page · raw message

drivers/dma/xilinx/xdma.c new file +955 -0

955 lines not on this page · raw message

include/linux/platform_data/amd_xdma.h new file +34 -0
new file mode 100644
index 000000000000..b5e23e14bac8
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _PLATDATA_AMD_XDMA_H
+#define _PLATDATA_AMD_XDMA_H
+
+#include <linux/dmaengine.h>
+
+/**
+ * struct xdma_chan_info - DMA channel information
+ *	This information is used to match channel when request dma channel
+ * @dir: Channel transfer direction
+ */
+struct xdma_chan_info {
+	enum dma_transfer_direction dir;
+};
+
+#define XDMA_FILTER_PARAM(chan_info)	((void *)(chan_info))
+
+struct dma_slave_map;
+
+/**
+ * struct xdma_platdata - platform specific data for XDMA engine
+ * @max_dma_channels: Maximum dma channels in each direction
+ */
+struct xdma_platdata {
+	u32 max_dma_channels;
+	u32 device_map_cnt;
+	struct dma_slave_map *device_map;
+};
+
+#endif /* _PLATDATA_AMD_XDMA_H */
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH V5 XDMA 1/2] dmaengine: xilinx: xdma: Add xilinx xdma driver

Quoted 25 lines · Lizhi Hou

Add driver to enable PCIe board which uses XDMA (the DMA/Bridge Subsystem for PCI Express). For example, Xilinx Alveo PCIe devices.
https://www.xilinx.com/products/boards-and-kits/alveo.html

The XDMA engine support up to 4 Host to Card (H2C) and 4 Card to Host (C2H) channels. 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 hardware detail is provided by

    https://docs.xilinx.com/r/en-US/pg195-pcie-dma/Introduction

This driver implements dmaengine APIs.
- probe the available DMA channels
- use dma_slave_map for channel lookup
- use virtual channel to manage dmaengine tx descriptors
- implement device_prep_slave_sg callback to handle host scatter gather list
- implement device_config to config device address for DMA transfer

---

drivers/dma/xilinx/xdma-regs.h line 104

Quoted 9 lines
drivers/dma/xilinx/xdma-regs.h new file +9 -0
+/* bits of the channel control register */
+#define CHAN_CTRL_RUN_STOP			BIT(0)
+#define CHAN_CTRL_IE_DESC_STOPPED		BIT(1)
+#define CHAN_CTRL_IE_DESC_COMPLETED		BIT(2)
+#define CHAN_CTRL_IE_DESC_ALIGN_MISMATCH	BIT(3)
+#define CHAN_CTRL_IE_MAGIC_STOPPED		BIT(4)
+#define CHAN_CTRL_IE_IDLE_STOPPED		BIT(6)
+#define CHAN_CTRL_IE_READ_ERROR			(0x1FUL << 9)
+#define CHAN_CTRL_IE_DESC_ERROR			(0x1FUL << 19)

Looks GENMASK()

drivers/dma/xilinx/xdma-regs.h line 147

Quoted 9 lines
drivers/dma/xilinx/xdma-regs.h new file +9 -0
+/* bits of the SG DMA control register */
+#define XDMA_CTRL_RUN_STOP			BIT(0)
+#define XDMA_CTRL_IE_DESC_STOPPED		BIT(1)
+#define XDMA_CTRL_IE_DESC_COMPLETED		BIT(2)
+#define XDMA_CTRL_IE_DESC_ALIGN_MISMATCH	BIT(3)
+#define XDMA_CTRL_IE_MAGIC_STOPPED		BIT(4)
+#define XDMA_CTRL_IE_IDLE_STOPPED		BIT(6)
+#define XDMA_CTRL_IE_READ_ERROR			(0x1FUL << 9)
+#define XDMA_CTRL_IE_DESC_ERROR			(0x1FUL << 19)

Ditto.

Quoted 50 lines
drivers/dma/xilinx/xdma.c new file +50 -0
+/**
+ * xdma_config_channels - Detect and config DMA channels
+ * @xdev: DMA device pointer
+ * @dir: Channel direction
+ */
+static int xdma_config_channels(struct xdma_device *xdev,
+				enum dma_transfer_direction dir)
+{
+	struct xdma_platdata *pdata = dev_get_platdata(&xdev->pdev->dev);
+	u32 base, identifier, target;
+	struct xdma_chan **chans;
+	u32 *chan_num;
+	int i, j, ret;
+
+	if (dir == DMA_MEM_TO_DEV) {
+		base = XDMA_CHAN_H2C_OFFSET;
+		target = XDMA_CHAN_H2C_TARGET;
+		chans = &xdev->h2c_chans;
+		chan_num = &xdev->h2c_chan_num;
+	} else if (dir == DMA_DEV_TO_MEM) {
+		base = XDMA_CHAN_C2H_OFFSET;
+		target = XDMA_CHAN_C2H_TARGET;
+		chans = &xdev->c2h_chans;
+		chan_num = &xdev->c2h_chan_num;
+	} else {
+		xdma_err(xdev, "invalid direction specified");
+		return -EINVAL;
+	}
+
+	/* detect number of available DMA channels */
+	for (i = 0, *chan_num = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
+				    XDMA_CHAN_IDENTIFIER, &identifier);
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id: %d", ret);
+			return ret;
+		}
+
+		/* check if it is available DMA channel */
+		if (XDMA_CHAN_CHECK_TARGET(identifier, target))
+			(*chan_num)++;
+	}
+
+	if (!*chan_num) {
+		xdma_err(xdev, "does not probe any channel");
+		return -EINVAL;
+	}
+
+	*chans = devm_kzalloc(&xdev->pdev->dev, sizeof(**chans) * (*chan_num),
+			      GFP_KERNEL);

drivers/dma/xilinx/xdma.c line 448

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+	if (!*chans)
+		return -ENOMEM;
+
+	for (i = 0, j = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
+				    XDMA_CHAN_IDENTIFIER, &identifier);
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id: %d", ret);
+			return ret;
+		}

Is it ok to not rollback the allocation in case of an error occurs?

drivers/dma/xilinx/xdma.c line 456

Quoted 8 lines
drivers/dma/xilinx/xdma.c new file +8 -0
+
+		if (!XDMA_CHAN_CHECK_TARGET(identifier, target))
+			continue;
+
+		if (j == *chan_num) {
+			xdma_err(xdev, "invalid channel number");
+			return -EIO;
+		}

Same here?

drivers/dma/xilinx/xdma.c line 465

Quoted 9 lines
drivers/dma/xilinx/xdma.c new file +9 -0
+
+		/* init channel structure and hardware */
+		(*chans)[j].xdev_hdl = xdev;
+		(*chans)[j].base = base + i * XDMA_CHAN_STRIDE;
+		(*chans)[j].dir = dir;
+
+		ret = xdma_channel_init(&(*chans)[j]);
+		if (ret)
+			return ret;

And here.

Quoted 26 lines
(quoted diff) +24 -0
+		(*chans)[j].vchan.desc_free = xdma_free_desc;
+		vchan_init(&(*chans)[j].vchan, &xdev->dma_dev);
+
+		j++;
+	}
+
+	dev_info(&xdev->pdev->dev, "configured %d %s channels", j,
+		 (dir == DMA_MEM_TO_DEV) ? "H2C" : "C2H");
+
+	return 0;
+}
 
 
+static int xdma_irq_init(struct xdma_device *xdev)
+{
+	u32 irq = xdev->irq_start;
+	int i, j, ret;
+
+	/* return failure if there are not enough IRQs */
+	if (xdev->irq_num < xdev->h2c_chan_num + xdev->c2h_chan_num) {
+		xdma_err(xdev, "not enough irq");
+		return -EINVAL;
+	}
+
+	/* setup H2C interrupt handler */
+	for (i = 0; i < xdev->h2c_chan_num; i++) {

Review

Quoted 10 lines
(quoted diff) +10 -0
+		ret = request_irq(irq, xdma_channel_isr, 0,
+				  "xdma-h2c-channel", &xdev->h2c_chans[i]);
+		if (ret) {
+			xdma_err(xdev, "H2C channel%d request irq%d failed: %d",
+				 i, irq, ret);
+			for (j = 0; j < i; j++) {
+				free_irq(xdev->h2c_chans[j].irq,
+					 &xdev->h2c_chans[j]);
+			}
+			return ret;

Remove freeing for loop and just do

			goto failed_init_c2h;
And reverse the iteration order down on the error path (from i to zero) by 
using
	while (i--)
Quoted 7 lines
drivers/dma/xilinx/xdma.c new file +7 -0
+		}
+		xdev->h2c_chans[i].irq = irq;
+		irq++;
+	}
+
+	/* setup C2H interrupt handler */
+	for (i = 0; i < xdev->c2h_chan_num; i++) {

drivers/dma/xilinx/xdma.c line 775

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+		ret = request_irq(irq, xdma_channel_isr, 0,
+				  "xdma-c2h-channel", &xdev->c2h_chans[i]);
+		if (ret) {
+			xdma_err(xdev, "H2C channel%d request irq%d failed: %d",
+				 i, irq, ret);
+			for (j = 0; j < i; j++) {
+				free_irq(xdev->c2h_chans[j].irq,
+					 &xdev->c2h_chans[j]);
+			}
+			goto failed_init_c2h;

Ditto. But use j for this whole for loop so you can just do while (j--) on the error path as the index variable won't clash with the previous loop.

Quoted 14 lines
drivers/dma/xilinx/xdma.c new file +14 -0
+		}
+		xdev->c2h_chans[i].irq = irq;
+		irq++;
+	}
+
+	/* config hardware IRQ registers */
+	ret = xdma_set_vector_reg(xdev, XDMA_IRQ_CHAN_VEC_NUM, 0,
+				  xdev->h2c_chan_num + xdev->c2h_chan_num);
+	if (ret) {
+		xdma_err(xdev, "failed to set channel vectors: %d", ret);
+		goto failed;
+	}
+
+	/* enable interrupt */

drivers/dma/xilinx/xdma.c line 799

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+	ret = xdma_enable_intr(xdev);
+	if (ret) {
+		xdma_err(xdev, "failed to enable interrupts: %d", ret);
+		goto failed;
+	}
+
+	return 0;
+
+failed:
+	for (i = 0; i < xdev->c2h_chan_num; i++)

As mentioned above:

while (j--)

drivers/dma/xilinx/xdma.c line 802

Quoted 3 lines
drivers/dma/xilinx/xdma.c new file +3 -0
+		free_irq(xdev->c2h_chans[i].irq, &xdev->c2h_chans[i]);
+failed_init_c2h:
+	for (i = 0; i < xdev->h2c_chan_num; i++)

while (i--)

Quoted 4 lines
drivers/dma/xilinx/xdma.c new file +4 -0
+		free_irq(xdev->h2c_chans[i].irq, &xdev->h2c_chans[i]);
+
+	return ret;
+}

drivers/dma/xilinx/xdma.c line 816

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+
+static bool xdma_filter_fn(struct dma_chan *chan, void *param)
+{
+	struct xdma_chan *xdma_chan = to_xdma_chan(chan);
+	struct xdma_chan_info *chan_info = param;
+
+	if (chan_info->dir != xdma_chan->dir)
+		return false;
+
+	return true;

return chan_info->dir == xdma_chan->dir;

Signature · 1 line
 i.
author maintainer
#3 lore raw

Re: [PATCH V5 XDMA 1/2] dmaengine: xilinx: xdma: Add xilinx xdma driver

Quoted 21 lines · Ilpo Järvinen
Quoted 19 lines · Lizhi Hou

Add driver to enable PCIe board which uses XDMA (the DMA/Bridge Subsystem for PCI Express). For example, Xilinx Alveo PCIe devices.
https://www.xilinx.com/products/boards-and-kits/alveo.html

The XDMA engine support up to 4 Host to Card (H2C) and 4 Card to Host (C2H) channels. 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 hardware detail is provided by

     https://docs.xilinx.com/r/en-US/pg195-pcie-dma/Introduction

This driver implements dmaengine APIs.
- probe the available DMA channels
- use dma_slave_map for channel lookup
- use virtual channel to manage dmaengine tx descriptors
- implement device_prep_slave_sg callback to handle host scatter gather list
- implement device_config to config device address for DMA transfer

Quoted 15 lines
Quoted 5 lines

---

drivers/dma/xilinx/xdma-regs.h line 104

Quoted 9 lines
drivers/dma/xilinx/xdma-regs.h new file +9 -0
+/* bits of the channel control register */
+#define CHAN_CTRL_RUN_STOP			BIT(0)
+#define CHAN_CTRL_IE_DESC_STOPPED		BIT(1)
+#define CHAN_CTRL_IE_DESC_COMPLETED		BIT(2)
+#define CHAN_CTRL_IE_DESC_ALIGN_MISMATCH	BIT(3)
+#define CHAN_CTRL_IE_MAGIC_STOPPED		BIT(4)
+#define CHAN_CTRL_IE_IDLE_STOPPED		BIT(6)
+#define CHAN_CTRL_IE_READ_ERROR			(0x1FUL << 9)
+#define CHAN_CTRL_IE_DESC_ERROR			(0x1FUL << 19)

Looks GENMASK()

Sure. Thanks for pointing it out.

Quoted 65 lines

drivers/dma/xilinx/xdma-regs.h line 147

Quoted 10 lines
drivers/dma/xilinx/xdma-regs.h new file +9 -0
+/* bits of the SG DMA control register */
+#define XDMA_CTRL_RUN_STOP			BIT(0)
+#define XDMA_CTRL_IE_DESC_STOPPED		BIT(1)
+#define XDMA_CTRL_IE_DESC_COMPLETED		BIT(2)
+#define XDMA_CTRL_IE_DESC_ALIGN_MISMATCH	BIT(3)
+#define XDMA_CTRL_IE_MAGIC_STOPPED		BIT(4)
+#define XDMA_CTRL_IE_IDLE_STOPPED		BIT(6)
+#define XDMA_CTRL_IE_READ_ERROR			(0x1FUL << 9)
+#define XDMA_CTRL_IE_DESC_ERROR			(0x1FUL << 19)

Ditto.

Quoted 53 lines
drivers/dma/xilinx/xdma.c new file +53 -0
+/**
+ * xdma_config_channels - Detect and config DMA channels
+ * @xdev: DMA device pointer
+ * @dir: Channel direction
+ */
+static int xdma_config_channels(struct xdma_device *xdev,
+				enum dma_transfer_direction dir)
+{
+	struct xdma_platdata *pdata = dev_get_platdata(&xdev->pdev->dev);
+	u32 base, identifier, target;
+	struct xdma_chan **chans;
+	u32 *chan_num;
+	int i, j, ret;
+
+	if (dir == DMA_MEM_TO_DEV) {
+		base = XDMA_CHAN_H2C_OFFSET;
+		target = XDMA_CHAN_H2C_TARGET;
+		chans = &xdev->h2c_chans;
+		chan_num = &xdev->h2c_chan_num;
+	} else if (dir == DMA_DEV_TO_MEM) {
+		base = XDMA_CHAN_C2H_OFFSET;
+		target = XDMA_CHAN_C2H_TARGET;
+		chans = &xdev->c2h_chans;
+		chan_num = &xdev->c2h_chan_num;
+	} else {
+		xdma_err(xdev, "invalid direction specified");
+		return -EINVAL;
+	}
+
+	/* detect number of available DMA channels */
+	for (i = 0, *chan_num = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
+				    XDMA_CHAN_IDENTIFIER, &identifier);
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id: %d", ret);
+			return ret;
+		}
+
+		/* check if it is available DMA channel */
+		if (XDMA_CHAN_CHECK_TARGET(identifier, target))
+			(*chan_num)++;
+	}
+
+	if (!*chan_num) {
+		xdma_err(xdev, "does not probe any channel");
+		return -EINVAL;
+	}
+
+	*chans = devm_kzalloc(&xdev->pdev->dev, sizeof(**chans) * (*chan_num),
+			      GFP_KERNEL);
+	if (!*chans)
+		return -ENOMEM;
+
Quoted 8 lines

drivers/dma/xilinx/xdma.c line 448

Quoted 7 lines
drivers/dma/xilinx/xdma.c new file +7 -0
+	for (i = 0, j = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
+				    XDMA_CHAN_IDENTIFIER, &identifier);
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id: %d", ret);
+			return ret;
+		}

Is it ok to not rollback the allocation in case of an error occurs?

In this loop, the failures are returned by read/write registers. The read/write register failure indicates serious hardware issue and the hardware may not be rollback in this situation.

Quoted 58 lines

drivers/dma/xilinx/xdma.c line 456

Quoted 9 lines
drivers/dma/xilinx/xdma.c new file +8 -0
+
+		if (!XDMA_CHAN_CHECK_TARGET(identifier, target))
+			continue;
+
+		if (j == *chan_num) {
+			xdma_err(xdev, "invalid channel number");
+			return -EIO;
+		}

Same here?

drivers/dma/xilinx/xdma.c line 465

Quoted 9 lines
drivers/dma/xilinx/xdma.c new file +9 -0
+
+		/* init channel structure and hardware */
+		(*chans)[j].xdev_hdl = xdev;
+		(*chans)[j].base = base + i * XDMA_CHAN_STRIDE;
+		(*chans)[j].dir = dir;
+
+		ret = xdma_channel_init(&(*chans)[j]);
+		if (ret)
+			return ret;

And here.

Quoted 25 lines
(quoted diff) +24 -0
+		(*chans)[j].vchan.desc_free = xdma_free_desc;
+		vchan_init(&(*chans)[j].vchan, &xdev->dma_dev);
+
+		j++;
+	}
+
+	dev_info(&xdev->pdev->dev, "configured %d %s channels", j,
+		 (dir == DMA_MEM_TO_DEV) ? "H2C" : "C2H");
+
+	return 0;
+}
 
+static int xdma_irq_init(struct xdma_device *xdev)
+{
+	u32 irq = xdev->irq_start;
+	int i, j, ret;
+
+	/* return failure if there are not enough IRQs */
+	if (xdev->irq_num < xdev->h2c_chan_num + xdev->c2h_chan_num) {
+		xdma_err(xdev, "not enough irq");
+		return -EINVAL;
+	}
+
+	/* setup H2C interrupt handler */
+	for (i = 0; i < xdev->h2c_chan_num; i++) {

Review

Quoted 10 lines
(quoted diff) +10 -0
+		ret = request_irq(irq, xdma_channel_isr, 0,
+				  "xdma-h2c-channel", &xdev->h2c_chans[i]);
+		if (ret) {
+			xdma_err(xdev, "H2C channel%d request irq%d failed: %d",
+				 i, irq, ret);
+			for (j = 0; j < i; j++) {
+				free_irq(xdev->h2c_chans[j].irq,
+					 &xdev->h2c_chans[j]);
+			}
+			return ret;

Remove freeing for loop and just do

Quoted 4 lines
			goto failed_init_c2h;
And reverse the iteration order down on the error path (from i to zero) by
using
	while (i--)

Thanks. This looks much cleaner than before. :)

Quoted 54 lines
Quoted 8 lines
drivers/dma/xilinx/xdma.c new file +7 -0
+		}
+		xdev->h2c_chans[i].irq = irq;
+		irq++;
+	}
+
+	/* setup C2H interrupt handler */
+	for (i = 0; i < xdev->c2h_chan_num; i++) {

drivers/dma/xilinx/xdma.c line 775

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+		ret = request_irq(irq, xdma_channel_isr, 0,
+				  "xdma-c2h-channel", &xdev->c2h_chans[i]);
+		if (ret) {
+			xdma_err(xdev, "H2C channel%d request irq%d failed: %d",
+				 i, irq, ret);
+			for (j = 0; j < i; j++) {
+				free_irq(xdev->c2h_chans[j].irq,
+					 &xdev->c2h_chans[j]);
+			}
+			goto failed_init_c2h;

Ditto. But use j for this whole for loop so you can just do while (j--) on the error path as the index variable won't clash with the previous loop.

Quoted 14 lines
drivers/dma/xilinx/xdma.c new file +14 -0
+		}
+		xdev->c2h_chans[i].irq = irq;
+		irq++;
+	}
+
+	/* config hardware IRQ registers */
+	ret = xdma_set_vector_reg(xdev, XDMA_IRQ_CHAN_VEC_NUM, 0,
+				  xdev->h2c_chan_num + xdev->c2h_chan_num);
+	if (ret) {
+		xdma_err(xdev, "failed to set channel vectors: %d", ret);
+		goto failed;
+	}
+
+	/* enable interrupt */

drivers/dma/xilinx/xdma.c line 799

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+	ret = xdma_enable_intr(xdev);
+	if (ret) {
+		xdma_err(xdev, "failed to enable interrupts: %d", ret);
+		goto failed;
+	}
+
+	return 0;
+
+failed:
+	for (i = 0; i < xdev->c2h_chan_num; i++)

As mentioned above:

while (j--)

drivers/dma/xilinx/xdma.c line 802

Quoted 3 lines
drivers/dma/xilinx/xdma.c new file +3 -0
+		free_irq(xdev->c2h_chans[i].irq, &xdev->c2h_chans[i]);
+failed_init_c2h:
+	for (i = 0; i < xdev->h2c_chan_num; i++)

while (i--)

Quoted 15 lines
Quoted 4 lines
drivers/dma/xilinx/xdma.c new file +4 -0
+		free_irq(xdev->h2c_chans[i].irq, &xdev->h2c_chans[i]);
+
+	return ret;
+}

drivers/dma/xilinx/xdma.c line 816

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+
+static bool xdma_filter_fn(struct dma_chan *chan, void *param)
+{
+	struct xdma_chan *xdma_chan = to_xdma_chan(chan);
+	struct xdma_chan_info *chan_info = param;
+
+	if (chan_info->dir != xdma_chan->dir)
+		return false;
+
+	return true;

return chan_info->dir == xdma_chan->dir;

Sure. I will change this.

Thanks,

Lizhi

Quoted 2 lines
#4 lore raw

Re: [PATCH V5 XDMA 1/2] dmaengine: xilinx: xdma: Add xilinx xdma driver

Quoted 45 lines · Lizhi Hou
Quoted 29 lines · Ilpo Järvinen
Quoted 26 lines · Lizhi Hou

Add driver to enable PCIe board which uses XDMA (the DMA/Bridge Subsystem for PCI Express). For example, Xilinx Alveo PCIe devices.
https://www.xilinx.com/products/boards-and-kits/alveo.html

The XDMA engine support up to 4 Host to Card (H2C) and 4 Card to Host (C2H)
channels. 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 hardware detail is provided by

     https://docs.xilinx.com/r/en-US/pg195-pcie-dma/Introduction

This driver implements dmaengine APIs.
- probe the available DMA channels
- use dma_slave_map for channel lookup
- use virtual channel to manage dmaengine tx descriptors
- implement device_prep_slave_sg callback to handle host scatter
gather
list
- implement device_config to config device address for DMA transfer

---

Quoted 13 lines

drivers/dma/xilinx/xdma.c line 448

Quoted 12 lines
drivers/dma/xilinx/xdma.c new file +12 -0
+	*chans = devm_kzalloc(&xdev->pdev->dev, sizeof(**chans) * (*chan_num),
+			      GFP_KERNEL);
+	if (!*chans)
+		return -ENOMEM;
+
+	for (i = 0, j = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
+				    XDMA_CHAN_IDENTIFIER, &identifier);
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id: %d", ret);
+			return ret;
+		}

Is it ok to not rollback the allocation in case of an error occurs?

Quoted 3 lines

In this loop, the failures are returned by read/write registers. The read/write register failure indicates serious hardware issue and the hardware may not be rollback in this situation.

What I meant is that you allocated memory above (to *chans, see above). Shouldn't that memory be free in case the hw is not working before you return the error from this function?

Check also the other returns below for the same problemx.

Quoted 29 lines
Quoted 29 lines

drivers/dma/xilinx/xdma.c line 456

Quoted 5 lines
drivers/dma/xilinx/xdma.c new file +5 -0
+
+		if (j == *chan_num) {
+			xdma_err(xdev, "invalid channel number");
+			return -EIO;
+		}

Same here?

drivers/dma/xilinx/xdma.c line 465

Quoted 9 lines
drivers/dma/xilinx/xdma.c new file +9 -0
+
+		/* init channel structure and hardware */
+		(*chans)[j].xdev_hdl = xdev;
+		(*chans)[j].base = base + i * XDMA_CHAN_STRIDE;
+		(*chans)[j].dir = dir;
+
+		ret = xdma_channel_init(&(*chans)[j]);
+		if (ret)
+			return ret;

And here.

Quoted 11 lines
drivers/dma/xilinx/xdma.c new file +11 -0
+		(*chans)[j].vchan.desc_free = xdma_free_desc;
+		vchan_init(&(*chans)[j].vchan, &xdev->dma_dev);
+
+		j++;
+	}
+
+	dev_info(&xdev->pdev->dev, "configured %d %s channels", j,
+		 (dir == DMA_MEM_TO_DEV) ? "H2C" : "C2H");
+
+	return 0;
+}
Signature · 1 line
 i.
author maintainer
#5 lore raw

Re: [PATCH V5 XDMA 1/2] dmaengine: xilinx: xdma: Add xilinx xdma driver

Quoted 44 lines · Ilpo Järvinen
Quoted 42 lines · Lizhi Hou
Quoted 41 lines · Ilpo Järvinen
Quoted 28 lines · Lizhi Hou

Add driver to enable PCIe board which uses XDMA (the DMA/Bridge Subsystem for PCI Express). For example, Xilinx Alveo PCIe devices.
https://www.xilinx.com/products/boards-and-kits/alveo.html

The XDMA engine support up to 4 Host to Card (H2C) and 4 Card to Host (C2H)
channels. 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 hardware detail is provided by

      https://docs.xilinx.com/r/en-US/pg195-pcie-dma/Introduction

This driver implements dmaengine APIs.
- probe the available DMA channels
- use dma_slave_map for channel lookup
- use virtual channel to manage dmaengine tx descriptors
- implement device_prep_slave_sg callback to handle host scatter
gather
list
- implement device_config to config device address for DMA transfer

---

drivers/dma/xilinx/xdma.c new file +2 -0
+	*chans = devm_kzalloc(&xdev->pdev->dev, sizeof(**chans) * (*chan_num),
+			      GFP_KERNEL);

drivers/dma/xilinx/xdma.c line 448

Quoted 10 lines
drivers/dma/xilinx/xdma.c new file +10 -0
+	if (!*chans)
+		return -ENOMEM;
+
+	for (i = 0, j = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
+				    XDMA_CHAN_IDENTIFIER, &identifier);
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id: %d", ret);
+			return ret;
+		}

Is it ok to not rollback the allocation in case of an error occurs?

Quoted 8 lines
Quoted 3 lines

In this loop, the failures are returned by read/write registers. The read/write register failure indicates serious hardware issue and the hardware may not be rollback in this situation.

What I meant is that you allocated memory above (to *chans, see above). Shouldn't that memory be free in case the hw is not working before you return the error from this function?

Check also the other returns below for the same problemx.

The memory does not need to be freed immediately. And it should not be memory leak because devm_* is used.

Thanks,

Lizhi

Quoted 30 lines
Quoted 29 lines
Quoted 29 lines

drivers/dma/xilinx/xdma.c line 456

Quoted 5 lines
drivers/dma/xilinx/xdma.c new file +5 -0
+
+		if (j == *chan_num) {
+			xdma_err(xdev, "invalid channel number");
+			return -EIO;
+		}

Same here?

drivers/dma/xilinx/xdma.c line 465

Quoted 9 lines
drivers/dma/xilinx/xdma.c new file +9 -0
+
+		/* init channel structure and hardware */
+		(*chans)[j].xdev_hdl = xdev;
+		(*chans)[j].base = base + i * XDMA_CHAN_STRIDE;
+		(*chans)[j].dir = dir;
+
+		ret = xdma_channel_init(&(*chans)[j]);
+		if (ret)
+			return ret;

And here.

Quoted 11 lines
drivers/dma/xilinx/xdma.c new file +11 -0
+		(*chans)[j].vchan.desc_free = xdma_free_desc;
+		vchan_init(&(*chans)[j].vchan, &xdev->dma_dev);
+
+		j++;
+	}
+
+	dev_info(&xdev->pdev->dev, "configured %d %s channels", j,
+		 (dir == DMA_MEM_TO_DEV) ? "H2C" : "C2H");
+
+	return 0;
+}
#6 lore raw

Re: [PATCH V5 XDMA 1/2] dmaengine: xilinx: xdma: Add xilinx xdma driver

Quoted 56 lines · Lizhi Hou
Quoted 54 lines · Ilpo Järvinen
Quoted 52 lines · Lizhi Hou
Quoted 39 lines · Ilpo Järvinen
Quoted 37 lines · Lizhi Hou

Add driver to enable PCIe board which uses XDMA (the DMA/Bridge Subsystem
for PCI Express). For example, Xilinx Alveo PCIe devices.
https://www.xilinx.com/products/boards-and-kits/alveo.html

The XDMA engine support up to 4 Host to Card (H2C) and 4 Card to Host (C2H)
channels. 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 hardware detail is provided by

      https://docs.xilinx.com/r/en-US/pg195-pcie-dma/Introduction

This driver implements dmaengine APIs.
- probe the available DMA channels
- use dma_slave_map for channel lookup
- use virtual channel to manage dmaengine tx descriptors
- implement device_prep_slave_sg callback to handle host scatter
gather
list
- implement device_config to config device address for DMA
transfer

---

(quoted diff) +1 -0
+	*chans = devm_kzalloc(&xdev->pdev->dev, sizeof(**chans) *

(*chan_num),

drivers/dma/xilinx/xdma.c new file +6 -0
+			      GFP_KERNEL);
+	if (!*chans)
+		return -ENOMEM;
+
+	for (i = 0, j = 0; i < pdata->max_dma_channels; i++) {
+		ret = xdma_read_reg(xdev, base + i * XDMA_CHAN_STRIDE,
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +1 -0
+				    XDMA_CHAN_IDENTIFIER,

&identifier);

(quoted diff) +2 -0
+		if (ret) {
+			xdma_err(xdev, "failed to read channel id:

%d", ret);

(quoted diff) +2 -0
+			return ret;
+		}

Is it ok to not rollback the allocation in case of an error occurs?

In this loop, the failures are returned by read/write registers. The read/write register failure indicates serious hardware issue and the hardware
may not be rollback in this situation.

Quoted 8 lines
Quoted 5 lines

What I meant is that you allocated memory above (to *chans, see above). Shouldn't that memory be free in case the hw is not working before you return the error from this function?

Check also the other returns below for the same problemx.

The memory does not need to be freed immediately. And it should not be memory leak because devm_* is used.

Ah, sorry. I think I checked exactly that there wasn't m in it but clearly there is now that I recheck.

Signature · 1 line
 i.
author maintainer
#7 lore raw

PATCH v5 2/2 dmaengine: xilinx: xdma: Add user logic interrupt support

In reply to Lizhi Hou

The Xilinx DMA/Bridge Subsystem for PCIe (XDMA) provides up to 16 user interrupt wires to user logic that generate interrupts to the host. This patch adds APIs to enable/disable user logic interrupt for a given interrupt wire index.

---
 MAINTAINERS                  |  1 +
 drivers/dma/xilinx/xdma.c    | 65 ++++++++++++++++++++++++++++++++++++
 include/linux/dma/amd_xdma.h | 16 +++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 include/linux/dma/amd_xdma.h
MAINTAINERS +1 -0
index c1be0b2e378a..019d84b2b086 100644
@@ -21691,6 +21691,7 @@ L:	[email protected]
 S:	Supported
 F:	drivers/dma/xilinx/xdma-regs.h
 F:	drivers/dma/xilinx/xdma.c
+F:	include/linux/dma/amd_xdma.h
 F:	include/linux/platform_data/amd_xdma.h
 
 XILINX ZYNQMP DPDMA DRIVER
drivers/dma/xilinx/xdma.c +65 -0

89 lines not on this page · raw message

include/linux/dma/amd_xdma.h new file +16 -0
new file mode 100644
index 000000000000..91fb02ff93a7
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _DMAENGINE_AMD_XDMA_H
+#define _DMAENGINE_AMD_XDMA_H
+
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index,
+			 u32 *irq);
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index);
+
+#endif /* _DMAENGINE_AMD_XDMA_H */
Signature · 1 line
2.27.0
#8 lore raw

Re: [PATCH V5 XDMA 2/2] dmaengine: xilinx: xdma: Add user logic interrupt support

Quoted 137 lines · Lizhi Hou

The Xilinx DMA/Bridge Subsystem for PCIe (XDMA) provides up to 16 user interrupt wires to user logic that generate interrupts to the host. This patch adds APIs to enable/disable user logic interrupt for a given interrupt wire index.

---
  MAINTAINERS                  |  1 +
  drivers/dma/xilinx/xdma.c    | 65 ++++++++++++++++++++++++++++++++++++
  include/linux/dma/amd_xdma.h | 16 +++++++++
  3 files changed, 82 insertions(+)

create mode 100644 include/linux/dma/amd_xdma.h

MAINTAINERS +1 -0
index c1be0b2e378a..019d84b2b086 100644
@@ -21691,6 +21691,7 @@ L:	[email protected]
  S:	Supported
  F:	drivers/dma/xilinx/xdma-regs.h
  F:	drivers/dma/xilinx/xdma.c
+F:	include/linux/dma/amd_xdma.h
  F:	include/linux/platform_data/amd_xdma.h
 
  XILINX ZYNQMP DPDMA DRIVER
drivers/dma/xilinx/xdma.c +65 -0
index 58a57e03bef5..13f627445c4a 100644
@@ -25,6 +25,7 @@
  #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>
@@ -736,6 +737,7 @@ static int xdma_set_vector_reg(struct xdma_device *xdev, u32 vec_tbl_start,
  static int xdma_irq_init(struct xdma_device *xdev)
  {
  	u32 irq = xdev->irq_start;
+	u32 user_irq_start;
  	int i, j, ret;
 
  	/* return failure if there are not enough IRQs */
@@ -786,6 +788,18 @@ static int xdma_irq_init(struct xdma_device *xdev)
  		goto failed;
  	}
 
+	/* config user IRQ registers if needed */
+	user_irq_start = xdev->h2c_chan_num + xdev->c2h_chan_num;
+	if (xdev->irq_num > user_irq_start) {
+		ret = xdma_set_vector_reg(xdev, XDMA_IRQ_USER_VEC_NUM,
+					  user_irq_start,
+					  xdev->irq_num - user_irq_start);
+		if (ret) {
+			xdma_err(xdev, "failed to set user vectors: %d", ret);
+			goto failed;
+		}
+	}
+
  	/* enable interrupt */
  	ret = xdma_enable_intr(xdev);
  	if (ret) {
@@ -816,6 +830,57 @@ static bool xdma_filter_fn(struct dma_chan *chan, void *param)
  	return true;
  }
 
+/**
+ * xdma_disable_user_irq - Disable user interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User IRQ index
+ */
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index)
+{
+	struct xdma_device *xdev = platform_get_drvdata(pdev);
+
+	if (xdev->h2c_chan_num + xdev->c2h_chan_num + user_irq_index >=
+	    xdev->irq_num) {
+		xdma_err(xdev, "invalid user irq index");
+		return;
+	}
+
+	xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1C,
+		       (1 << user_irq_index));
+}
+EXPORT_SYMBOL(xdma_disable_user_irq);
+
+/**
+ * xdma_enable_user_irq - Enable user logic interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User logic IRQ wire index
+ * @irq: Pointer to returning system IRQ number
+ */
+int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index,
+			 u32 *irq)
+{
+	struct xdma_device *xdev = platform_get_drvdata(pdev);
+	u32 user_irq;
+	int ret;
+
+	user_irq = xdev->h2c_chan_num + xdev->c2h_chan_num + user_irq_index;
+	if (user_irq >= xdev->irq_num) {
+		xdma_err(xdev, "invalid user irq index");
+		return -EINVAL;
+	}
+
+	ret = xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1S,
+			     (1 << user_irq_index));
+	if (ret) {
+		xdma_err(xdev, "set user irq mask failed, %d", ret);
+		return ret;
+	}
+	*irq = user_irq + xdev->irq_start;
+
+	return 0;
+}
+EXPORT_SYMBOL(xdma_enable_user_irq);
+
  /**
   * xdma_remove - Driver remove function
   * @pdev: Pointer to the platform_device structure
include/linux/dma/amd_xdma.h new file +6 -0
new file mode 100644
index 000000000000..91fb02ff93a7
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _DMAENGINE_AMD_XDMA_H

include/linux/dma/amd_xdma.h line 16

Quoted 10 lines
include/linux/dma/amd_xdma.h new file +10 -0
+#define _DMAENGINE_AMD_XDMA_H
+
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index,
+			 u32 *irq);
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index);
+
+#endif /* _DMAENGINE_AMD_XDMA_H */

Hi,
While rewriting our V4L2 driver to use your XDMA driver, I realized, that the API for the user interrupts is still not fully usable. If
the expected outcome is that the "parent" driver using the xdma driver knows nothing about the IRQ allocation in XDMA, then the xdma_enable_user_irq() function must be split into two separate functions:

int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index)

and

int xdma_get_system_irq(u32 user_irq_index)

that returns the system IRQ number for the given user_irq_index.
Because without it you can not get the system IRQ number without enabling the irq at the same time which is usually not what you want.

As a workaround, you can compute the system IRQ yourself with the knowledge of the XDMA internals (this is what I have to do at the moment in our driver), but then the "u32 *irq" parameter becomes useless and can be removed anyway.

M.

author maintainer
#9 lore raw

Re: [PATCH V5 XDMA 2/2] dmaengine: xilinx: xdma: Add user logic interrupt support

Quoted 156 lines · Martin Tůma
Quoted 148 lines · Lizhi Hou

The Xilinx DMA/Bridge Subsystem for PCIe (XDMA) provides up to 16 user interrupt wires to user logic that generate interrupts to the host. This patch adds APIs to enable/disable user logic interrupt for a given interrupt wire index.

---
  MAINTAINERS                  |  1 + drivers/dma/xilinx/xdma.c    | 65 ++++++++++++++++++++++++++++++++++++ include/linux/dma/amd_xdma.h | 16 +++++++++

  3 files changed, 82 insertions(+)

  create mode 100644 include/linux/dma/amd_xdma.h

MAINTAINERS +0 -0
index c1be0b2e378a..019d84b2b086 100644
@@ -21691,6 +21691,7 @@ L:    [email protected]

  S:    Supported
  F:    drivers/dma/xilinx/xdma-regs.h
  F:    drivers/dma/xilinx/xdma.c
+F:    include/linux/dma/amd_xdma.h
  F:    include/linux/platform_data/amd_xdma.h
    XILINX ZYNQMP DPDMA DRIVER

drivers/dma/xilinx/xdma.c +2 -0
index 58a57e03bef5..13f627445c4a 100644
@@ -25,6 +25,7 @@
   #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>
@@ -736,6 +737,7 @@ static int xdma_set_vector_reg(struct xdma_device 
 *xdev, u32 vec_tbl_start,
   static int xdma_irq_init(struct xdma_device *xdev)
   {
       u32 irq = xdev->irq_start;
+    u32 user_irq_start;
       int i, j, ret;
         /* return failure if there are not enough IRQs */
@@ -786,6 +788,18 @@ static int xdma_irq_init(struct xdma_device *xdev)
           goto failed;
       }

  +    /* config user IRQ registers if needed */
+    user_irq_start = xdev->h2c_chan_num + xdev->c2h_chan_num; +    if (xdev->irq_num > user_irq_start) {
+        ret = xdma_set_vector_reg(xdev, XDMA_IRQ_USER_VEC_NUM, +                      user_irq_start,
+                      xdev->irq_num - user_irq_start); +        if (ret) {
+            xdma_err(xdev, "failed to set user vectors: %d", ret); +            goto failed;
+        }
+    }
+
      /* enable interrupt */
      ret = xdma_enable_intr(xdev);
      if (ret) {

drivers/dma/xilinx/xdma.c +0 -0
@@ -816,6 +830,57 @@ static bool xdma_filter_fn(struct dma_chan 
 *chan, void *param)
       return true;
   }

  +/**
+ * xdma_disable_user_irq - Disable user interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User IRQ index
+ */
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index)

drivers/dma/xilinx/xdma.c +21 -0
+{
+    struct xdma_device *xdev = platform_get_drvdata(pdev);
+
+    if (xdev->h2c_chan_num + xdev->c2h_chan_num + user_irq_index >=
+        xdev->irq_num) {
+        xdma_err(xdev, "invalid user irq index");
+        return;
+    }
+
+    xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1C,
+               (1 << user_irq_index));
+}
+EXPORT_SYMBOL(xdma_disable_user_irq);
+
+/**
+ * xdma_enable_user_irq - Enable user logic interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User logic IRQ wire index
+ * @irq: Pointer to returning system IRQ number
+ */
+int xdma_enable_user_irq(struct platform_device *pdev, u32 

user_irq_index,
+             u32 *irq)

drivers/dma/xilinx/xdma.c +6 -0
+{
+    struct xdma_device *xdev = platform_get_drvdata(pdev);
+    u32 user_irq;
+    int ret;
+
+    user_irq = xdev->h2c_chan_num + xdev->c2h_chan_num + 

user_irq_index;
+    if (user_irq >= xdev->irq_num) {
+        xdma_err(xdev, "invalid user irq index"); +        return -EINVAL;
+    }

drivers/dma/xilinx/xdma.c +13 -0
+
+    ret = xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1S,
+                 (1 << user_irq_index));
+    if (ret) {
+        xdma_err(xdev, "set user irq mask failed, %d", ret);
+        return ret;
+    }
+    *irq = user_irq + xdev->irq_start;
+
+    return 0;
+}
+EXPORT_SYMBOL(xdma_enable_user_irq);
+

  /**
   * xdma_remove - Driver remove function
   * @pdev: Pointer to the platform_device structure

include/linux/dma/amd_xdma.h new file +12 -0
new file mode 100644
index 000000000000..91fb02ff93a7
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _DMAENGINE_AMD_XDMA_H
+#define _DMAENGINE_AMD_XDMA_H
+
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+int xdma_enable_user_irq(struct platform_device *pdev, u32 

user_irq_index,
+             u32 *irq);
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index);

include/linux/dma/amd_xdma.h

Quoted 2 lines
include/linux/dma/amd_xdma.h new file +2 -0
+
+#endif /* _DMAENGINE_AMD_XDMA_H */

Hi,
While rewriting our V4L2 driver to use your XDMA driver, I realized, that the API for the user interrupts is still not fully usable. If
the expected outcome is that the "parent" driver using the xdma driver knows nothing about the IRQ allocation in XDMA, then the xdma_enable_user_irq() function must be split into two separate functions:

Quoted 8 lines

int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index)

and

int xdma_get_system_irq(u32 user_irq_index)

that returns the system IRQ number for the given user_irq_index.
Because without it you can not get the system IRQ number without enabling the irq at the same time which is usually not what you want.

Is this because the user logic you are using does not have disable/enable interrupt by itself?  I thought that the user logic IP would have its own register to enable/disable interrupt.

And xdma_enable_user_irq() will only be called once for creating the user logic platform device. Then, user logic IP driver would use its own register to control interrupts. The existing platform driver does not call any xdma_* to control interrupt.

Thanks,

Lizhi

Quoted 4 lines

As a workaround, you can compute the system IRQ yourself with the knowledge of the XDMA internals (this is what I have to do at the moment in our driver), but then the "u32 *irq" parameter becomes useless and can be removed anyway.

M.

#10 lore raw

Re: [PATCH V5 XDMA 2/2] dmaengine: xilinx: xdma: Add user logic interrupt support

Quoted 162 lines · Lizhi Hou
Quoted 160 lines · Martin Tůma
Quoted 150 lines · Lizhi Hou

The Xilinx DMA/Bridge Subsystem for PCIe (XDMA) provides up to 16 user interrupt wires to user logic that generate interrupts to the host. This patch adds APIs to enable/disable user logic interrupt for a given interrupt wire index.

---
  MAINTAINERS                  |  1 +
  drivers/dma/xilinx/xdma.c    | 65
++++++++++++++++++++++++++++++++++++
  include/linux/dma/amd_xdma.h | 16 +++++++++

  3 files changed, 82 insertions(+)

  create mode 100644 include/linux/dma/amd_xdma.h

MAINTAINERS +0 -0
index c1be0b2e378a..019d84b2b086 100644
@@ -21691,6 +21691,7 @@ L:    [email protected]

  S:    Supported
  F:    drivers/dma/xilinx/xdma-regs.h
  F:    drivers/dma/xilinx/xdma.c
+F:    include/linux/dma/amd_xdma.h
  F:    include/linux/platform_data/amd_xdma.h
    XILINX ZYNQMP DPDMA DRIVER

drivers/dma/xilinx/xdma.c +2 -0
index 58a57e03bef5..13f627445c4a 100644
@@ -25,6 +25,7 @@
   #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>
@@ -736,6 +737,7 @@ static int xdma_set_vector_reg(struct 
 xdma_device *xdev, u32 vec_tbl_start,
   static int xdma_irq_init(struct xdma_device *xdev)
   {
       u32 irq = xdev->irq_start;
+    u32 user_irq_start;
       int i, j, ret;
         /* return failure if there are not enough IRQs */
@@ -786,6 +788,18 @@ static int xdma_irq_init(struct xdma_device *xdev)
           goto failed;
       }

  +    /* config user IRQ registers if needed */
+    user_irq_start = xdev->h2c_chan_num + xdev->c2h_chan_num; +    if (xdev->irq_num > user_irq_start) {
+        ret = xdma_set_vector_reg(xdev, XDMA_IRQ_USER_VEC_NUM, +                      user_irq_start,
+                      xdev->irq_num - user_irq_start); +        if (ret) {
+            xdma_err(xdev, "failed to set user vectors: %d", ret); +            goto failed;
+        }
+    }
+
      /* enable interrupt */
      ret = xdma_enable_intr(xdev);
      if (ret) {

drivers/dma/xilinx/xdma.c +0 -0
@@ -816,6 +830,57 @@ static bool xdma_filter_fn(struct dma_chan 
 *chan, void *param)
       return true;
   }

  +/**
+ * xdma_disable_user_irq - Disable user interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User IRQ index
+ */
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index)

drivers/dma/xilinx/xdma.c +21 -0
+{
+    struct xdma_device *xdev = platform_get_drvdata(pdev);
+
+    if (xdev->h2c_chan_num + xdev->c2h_chan_num + user_irq_index >=
+        xdev->irq_num) {
+        xdma_err(xdev, "invalid user irq index");
+        return;
+    }
+
+    xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1C,
+               (1 << user_irq_index));
+}
+EXPORT_SYMBOL(xdma_disable_user_irq);
+
+/**
+ * xdma_enable_user_irq - Enable user logic interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User logic IRQ wire index
+ * @irq: Pointer to returning system IRQ number
+ */
+int xdma_enable_user_irq(struct platform_device *pdev, u32 

user_irq_index,
+             u32 *irq)

drivers/dma/xilinx/xdma.c +6 -0
+{
+    struct xdma_device *xdev = platform_get_drvdata(pdev);
+    u32 user_irq;
+    int ret;
+
+    user_irq = xdev->h2c_chan_num + xdev->c2h_chan_num + 

user_irq_index;
+    if (user_irq >= xdev->irq_num) {
+        xdma_err(xdev, "invalid user irq index"); +        return -EINVAL;
+    }
+
+    ret = xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1S,
+                 (1 << user_irq_index));
+    if (ret) {
+        xdma_err(xdev, "set user irq mask failed, %d", ret); +        return ret;
+    }
+    *irq = user_irq + xdev->irq_start;

drivers/dma/xilinx/xdma.c +5 -0
+
+    return 0;
+}
+EXPORT_SYMBOL(xdma_enable_user_irq);
+

  /**
   * xdma_remove - Driver remove function
   * @pdev: Pointer to the platform_device structure

include/linux/dma/amd_xdma.h new file +12 -0
new file mode 100644
index 000000000000..91fb02ff93a7
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _DMAENGINE_AMD_XDMA_H
+#define _DMAENGINE_AMD_XDMA_H
+
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+int xdma_enable_user_irq(struct platform_device *pdev, u32 

user_irq_index,
+             u32 *irq);
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index);

include/linux/dma/amd_xdma.h

Quoted 2 lines
include/linux/dma/amd_xdma.h new file +2 -0
+
+#endif /* _DMAENGINE_AMD_XDMA_H */

Hi,
While rewriting our V4L2 driver to use your XDMA driver, I realized, that the API for the user interrupts is still not fully usable. If
the expected outcome is that the "parent" driver using the xdma driver knows nothing about the IRQ allocation in XDMA, then the xdma_enable_user_irq() function must be split into two separate functions:

int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index)

Quoted 9 lines
Quoted 6 lines

and

int xdma_get_system_irq(u32 user_irq_index)

that returns the system IRQ number for the given user_irq_index.
Because without it you can not get the system IRQ number without enabling the irq at the same time which is usually not what you want.

Is this because the user logic you are using does not have disable/enable interrupt by itself?  I thought that the user logic IP would have its own register to enable/disable interrupt.

Yes. Our HW has no "own" registers to disable the IRQs. As such registers are already there in XDMA, there was no need for them.

Quoted 2 lines

And xdma_enable_user_irq() will only be called once for creating the user logic platform device. Then, user logic IP driver would use its own register to control interrupts. The existing platform driver does not call any xdma_* to control interrupt.

As you can see, there is HW that was designed to use the XDMA registers during normal operation and separate enable/disable and irq mapping functions are required. Maybe it is not the best HW design, but it can evidently happen and then the driver author is screwed with the merged API.

M.

Quoted 9 lines

Thanks,

Lizhi

Quoted 4 lines

As a workaround, you can compute the system IRQ yourself with the knowledge of the XDMA internals (this is what I have to do at the moment in our driver), but then the "u32 *irq" parameter becomes useless and can be removed anyway.

M.

author maintainer
#11 lore raw

Re: [PATCH V5 XDMA 2/2] dmaengine: xilinx: xdma: Add user logic interrupt support

Quoted 172 lines · Martin Tůma
Quoted 170 lines · Lizhi Hou
Quoted 167 lines · Martin Tůma
Quoted 151 lines · Lizhi Hou

The Xilinx DMA/Bridge Subsystem for PCIe (XDMA) provides up to 16 user interrupt wires to user logic that generate interrupts to the host. This patch adds APIs to enable/disable user logic interrupt for a given interrupt wire index.

---
  MAINTAINERS                  |  1 +
  drivers/dma/xilinx/xdma.c    | 65
++++++++++++++++++++++++++++++++++++
  include/linux/dma/amd_xdma.h | 16 +++++++++

  3 files changed, 82 insertions(+)

  create mode 100644 include/linux/dma/amd_xdma.h

MAINTAINERS +0 -0
index c1be0b2e378a..019d84b2b086 100644
@@ -21691,6 +21691,7 @@ L:    [email protected]

  S:    Supported
  F:    drivers/dma/xilinx/xdma-regs.h
  F:    drivers/dma/xilinx/xdma.c
+F:    include/linux/dma/amd_xdma.h
  F:    include/linux/platform_data/amd_xdma.h
    XILINX ZYNQMP DPDMA DRIVER

drivers/dma/xilinx/xdma.c +2 -0
index 58a57e03bef5..13f627445c4a 100644
@@ -25,6 +25,7 @@
   #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>
@@ -736,6 +737,7 @@ static int xdma_set_vector_reg(struct 
 xdma_device *xdev, u32 vec_tbl_start,
   static int xdma_irq_init(struct xdma_device *xdev)
   {
       u32 irq = xdev->irq_start;
+    u32 user_irq_start;
       int i, j, ret;
         /* return failure if there are not enough IRQs */
@@ -786,6 +788,18 @@ static int xdma_irq_init(struct xdma_device 
 *xdev)
           goto failed;
       }

  +    /* config user IRQ registers if needed */
+    user_irq_start = xdev->h2c_chan_num + xdev->c2h_chan_num; +    if (xdev->irq_num > user_irq_start) {
+        ret = xdma_set_vector_reg(xdev, XDMA_IRQ_USER_VEC_NUM, +                      user_irq_start,
+                      xdev->irq_num - user_irq_start); +        if (ret) {
+            xdma_err(xdev, "failed to set user vectors: %d", ret); +            goto failed;
+        }
+    }
+
      /* enable interrupt */
      ret = xdma_enable_intr(xdev);
      if (ret) {

drivers/dma/xilinx/xdma.c +0 -0
@@ -816,6 +830,57 @@ static bool xdma_filter_fn(struct dma_chan 
 *chan, void *param)
       return true;
   }

  +/**
+ * xdma_disable_user_irq - Disable user interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User IRQ index
+ */
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index)

drivers/dma/xilinx/xdma.c +21 -0
+{
+    struct xdma_device *xdev = platform_get_drvdata(pdev);
+
+    if (xdev->h2c_chan_num + xdev->c2h_chan_num + user_irq_index >=
+        xdev->irq_num) {
+        xdma_err(xdev, "invalid user irq index");
+        return;
+    }
+
+    xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1C,
+               (1 << user_irq_index));
+}
+EXPORT_SYMBOL(xdma_disable_user_irq);
+
+/**
+ * xdma_enable_user_irq - Enable user logic interrupt
+ * @pdev: Pointer to the platform_device structure
+ * @user_irq_index: User logic IRQ wire index
+ * @irq: Pointer to returning system IRQ number
+ */
+int xdma_enable_user_irq(struct platform_device *pdev, u32 

user_irq_index,
+             u32 *irq)

drivers/dma/xilinx/xdma.c +6 -0
+{
+    struct xdma_device *xdev = platform_get_drvdata(pdev);
+    u32 user_irq;
+    int ret;
+
+    user_irq = xdev->h2c_chan_num + xdev->c2h_chan_num + 

user_irq_index;
+    if (user_irq >= xdev->irq_num) {
+        xdma_err(xdev, "invalid user irq index"); +        return -EINVAL;
+    }
+
+    ret = xdma_write_reg(xdev, XDMA_IRQ_BASE, XDMA_IRQ_USER_INT_EN_W1S,
+                 (1 << user_irq_index));
+    if (ret) {
+        xdma_err(xdev, "set user irq mask failed, %d", ret); +        return ret;
+    }
+    *irq = user_irq + xdev->irq_start;

drivers/dma/xilinx/xdma.c +5 -0
+
+    return 0;
+}
+EXPORT_SYMBOL(xdma_enable_user_irq);
+

  /**
   * xdma_remove - Driver remove function
   * @pdev: Pointer to the platform_device structure

include/linux/dma/amd_xdma.h new file +12 -0
new file mode 100644
index 000000000000..91fb02ff93a7
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2022, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _DMAENGINE_AMD_XDMA_H
+#define _DMAENGINE_AMD_XDMA_H
+
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+
+int xdma_enable_user_irq(struct platform_device *pdev, u32 

user_irq_index,
+             u32 *irq);
+void xdma_disable_user_irq(struct platform_device *pdev, u32 user_irq_index);

include/linux/dma/amd_xdma.h

Quoted 2 lines
include/linux/dma/amd_xdma.h new file +2 -0
+
+#endif /* _DMAENGINE_AMD_XDMA_H */

Hi,
While rewriting our V4L2 driver to use your XDMA driver, I realized, that the API for the user interrupts is still not fully usable. If
the expected outcome is that the "parent" driver using the xdma driver knows nothing about the IRQ allocation in XDMA, then the xdma_enable_user_irq() function must be split into two separate functions:

int xdma_enable_user_irq(struct platform_device *pdev, u32 user_irq_index)

and

int xdma_get_system_irq(u32 user_irq_index)

that returns the system IRQ number for the given user_irq_index.
Because without it you can not get the system IRQ number without enabling the irq at the same time which is usually not what you want.

Quoted 8 lines
Quoted 2 lines

Is this because the user logic you are using does not have disable/enable interrupt by itself?  I thought that the user logic IP would have its own register to enable/disable interrupt.

Yes. Our HW has no "own" registers to disable the IRQs. As such registers are already there in XDMA, there was no need for them.

Quoted 2 lines

And xdma_enable_user_irq() will only be called once for creating the user logic platform device. Then, user logic IP driver would use its own register to control interrupts. The existing platform driver does not call any xdma_* to control interrupt.

As you can see, there is HW that was designed to use the XDMA registers during normal operation and separate enable/disable and irq mapping functions are required. Maybe it is not the best HW design, but it can evidently happen and then the driver author is screwed with the merged API.

Ok. To support this case, I will separate the API as you mentioned.

Thanks,

Lizhi

Quoted 12 lines

M.

Quoted 9 lines

Thanks,

Lizhi

Quoted 4 lines

As a workaround, you can compute the system IRQ yourself with the knowledge of the XDMA internals (this is what I have to do at the moment in our driver), but then the "u32 *irq" parameter becomes useless and can be removed anyway.

M.