Skip to contentSkip to conversation

remoteproc: elf_loader: Make rproc_elf_load_segments() more generic

PATCH Superseded Uwe Kleine-König 3 participants 6 messages Nov 5 – Nov 9

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover remoteproc: elf_loader: Make rproc_elf_load_segments() more generic

Hello,

changes since v2 (available at
https://lore.kernel.org/linux-remoteproc/[email protected]):

- trivially rebase to next-20261104
- update commit log of the first patch to justify the new callback working on the device address instead of a virtual address. The latter would work with imx_dsp_rproc, but on (not yet mainline) ADI SC846 it doesn't because the remote proc's memory isn't directly accessbile by Linux.

Uwe Kleine-König (2):
remoteproc: elf_loader: Create new callback for copying segments to
remote cpu
remoteproc: imx_dsp_rproc: Implement .memcpy_tova()

 drivers/remoteproc/imx_dsp_rproc.c         | 120 ++++++---------------
 drivers/remoteproc/remoteproc_elf_loader.c |  64 ++++++-----
 include/linux/remoteproc.h                 |   4 +
 3 files changed, 71 insertions(+), 117 deletions(-)

base-commit: 8b7179210b22f8e48a82c5ceacb9d56a9c4cfec4

Signature · 1 line
2.47.3
#1 lore raw

PATCH v3 1/2 remoteproc: elf_loader: Create new callback for copying segments to remote cpu

Some devices have special needs about how an Elf segment is copied to the remote cpu's memory space where the default implementation using da_to_va() + memcpy_toio/memcpy and memset_io/memset isn't suitable. An affected mainline driver is imx-dsp-rproc which includes a near-copy of rproc_elf_load_segments() just to handle the IRAM only allowing 32 bit accesses. On (not-yet-mainline) ADI SC846 the respective memory isn't directly accessible at all and DMA must be used to copy the data. To cover that usecase don't keep rproc_da_to_va() in rproc_elf_load_segments() and let the new callback operate on the device address.

The new callback can be used to implement just the driver specific memory mapping + memcpy() + memset() to increase code reuse.

The patch only appears big because of indentation changes.

---
 drivers/remoteproc/remoteproc_elf_loader.c | 64 +++++++++++++---------
 include/linux/remoteproc.h                 |  4 ++
 2 files changed, 41 insertions(+), 27 deletions(-)
drivers/remoteproc/remoteproc_elf_loader.c +37 -27
index da3cddbe7d4c..3c749a39e5da 100644
@@ -227,35 +227,45 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 			break;
 		}
 
-		/* grab the kernel address for this device address */
-		ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem);
-		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
-				memsz);
-			ret = -EINVAL;
-			break;
-		}
+		if (rproc->ops->memcpy_tova) {
+			ret = rproc->ops->memcpy_tova(rproc, da, elf_data + offset, filesz, memsz);
+			if (ret) {
+				dev_err(dev, "Failed to copy segment %d to device address 0x%llx: %pe\n",
+					i, da, ERR_PTR(ret));
+				break;
+			}
+		} else {
+			/* grab the kernel address for this device address */
+			ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem);
+			if (!ptr) {
+				dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
+					memsz);
+				ret = -EINVAL;
+				break;
+			}
 
-		/* put the segment where the remote processor expects it */
-		if (filesz) {
-			if (is_iomem)
-				memcpy_toio((void __iomem *)ptr, elf_data + offset, filesz);
-			else
-				memcpy(ptr, elf_data + offset, filesz);
-		}
+			/* put the segment where the remote processor expects it */
+			if (filesz) {
+				if (is_iomem)
+					memcpy_toio((void __iomem *)ptr, elf_data + offset, filesz);
+				else
+					memcpy(ptr, elf_data + offset, filesz);
+			}
 
-		/*
-		 * Zero out remaining memory for this segment.
-		 *
-		 * This isn't strictly required since dma_alloc_coherent already
-		 * did this for us. albeit harmless, we may consider removing
-		 * this.
-		 */
-		if (memsz > filesz) {
-			if (is_iomem)
-				memset_io((void __iomem *)(ptr + filesz), 0, memsz - filesz);
-			else
-				memset(ptr + filesz, 0, memsz - filesz);
+			/*
+			 * Zero out remaining memory for this segment.
+			 *
+			 * This isn't strictly required since dma_alloc_coherent already
+			 * did this for us. albeit harmless, we may consider removing
+			 * this.
+			 */
+			if (memsz > filesz) {
+				if (is_iomem)
+					memset_io((void __iomem *)(ptr + filesz),
+						  0, memsz - filesz);
+				else
+					memset(ptr + filesz, 0, memsz - filesz);
+			}
 		}
 	}
 
include/linux/remoteproc.h +4 -0
index 00019b1895e6..e750a127cbc4 100644
@@ -75,6 +75,7 @@ enum rsc_handling_status {
  * @detach:	detach from a device, leaving it powered up
  * @kick:	kick a virtqueue (virtqueue id given as a parameter)
  * @da_to_va:	optional platform hook to perform address translations
+ * @memcpy_tova:optional platform hook to copy data into remote memory range
  * @parse_fw:	parse firmware to extract information (e.g. resource table)
  * @handle_rsc:	optional platform hook to handle vendor resources. Should return
  *		RSC_HANDLED if resource was handled, RSC_IGNORED if not handled
@@ -99,6 +100,9 @@ struct rproc_ops {
 	int (*detach)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
 	void * (*da_to_va)(struct rproc *rproc, u64 da, size_t len, bool *is_iomem);
+	int (*memcpy_tova)(struct rproc *rproc, u64 da, const void *src,
+			   size_t filesz, size_t memsz);
+
 	int (*parse_fw)(struct rproc *rproc, const struct firmware *fw);
 	int (*handle_rsc)(struct rproc *rproc, u32 rsc_type, void *rsc,
 			  int offset, int avail);
Signature · 1 line
2.47.3
#5 lore raw

PATCH v3 2/2 remoteproc: imx_dsp_rproc: Implement .memcpy_tova()

In reply to Uwe Kleine-König

This callback allows to use the core's rproc_elf_load_segments() function instead of reimplementing it for very minor changes.

---
 drivers/remoteproc/imx_dsp_rproc.c | 120 ++++++++---------------------
 1 file changed, 30 insertions(+), 90 deletions(-)
drivers/remoteproc/imx_dsp_rproc.c +30 -90
index 96d619daf3f5..324c10f18507 100644
@@ -864,97 +864,41 @@ static int imx_dsp_rproc_memset(void *addr, u8 value, size_t size)
 	return 0;
 }
 
-/*
- * imx_dsp_rproc_elf_load_segments() - load firmware segments to memory
- * @rproc: remote processor which will be booted using these fw segments
- * @fw: the ELF firmware image
- *
- * This function loads the firmware segments to memory, where the remote
- * processor expects them.
- *
- * Return: 0 on success and an appropriate error code otherwise
- */
-static int imx_dsp_rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
+static int imx_dsp_rproc_memcpy_tova(struct rproc *rproc, u64 da, const void *src,
+				     size_t filesz, size_t memsz)
 {
 	struct device *dev = &rproc->dev;
-	const void *ehdr, *phdr;
-	int i, ret = 0;
-	u16 phnum;
-	const u8 *elf_data = fw->data;
-	u8 class = fw_elf_get_class(fw);
-	u32 elf_phdr_get_size = elf_size_of_phdr(class);
+	void *ptr;
+	int ret;
 
-	ehdr = elf_data;
-	phnum = elf_hdr_get_e_phnum(class, ehdr);
-	phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr);
+	/* grab the kernel address for this device address */
+	ptr = rproc_da_to_va(rproc, da, memsz, NULL);
+	if (!ptr) {
+		dev_err(dev, "bad phdr da 0x%llx mem 0x%zx\n", da, memsz);
+		return -EINVAL;
+	}
 
-	/* go through the available ELF segments */
-	for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) {
-		u64 da = elf_phdr_get_p_paddr(class, phdr);
-		u64 memsz = elf_phdr_get_p_memsz(class, phdr);
-		u64 filesz = elf_phdr_get_p_filesz(class, phdr);
-		u64 offset = elf_phdr_get_p_offset(class, phdr);
-		u32 type = elf_phdr_get_p_type(class, phdr);
-		void *ptr;
-
-		if (type != PT_LOAD || !memsz)
-			continue;
-
-		dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
-			type, da, memsz, filesz);
-
-		if (filesz > memsz) {
-			dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
-				filesz, memsz);
-			ret = -EINVAL;
-			break;
-		}
-
-		if (offset + filesz > fw->size) {
-			dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
-				offset + filesz, fw->size);
-			ret = -EINVAL;
-			break;
-		}
-
-		if (!rproc_u64_fit_in_size_t(memsz)) {
-			dev_err(dev, "size (%llx) does not fit in size_t type\n",
-				memsz);
-			ret = -EOVERFLOW;
-			break;
-		}
-
-		/* grab the kernel address for this device address */
-		ptr = rproc_da_to_va(rproc, da, memsz, NULL);
-		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
-				memsz);
-			ret = -EINVAL;
-			break;
-		}
-
-		/* put the segment where the remote processor expects it */
-		if (filesz) {
-			ret = imx_dsp_rproc_memcpy(ptr, elf_data + offset, filesz);
-			if (ret) {
-				dev_err(dev, "memory copy failed for da 0x%llx memsz 0x%llx\n",
-					da, memsz);
-				break;
-			}
-		}
-
-		/* zero out remaining memory for this segment */
-		if (memsz > filesz) {
-			ret = imx_dsp_rproc_memset(ptr + filesz, 0, memsz - filesz);
-			if (ret) {
-				dev_err(dev, "memset failed for da 0x%llx memsz 0x%llx\n",
-					da, memsz);
-				break;
-			}
+	/* put the segment where the remote processor expects it */
+	if (filesz) {
+		ret = imx_dsp_rproc_memcpy(ptr, src, filesz);
+		if (ret) {
+			dev_err(dev, "memory copy failed for da 0x%llx memsz 0x%zx\n",
+				da, memsz);
+			return ret;
 		}
 	}
 
-	return ret;
+	/* zero out remaining memory for this segment */
+	if (memsz > filesz) {
+		ret = imx_dsp_rproc_memset(ptr + filesz, 0, memsz - filesz);
+		if (ret) {
+			dev_err(dev, "memset failed for da 0x%llx memsz 0x%zx\n",
+				da, memsz);
+			return ret;
+		}
+	}
+
+	return 0;
 }
 
 static int imx_dsp_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
@@ -969,7 +913,6 @@ static int imx_dsp_rproc_load(struct rproc *rproc, const struct firmware *fw)
 	struct imx_dsp_rproc *priv = rproc->priv;
 	const struct imx_dsp_rproc_dcfg *dsp_dcfg = priv->dsp_dcfg;
 	struct rproc_mem_entry *carveout;
-	int ret;
 
 	/* Reset DSP if needed */
 	if (dsp_dcfg->reset)
@@ -985,11 +928,7 @@ static int imx_dsp_rproc_load(struct rproc *rproc, const struct firmware *fw)
 		}
 	}
 
-	ret = imx_dsp_rproc_elf_load_segments(rproc, fw);
-	if (ret)
-		return ret;
-
-	return 0;
+	return rproc_elf_load_segments(rproc, fw);
 }
 
 static const struct rproc_ops imx_dsp_rproc_ops = {
@@ -998,6 +937,7 @@ static const struct rproc_ops imx_dsp_rproc_ops = {
 	.start		= imx_dsp_rproc_start,
 	.stop		= imx_dsp_rproc_stop,
 	.kick		= imx_dsp_rproc_kick,
+	.memcpy_tova	= imx_dsp_rproc_memcpy_tova,
 	.load		= imx_dsp_rproc_load,
 	.parse_fw	= imx_dsp_rproc_parse_fw,
 	.handle_rsc	= imx_dsp_rproc_handle_rsc,
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v3 1/2] remoteproc: elf_loader: Create new callback for copying segments to remote cpu

In reply to Uwe Kleine-König
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] remoteproc: misleading memcpy_tova callback name and incomplete kernel-doc --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] remoteproc: misleading memcpy_tova callback name and incomplete kernel-doc --

commit 1f800d9eb3b123f9d100331894730d47e230cc68
Author: Uwe Kleine-König <[email protected]>

remoteproc: elf_loader: Create new callback for copying segments to remote cpu

This commit introduces a new memcpy_tova callback in struct rproc_ops so platform drivers with special memory access requirements can override the default rproc_da_to_va() plus memcpy/memset sequence in rproc_elf_load_segments().

Quoted 44 lines
drivers/remoteproc/remoteproc_elf_loader.c +7 -29
index da3cddbe7d4cb..3c749a39e5dad 100644
@@ -227,35 +227,45 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 			break;
 		}
 
-		/* grab the kernel address for this device address */
-		ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem);
-		if (!ptr) {
-			dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
-				memsz);
-			ret = -EINVAL;
-			break;
-		}
-
-		/* put the segment where the remote processor expects it */
-		if (filesz) {
-			if (is_iomem)
-				memcpy_toio((void __iomem *)ptr, elf_data + offset, filesz);
-			else
-				memcpy(ptr, elf_data + offset, filesz);
-		}
-
-		/*
-		 * Zero out remaining memory for this segment.
-		 *
-		 * This isn't strictly required since dma_alloc_coherent already
-		 * did this for us. albeit harmless, we may consider removing
-		 * this.
-		 */
-		if (memsz > filesz) {
-			if (is_iomem)
-				memset_io((void __iomem *)(ptr + filesz), 0, memsz - filesz);
-			else
-				memset(ptr + filesz, 0, memsz - filesz);
+		if (rproc->ops->memcpy_tova) {
+			ret = rproc->ops->memcpy_tova(rproc, da, elf_data + offset, filesz, memsz);
+			if (ret) {
+				dev_err(dev, "Failed to copy segment %d to device address 0x%llx: %pe\n",
+					i, da, ERR_PTR(ret));
+				break;
+			}

[ ... ]

Quoted 13 lines
include/linux/remoteproc.h +1 -0
index 00019b1895e6e..e750a127cbc4a 100644
@@ -75,6 +75,7 @@ enum rsc_handling_status {
  * @detach:	detach from a device, leaving it powered up
  * @kick:	kick a virtqueue (virtqueue id given as a parameter)
  * @da_to_va:	optional platform hook to perform address translations
+ * @memcpy_tova:optional platform hook to copy data into remote memory range
  * @parse_fw:	parse firmware to extract information (e.g. resource table)
  * @handle_rsc:	optional platform hook to handle vendor resources. Should return
  *		RSC_HANDLED if resource was handled, RSC_IGNORED if not handled

include/linux/remoteproc.h struct rproc_ops { · line 104

Quoted 5 lines
include/linux/remoteproc.h +2 -0
@@ -99,6 +100,9 @@ struct rproc_ops {
 	int (*detach)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
 	void * (*da_to_va)(struct rproc *rproc, u64 da, size_t len, bool *is_iomem);
+	int (*memcpy_tova)(struct rproc *rproc, u64 da, const void *src,
+			   size_t filesz, size_t memsz);

[Severity: Low]
This isn't a bug, but is memcpy_tova the best name for this callback in struct rproc_ops?

Across the remoteproc subsystem (such as in struct rproc_mem_entry and ops->da_to_va), da denotes a remote processor device address while va denotes a kernel virtual address. Since rproc_elf_load_segments() passes the untranslated device address da directly to this callback (including for devices where remote memory is not mapped into CPU virtual address space at all), calling it memcpy_tova seems somewhat misleading.

In addition, when rproc->ops->memcpy_tova is provided, rproc_elf_load_segments() skips both the filesz copy and the trailing memsz - filesz zeroing step. Should the kernel-doc comment for @memcpy_tova in include/linux/remoteproc.h document that implementations must copy filesz bytes to da and zero-fill the remaining memsz - filesz bytes when memsz > filesz?

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

Re: [PATCH v3 1/2] remoteproc: elf_loader: Create new callback for copying segments to remote cpu

Hello,

Quoted 30 lines · On Thu, Nov 05, 2026 at 12:10:05PM +0000, [email protected] wrote:
Quoted 13 lines
include/linux/remoteproc.h +1 -0
index 00019b1895e6e..e750a127cbc4a 100644
@@ -75,6 +75,7 @@ enum rsc_handling_status {
  * @detach:	detach from a device, leaving it powered up
  * @kick:	kick a virtqueue (virtqueue id given as a parameter)
  * @da_to_va:	optional platform hook to perform address translations
+ * @memcpy_tova:optional platform hook to copy data into remote memory range
  * @parse_fw:	parse firmware to extract information (e.g. resource table)
  * @handle_rsc:	optional platform hook to handle vendor resources. Should return
  *		RSC_HANDLED if resource was handled, RSC_IGNORED if not handled

include/linux/remoteproc.h struct rproc_ops { · line 104

Quoted 5 lines
include/linux/remoteproc.h +2 -0
@@ -99,6 +100,9 @@ struct rproc_ops {
 	int (*detach)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
 	void * (*da_to_va)(struct rproc *rproc, u64 da, size_t len, bool *is_iomem);
+	int (*memcpy_tova)(struct rproc *rproc, u64 da, const void *src,
+			   size_t filesz, size_t memsz);

[Severity: Low]
This isn't a bug, but is memcpy_tova the best name for this callback in struct rproc_ops?

Across the remoteproc subsystem (such as in struct rproc_mem_entry and ops->da_to_va), da denotes a remote processor device address while va denotes a kernel virtual address. Since rproc_elf_load_segments() passes the untranslated device address da directly to this callback (including for devices where remote memory is not mapped into CPU virtual address space at all), calling it memcpy_tova seems somewhat misleading.

Quoted 6 lines

In addition, when rproc->ops->memcpy_tova is provided, rproc_elf_load_segments() skips both the filesz copy and the trailing memsz - filesz zeroing step. Should the kernel-doc comment for @memcpy_tova in include/linux/remoteproc.h document that implementations must copy filesz bytes to da and zero-fill the remaining memsz - filesz bytes when memsz > filesz?

That's a valid concern. Maybe populate_segment would be a better name? I considered being more verbose in the kdoc entry, but all the other entries are very sparse, too, so didn't want to add a 5 line comment for the new callback among all the one-liners. But I can if you tell me your preferences.

Best regards
Uwe

maintainer
#4 lore raw

Re: [PATCH v3 1/2] remoteproc: elf_loader: Create new callback for copying segments to remote cpu

Quoted 40 lines · Uwe Kleine-König

Hello,

Quoted 30 lines · On Thu, Nov 05, 2026 at 12:10:05PM +0000, [email protected] wrote:
Quoted 13 lines
include/linux/remoteproc.h +1 -0
index 00019b1895e6e..e750a127cbc4a 100644
@@ -75,6 +75,7 @@ enum rsc_handling_status {
  * @detach:	detach from a device, leaving it powered up
  * @kick:	kick a virtqueue (virtqueue id given as a parameter)
  * @da_to_va:	optional platform hook to perform address translations
+ * @memcpy_tova:optional platform hook to copy data into remote memory range
  * @parse_fw:	parse firmware to extract information (e.g. resource table)
  * @handle_rsc:	optional platform hook to handle vendor resources. Should return
  *		RSC_HANDLED if resource was handled, RSC_IGNORED if not handled

include/linux/remoteproc.h struct rproc_ops { · line 104

Quoted 5 lines
include/linux/remoteproc.h +2 -0
@@ -99,6 +100,9 @@ struct rproc_ops {
 	int (*detach)(struct rproc *rproc);
 	void (*kick)(struct rproc *rproc, int vqid);
 	void * (*da_to_va)(struct rproc *rproc, u64 da, size_t len, bool *is_iomem);
+	int (*memcpy_tova)(struct rproc *rproc, u64 da, const void *src,
+			   size_t filesz, size_t memsz);

[Severity: Low]
This isn't a bug, but is memcpy_tova the best name for this callback in struct rproc_ops?

Across the remoteproc subsystem (such as in struct rproc_mem_entry and ops->da_to_va), da denotes a remote processor device address while va denotes a kernel virtual address. Since rproc_elf_load_segments() passes the untranslated device address da directly to this callback (including for devices where remote memory is not mapped into CPU virtual address space at all), calling it memcpy_tova seems somewhat misleading.

Quoted 6 lines

In addition, when rproc->ops->memcpy_tova is provided, rproc_elf_load_segments() skips both the filesz copy and the trailing memsz - filesz zeroing step. Should the kernel-doc comment for @memcpy_tova in include/linux/remoteproc.h document that implementations must copy filesz bytes to da and zero-fill the remaining memsz - filesz bytes when memsz > filesz?

Quoted 6 lines

That's a valid concern. Maybe populate_segment would be a better name? I considered being more verbose in the kdoc entry, but all the other entries are very sparse, too, so didn't want to add a 5 line comment for the new callback among all the one-liners. But I can if you tell me your preferences.

I would stick with ->memcpy_tova() to keey the mcmcpy() semantic already rproc_elf_load_segments(). But yes, go ahead and add the 5 line comment.

Quoted 2 lines

Best regards
Uwe