Skip to contentSkip to conversation

fpga: add dma-buf based FPGA programming interface

PATCH In review Aravind Thokala 3 participants 8 messages Aug 16 – Aug 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover fpga: add dma-buf based FPGA programming interface

Some systems need to load large FPGA configuration images. The FPGA subsystem allows loading images from the filesystem, but this requires the entire image to be loaded into kernel memory first. For drivers that need a DMA-capable buffer for programming, the data is then copied again into DMA memory. This creates needless memory pressure and delays due to the extra copy.

This series adds dma-buf support that allows userspace to allocate a buffer directly from a DMA heap, write the FPGA image into it, and pass the file descriptor to the kernel via ioctl — skipping the intermediate kernel buffer entirely.

Userspace flow:
1. Allocate buffer from /dev/dma_heap/ (e.g., CMA heap)
2. mmap the buffer and write the FPGA image into it
3. ioctl(/dev/fpgaX, FPGA_IOCTL_LOAD_DMA_BUF, &dmabuf_fd)

The dma-buf logic is implemented as a separate layer on top of the FPGA manager, keeping buffer management separate from the write path. Individual FPGA drivers opt in by calling fpga_dmabuf_register().
---
This work is based on the approach discussed in [1].

[1] https://lore.kernel.org/all/[email protected]/
---
Aravind Thokala (2):
fpga: Add dma-buf interface for FPGA programming
fpga: versal: add dma-buf programming support

 .../userspace-api/ioctl/ioctl-number.rst      |   1 +
 MAINTAINERS                                   |   1 +
 drivers/fpga/Kconfig                          |   9 +
 drivers/fpga/Makefile                         |   2 +
 drivers/fpga/fpga-dmabuf.c                    | 198 ++++++++++++++++++
 drivers/fpga/versal-fpga.c                    |  33 ++-
 include/linux/fpga/fpga-dmabuf.h              |  22 ++
 include/linux/fpga/fpga-mgr.h                 |   1 +
 include/uapi/linux/fpga.h                     |  15 ++
 9 files changed, 281 insertions(+), 1 deletion(-)
 create mode 100644 drivers/fpga/fpga-dmabuf.c
 create mode 100644 include/linux/fpga/fpga-dmabuf.h
 create mode 100644 include/uapi/linux/fpga.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 fpga: Add dma-buf interface for FPGA programming

Loading FPGA images through the firmware interface requires the image to be loaded into kernel memory first and then copied into DMA memory. This extra copy creates needless memory pressure and delays.

Add a module that allows userspace to pass a pre-allocated DMA buffer directly to the FPGA manager via ioctl, eliminating the extra copy. A miscdevice (/dev/fpgaX) is created for each FPGA manager that registers dma-buf support. The module imports the dma-buf fd, maps it for DMA, and calls the FPGA manager load API with the scatter-gather table.

---
 .../userspace-api/ioctl/ioctl-number.rst      |   1 +
 MAINTAINERS                                   |   1 +
 drivers/fpga/Kconfig                          |   9 +
 drivers/fpga/Makefile                         |   2 +
 drivers/fpga/fpga-dmabuf.c                    | 198 ++++++++++++++++++
 include/linux/fpga/fpga-dmabuf.h              |  22 ++
 include/linux/fpga/fpga-mgr.h                 |   1 +
 include/uapi/linux/fpga.h                     |  15 ++
 8 files changed, 249 insertions(+)
 create mode 100644 drivers/fpga/fpga-dmabuf.c
 create mode 100644 include/linux/fpga/fpga-dmabuf.h
 create mode 100644 include/uapi/linux/fpga.h
Documentation/userspace-api/ioctl/ioctl-number.rst +1 -0
index 3f0ef1e27eb0..f8537dd23463 100644
@@ -170,6 +170,7 @@ Code  Seq#    Include File                                             Comments
 'I'   all    linux/isdn.h                                              conflict!
 'I'   00-0F  drivers/isdn/divert/isdn_divert.h                         conflict!
 'I'   40-4F  linux/mISDNif.h                                           conflict!
+'J'   01     uapi/linux/fpga.h                                         FPGA DMA buffer interface
 'K'   all    linux/kd.h
 'L'   00-1F  linux/loop.h                                              conflict!
 'L'   10-1F  drivers/scsi/mpt3sas/mpt3sas_ctl.h                        conflict!
MAINTAINERS +1 -0
index 5114e6db7307..7551dfa35d1c 100644
@@ -10217,6 +10217,7 @@ F:	Documentation/driver-api/fpga/
 F:	Documentation/fpga/
 F:	drivers/fpga/
 F:	include/linux/fpga/
+F:	include/uapi/linux/fpga.h
 
 FRAMEBUFFER CONSOLE
 M:	Helge Deller <[email protected]>
drivers/fpga/Kconfig +9 -0
index 748fc210c135..e3ad791c9808 100644
@@ -157,6 +157,15 @@ config OF_FPGA_REGION
 	  Support for loading FPGA images by applying a Device Tree
 	  overlay.
 
+config FPGA_DMA_BUF
+	tristate "FPGA DMA Buffer Support"
+	depends on DMA_SHARED_BUFFER
+	help
+	  Support for programming FPGAs from a pre-allocated DMA buffer
+	  shared via dma-buf. A miscdevice (/dev/fpgaX) accepts a dma-buf
+	  file descriptor via ioctl to load the FPGA image without
+	  redundant memory copies.
+
 config FPGA_DFL
 	tristate "FPGA Device Feature List (DFL) support"
 	select FPGA_BRIDGE
drivers/fpga/Makefile +2 -0
index 6f5798b27e0d..e52154296d2d 100644
@@ -40,6 +40,8 @@ obj-$(CONFIG_XILINX_PR_DECOUPLER)	+= xilinx-pr-decoupler.o
 # High Level Interfaces
 obj-$(CONFIG_FPGA_REGION)		+= fpga-region.o
 obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
+# FPGA DMA Buffer Interface
+obj-$(CONFIG_FPGA_DMA_BUF)		+= fpga-dmabuf.o
 
 # FPGA Device Feature List Support
 obj-$(CONFIG_FPGA_DFL)			+= dfl.o
drivers/fpga/fpga-dmabuf.c new file +198 -0

198 lines not on this page · raw message

include/linux/fpga/fpga-dmabuf.h new file +22 -0
new file mode 100644
index 000000000000..bf54bdfdf968
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+#ifndef _LINUX_FPGA_DMABUF_H
+#define _LINUX_FPGA_DMABUF_H
+
+struct fpga_manager;
+
+#if IS_REACHABLE(CONFIG_FPGA_DMA_BUF)
+int fpga_dmabuf_register(struct fpga_manager *mgr);
+void fpga_dmabuf_unregister(struct fpga_manager *mgr);
+#else
+static inline int fpga_dmabuf_register(struct fpga_manager *mgr)
+{
+	return 0;
+}
+
+static inline void fpga_dmabuf_unregister(struct fpga_manager *mgr) {}
+#endif
+
+#endif /* _LINUX_FPGA_DMABUF_H */
include/linux/fpga/fpga-mgr.h +1 -0
index 0d4fe068f3d8..38af47f60503 100644
@@ -213,6 +213,7 @@ struct fpga_manager {
 	const struct fpga_manager_ops *mops;
 	struct module *mops_owner;
 	void *priv;
+	void *dmabuf_priv;
 };
 
 #define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
include/uapi/linux/fpga.h new file +15 -0
new file mode 100644
index 000000000000..868650c66526
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * FPGA userspace API
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+#ifndef _UAPI_LINUX_FPGA_H
+#define _UAPI_LINUX_FPGA_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define FPGA_IOCTL_LOAD_DMA_BUF	_IOW('J', 1, __s32)
+
+#endif /* _UAPI_LINUX_FPGA_H */
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] fpga: Add dma-buf interface for FPGA programming

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Passing a DMA-mapped dma-buf scatterlist to the FPGA manager core causes double-mapping or CPU…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Passing a DMA-mapped dma-buf scatterlist to the FPGA manager core causes double-mapping or CPU access of device memory, triggering kernel panics.
- [High] The FPGA_IOCTL_LOAD_DMA_BUF ioctl lacks a firmware size parameter, causing the full page-aligned dma-buf capacity to be pushed into the FPGA port, corrupting configuration. --

--- Patch [1]: [PATCH 1/2] fpga: Add dma-buf interface for FPGA programming --- commit 9a25ee19d33999a56fc7670a1a9dbec98cb947aa
Author: Aravind Thokala <[email protected]>

fpga: Add dma-buf interface for FPGA programming

This commit adds a module that allows userspace to pass a pre-allocated DMA buffer directly to the FPGA manager via ioctl. It creates a miscdevice for each FPGA manager that registers dma-buf support to map and load the image without an extra memory copy.

Quoted 5 lines
drivers/fpga/fpga-dmabuf.c new file +0 -0
new file mode 100644
index 0000000000000..0e92eb1346981

[ ... ]

Quoted 7 lines
drivers/fpga/fpga-dmabuf.c new file +7 -0
+static int fpga_dmabuf_load(struct fpga_manager *mgr, int buffd)
+{
+	struct fpga_image_info *info;
+	struct dma_buf_attachment *attach;
+	struct dma_buf *dmabuf;
+	struct sg_table *sgt;
+	int ret;

[ ... ]

drivers/fpga/fpga-dmabuf.c line 72

Quoted 13 lines
drivers/fpga/fpga-dmabuf.c new file +13 -0
+	sgt = dma_buf_map_attachment_unlocked(attach, DMA_TO_DEVICE);
+	if (IS_ERR(sgt)) {
+		ret = PTR_ERR(sgt);
+		goto err_detach;
+	}
+
+	info->sgt = sgt;
+
+	ret = fpga_mgr_lock(mgr);
+	if (ret)
+		goto err_unmap;
+
+	ret = fpga_mgr_load(mgr, info);

[Severity: Critical]
Does passing a DMA-mapped sg_table into fpga_mgr_load() cause issues with the underlying FPGA manager drivers?

Hardware drivers typically expect an unmapped CPU sg_table and will call dma_map_sgtable() on it. Calling dma_map_sgtable() on an already mapped sg_table can overwrite DMA addresses and corrupt the DMA state.

Additionally, the core manager may fall back to CPU PIO to read the image header via sg_miter (which internally uses kmap). If the dma-buf is backed by P2P device memory, this could dereference invalid struct pages and cause a kernel panic. It also bypasses cache coherency since dma_buf_begin_cpu_access() is never called.

[ ... ]

Quoted 9 lines
include/uapi/linux/fpga.h new file +3 -0
new file mode 100644
index 0000000000000..868650c66526b
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * FPGA userspace API

include/uapi/linux/fpga.h line 13

Quoted 10 lines
include/uapi/linux/fpga.h new file +10 -0
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+#ifndef _UAPI_LINUX_FPGA_H
+#define _UAPI_LINUX_FPGA_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define FPGA_IOCTL_LOAD_DMA_BUF	_IOW('J', 1, __s32)

[Severity: High]
Since this IOCTL only takes a file descriptor (__s32), how does userspace supply the exact payload size of the firmware?

In fpga_dmabuf_load(), the info struct is zero-allocated, leaving info->data_size as 0. When fpga_mgr_buf_load_sg() evaluates the scatterlist, if data_size is 0, it loops over the entire mapped capacity of the sg_table.

Because dma-bufs are typically page-aligned, will this result in writing padding bytes at the end of the buffer directly into the FPGA configuration port, potentially corrupting the hardware state?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/2 fpga: versal: add dma-buf programming support

In reply to Aravind Thokala

The existing .write callback allocates a DMA-coherent buffer and copies the image into it before passing the address to firmware. With dma-buf, the buffer is already DMA-mapped by userspace.

Add a .write_sg callback that extracts the DMA address directly from the scatter-gather table and passes it to zynqmp_pm_load_pdi(), avoiding the intermediate copy. Register the dma-buf interface in probe with devres-managed cleanup.

---
 drivers/fpga/versal-fpga.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
drivers/fpga/versal-fpga.c +32 -1
index e6189106c468..bdaa15a6c991 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/dma-mapping.h>
+#include <linux/fpga/fpga-dmabuf.h>
 #include <linux/fpga/fpga-mgr.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
@@ -37,11 +38,34 @@ static int versal_fpga_ops_write(struct fpga_manager *mgr,
 	return ret;
 }
 
+static int versal_fpga_ops_write_sg(struct fpga_manager *mgr,
+				    struct sg_table *sgt)
+{
+	dma_addr_t dma_addr;
+
+	/* zynqmp_pm_load_pdi() takes a single base address */
+	if (sgt->nents != 1) {
+		dev_err(&mgr->dev, "dma-buf has %u segments, need exactly 1\n",
+			sgt->nents);
+		return -EINVAL;
+	}
+
+	dma_addr = sg_dma_address(sgt->sgl);
+
+	return zynqmp_pm_load_pdi(PDI_SRC_DDR, dma_addr);
+}
+
 static const struct fpga_manager_ops versal_fpga_ops = {
 	.write_init = versal_fpga_ops_write_init,
 	.write = versal_fpga_ops_write,
+	.write_sg = versal_fpga_ops_write_sg,
 };
 
+static void versal_fpga_dmabuf_remove(void *data)
+{
+	fpga_dmabuf_unregister(data);
+}
+
 static int versal_fpga_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -56,7 +80,14 @@ static int versal_fpga_probe(struct platform_device *pdev)
 
 	mgr = devm_fpga_mgr_register(dev, "Xilinx Versal FPGA Manager",
 				     &versal_fpga_ops, NULL);
-	return PTR_ERR_OR_ZERO(mgr);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
+
+	ret = fpga_dmabuf_register(mgr);
+	if (ret)
+		return ret;
+
+	return devm_add_action_or_reset(dev, versal_fpga_dmabuf_remove, mgr);
 }
 
 static const struct of_device_id versal_fpga_of_match[] = {
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] fpga: versal: add dma-buf programming support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Passing a user-accessible dma-buf directly to the PMC firmware introduces a speculative…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Passing a user-accessible dma-buf directly to the PMC firmware introduces a speculative Time-of-Check to Time-of-Use (TOCTOU) vulnerability that can potentially bypass FPGA bitstream authentication. --

--- Patch [2]: [PATCH 2/2] fpga: versal: add dma-buf programming support --- commit 92384704c37fbbfe16cd9659151fbb8646c178cc
Author: Aravind Thokala <[email protected]>

fpga: versal: add dma-buf programming support

This commit adds a .write_sg callback to the Versal FPGA driver to support dma-buf based programming. It extracts the DMA address from the scatter-gather table and passes it directly to the firmware, avoiding an intermediate buffer copy.

Quoted 4 lines
drivers/fpga/versal-fpga.c +0 -0
index e6189106c4686..bdaa15a6c9913 100644

[ ... ]

Quoted 9 lines
drivers/fpga/versal-fpga.c +5 -0
@@ -37,11 +38,34 @@ static int versal_fpga_ops_write(struct fpga_manager *mgr,
 	return ret;
 }
 
+static int versal_fpga_ops_write_sg(struct fpga_manager *mgr,
+				    struct sg_table *sgt)
+{
+	dma_addr_t dma_addr;
+

drivers/fpga/versal-fpga.c static int versal_fpga_ops_write(struct fpga_manager *mgr, · line 55

Quoted 10 lines
drivers/fpga/versal-fpga.c +10 -0
+	/* zynqmp_pm_load_pdi() takes a single base address */
+	if (sgt->nents != 1) {
+		dev_err(&mgr->dev, "dma-buf has %u segments, need exactly 1\n",
+			sgt->nents);
+		return -EINVAL;
+	}
+
+	dma_addr = sg_dma_address(sgt->sgl);
+
+	return zynqmp_pm_load_pdi(PDI_SRC_DDR, dma_addr);

[Severity: Medium]
Does this expose the firmware to a Time-of-Check to Time-of-Use (TOCTOU) vulnerability if the Xilinx PMC firmware double-fetches the payload?

By extracting the DMA address from the user-provided dma-buf and passing it directly to zynqmp_pm_load_pdi() without copying it to a kernel-owned isolated buffer, a process could potentially modify the bitstream concurrently during the firmware's authentication and loading process.

If a user with CAP_SYS_RAWIO maintains a writable memory mapping to the dma-buf (for instance, using udmabuf) and invokes FPGA_IOCTL_LOAD_DMA_BUF, they could overwrite the memory while the firmware reads it.

If the firmware re-reads any part of the bitstream after authentication, could this be abused to bypass secure boot boundaries?

Quoted 2 lines
drivers/fpga/versal-fpga.c +2 -0
+}
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH 2/2] fpga: versal: add dma-buf programming support

Hi,

Quoted 60 lines · On 8/16/2026 8:48 PM, [email protected] wrote:

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Passing a user-accessible dma-buf directly to the PMC firmware introduces a speculative Time-of-Check to Time-of-Use (TOCTOU) vulnerability that can potentially bypass FPGA bitstream authentication. --

--- Patch [2]: [PATCH 2/2] fpga: versal: add dma-buf programming support --- commit 92384704c37fbbfe16cd9659151fbb8646c178cc
Author: Aravind Thokala <[email protected]>

fpga: versal: add dma-buf programming support

This commit adds a .write_sg callback to the Versal FPGA driver to support dma-buf based programming. It extracts the DMA address from the scatter-gather table and passes it directly to the firmware, avoiding an intermediate buffer copy.

Quoted 4 lines
drivers/fpga/versal-fpga.c +0 -0
index e6189106c4686..bdaa15a6c9913 100644

[ ... ]

Quoted 9 lines
drivers/fpga/versal-fpga.c +5 -0
@@ -37,11 +38,34 @@ static int versal_fpga_ops_write(struct fpga_manager *mgr,
       return ret;
  }
 
+static int versal_fpga_ops_write_sg(struct fpga_manager *mgr,
+                                 struct sg_table *sgt)
+{
+     dma_addr_t dma_addr;
+

drivers/fpga/versal-fpga.c static int versal_fpga_ops_write(struct fpga_manager *mgr, · line 55

Quoted 10 lines
drivers/fpga/versal-fpga.c +10 -0
+     /* zynqmp_pm_load_pdi() takes a single base address */
+     if (sgt->nents != 1) {
+             dev_err(&mgr->dev, "dma-buf has %u segments, need exactly 1\n",
+                     sgt->nents);
+             return -EINVAL;
+     }
+
+     dma_addr = sg_dma_address(sgt->sgl);
+
+     return zynqmp_pm_load_pdi(PDI_SRC_DDR, dma_addr);

[Severity: Medium]
Does this expose the firmware to a Time-of-Check to Time-of-Use (TOCTOU) vulnerability if the Xilinx PMC firmware double-fetches the payload?

By extracting the DMA address from the user-provided dma-buf and passing it directly to zynqmp_pm_load_pdi() without copying it to a kernel-owned isolated buffer, a process could potentially modify the bitstream concurrently during the firmware's authentication and loading process.

If a user with CAP_SYS_RAWIO maintains a writable memory mapping to the dma-buf (for instance, using udmabuf) and invokes FPGA_IOCTL_LOAD_DMA_BUF, they could overwrite the memory while the firmware reads it.

Quoted 8 lines

If the firmware re-reads any part of the bitstream after authentication, could this be abused to bypass secure boot boundaries?

drivers/fpga/versal-fpga.c static int versal_fpga_ops_write(struct fpga_manager *mgr, · line 57

Quoted 2 lines
drivers/fpga/versal-fpga.c +2 -0
+}
+

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

This patch series provides a zero-copy path for FPGA programming by passing a userspace-allocated DMA buffer directly to the firmware. Secure bitstream authentication is outside the scope of this series. It is handled entirely by the firmware.

This follows the same approach as the existing write callback, which also provides a DMA address to zynqmp_pm_load_pdi().
The dma-buf path does not alter the existing security model.

Kind Regards,
Aravind.

#6 lore raw
In reply to Aravind Thokala
Quoted 11 lines · Aravind Thokala

Some systems need to load large FPGA configuration images. The FPGA subsystem allows loading images from the filesystem, but this requires the entire image to be loaded into kernel memory first. For drivers that need a DMA-capable buffer for programming, the data is then copied again into DMA memory. This creates needless memory pressure and delays due to the extra copy.

This series adds dma-buf support that allows userspace to allocate a buffer directly from a DMA heap, write the FPGA image into it, and pass the file descriptor to the kernel via ioctl — skipping the intermediate kernel buffer entirely.

Well when you have a device with limited DMA capabilitiesthen DMA buf heaps doesn't allocate DMA-capable memory for that device either.

So the explanation you give above why this interface might be useful is clearly not correct. The DMA subsystem will still do an additional copy when you try to import the DMA-buf allocated from the heap into this device.

Either you need to define a heap with specific allocation restrictions (e.g. GFP_DMA32) or you allocate the DMA-buf through your fpga device so that dma_alloc_attrs() knows that a certain device needs to access the pages beforehand.

Regards,
Christian.

Quoted 32 lines

Userspace flow:
1. Allocate buffer from /dev/dma_heap/ (e.g., CMA heap)
2. mmap the buffer and write the FPGA image into it
3. ioctl(/dev/fpgaX, FPGA_IOCTL_LOAD_DMA_BUF, &dmabuf_fd)

The dma-buf logic is implemented as a separate layer on top of the FPGA manager, keeping buffer management separate from the write path. Individual FPGA drivers opt in by calling fpga_dmabuf_register().
---
This work is based on the approach discussed in [1].

[1] https://lore.kernel.org/all/[email protected]/
---
Aravind Thokala (2):
fpga: Add dma-buf interface for FPGA programming
fpga: versal: add dma-buf programming support

 .../userspace-api/ioctl/ioctl-number.rst      |   1 +
 MAINTAINERS                                   |   1 +
 drivers/fpga/Kconfig                          |   9 +
 drivers/fpga/Makefile                         |   2 +
 drivers/fpga/fpga-dmabuf.c                    | 198 ++++++++++++++++++
 drivers/fpga/versal-fpga.c                    |  33 ++-
 include/linux/fpga/fpga-dmabuf.h              |  22 ++
 include/linux/fpga/fpga-mgr.h                 |   1 +
 include/uapi/linux/fpga.h                     |  15 ++
 9 files changed, 281 insertions(+), 1 deletion(-)
 create mode 100644 drivers/fpga/fpga-dmabuf.c
 create mode 100644 include/linux/fpga/fpga-dmabuf.h
 create mode 100644 include/uapi/linux/fpga.h
#7 lore raw

Hi Christian,

Quoted 13 lines · Christian König
Quoted 11 lines · Aravind Thokala

Some systems need to load large FPGA configuration images. The FPGA subsystem allows loading images from the filesystem, but this requires the entire image to be loaded into kernel memory first. For drivers that need a DMA-capable buffer for programming, the data is then copied again into DMA memory. This creates needless memory pressure and delays due to the extra copy.

This series adds dma-buf support that allows userspace to allocate a buffer directly from a DMA heap, write the FPGA image into it, and pass the file descriptor to the kernel via ioctl — skipping the intermediate kernel buffer entirely.

Quoted 9 lines

Well when you have a device with limited DMA capabilitiesthen DMA buf heaps doesn't allocate DMA-capable memory for that device either.

So the explanation you give above why this interface might be useful is clearly not correct. The DMA subsystem will still do an additional copy when you try to import the DMA-buf allocated from the heap into this device.

Either you need to define a heap with specific allocation restrictions (e.g. GFP_DMA32) or you allocate the DMA-buf through your fpga device so that dma_alloc_attrs() knows that a certain device needs to access the pages beforehand.

Regards,
Christian.

Thank you for the review. Agreed, the current approach does not guarantee DMA-capable allocations for all devices.

We are looking into the device-owned allocation approach using dma_alloc_attrs() and plan to follow up with an updated series.

Kind Regards,
Aravind.

Quoted 33 lines
Quoted 32 lines

Userspace flow:
1. Allocate buffer from /dev/dma_heap/ (e.g., CMA heap)
2. mmap the buffer and write the FPGA image into it
3. ioctl(/dev/fpgaX, FPGA_IOCTL_LOAD_DMA_BUF, &dmabuf_fd)

The dma-buf logic is implemented as a separate layer on top of the FPGA manager, keeping buffer management separate from the write path. Individual FPGA drivers opt in by calling fpga_dmabuf_register().
---
This work is based on the approach discussed in [1].

[1] https://lore.kernel.org/all/[email protected]/
---
Aravind Thokala (2):
fpga: Add dma-buf interface for FPGA programming
fpga: versal: add dma-buf programming support

  .../userspace-api/ioctl/ioctl-number.rst      |   1 +
  MAINTAINERS                                   |   1 +
  drivers/fpga/Kconfig                          |   9 +
  drivers/fpga/Makefile                         |   2 +
  drivers/fpga/fpga-dmabuf.c                    | 198 ++++++++++++++++++
  drivers/fpga/versal-fpga.c                    |  33 ++-
  include/linux/fpga/fpga-dmabuf.h              |  22 ++
  include/linux/fpga/fpga-mgr.h                 |   1 +
  include/uapi/linux/fpga.h                     |  15 ++
  9 files changed, 281 insertions(+), 1 deletion(-)

create mode 100644 drivers/fpga/fpga-dmabuf.c create mode 100644 include/linux/fpga/fpga-dmabuf.h create mode 100644 include/uapi/linux/fpga.h