Skip to contentSkip to conversation

remoteproc: add elf resource table loader

PATCH Superseded Fabien Dessenne 3 participants 19 messages Oct 9, 2019 – Dec 10, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover remoteproc: add elf resource table loader

Add some helpers that can be called by the drivers to load the firmware resource table from an elf32 / elf64 image.
The stm32 remoteproc driver makes use of it, to load the resource table before the elf image itself.

This series applies on top of the "remoteproc: Add support for R5F and DSP processors" series [1] proposed by Lokesh Vutla which introduces the elf64 support.

[1]: https://patchwork.ozlabs.org/project/uboot/list/?series=128946

Fabien Dessenne (5):
remoteproc: elf_loader: Add elf resource table load support
stm32mp1: declare backup register for copro resource table address
remoteproc: stm32: load resource table from firmware
stm32mp1: Fixup the Linux DeviceTree with coprocessor information
remoteproc: stm32: invert the is_running() return value

 arch/arm/mach-stm32mp/include/mach/stm32.h |   1 +
 board/st/stm32mp1/stm32mp1.c               |  16 +-
 drivers/remoteproc/rproc-elf-loader.c      | 269 +++++++++++++++++++++++++++++
 drivers/remoteproc/stm32_copro.c           |  17 +-
 include/remoteproc.h                       |  70 ++++++++
 test/dm/remoteproc.c                       |  91 ++++++++--
 6 files changed, 448 insertions(+), 16 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

---
 drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
 include/remoteproc.h                  |  70 +++++++++
 test/dm/remoteproc.c                  |  91 ++++++++++--
 3 files changed, 419 insertions(+), 11 deletions(-)
drivers/remoteproc/rproc-elf-loader.c +269 -0
index b38a226..9127ea5 100644
@@ -7,6 +7,39 @@
 #include <elf.h>
 #include <remoteproc.h>
 
+/**
+ * struct resource_table - firmware resource table header
+ * @ver: version number
+ * @num: number of resource entries
+ * @reserved: reserved (must be zero)
+ * @offset: array of offsets pointing at the various resource entries
+ *
+ * A resource table is essentially a list of system resources required
+ * by the remote processor. It may also include configuration entries.
+ * If needed, the remote processor firmware should contain this table
+ * as a dedicated ".resource_table" ELF section.
+ *
+ * Some resources entries are mere announcements, where the host is informed
+ * of specific remoteproc configuration. Other entries require the host to
+ * do something (e.g. allocate a system resource). Sometimes a negotiation
+ * is expected, where the firmware requests a resource, and once allocated,
+ * the host should provide back its details (e.g. address of an allocated
+ * memory region).
+ *
+ * The header of the resource table, as expressed by this structure,
+ * contains a version number (should we need to change this format in the
+ * future), the number of available resource entries, and their offsets
+ * in the table.
+ *
+ * Immediately following this header are the resource entries themselves.
+ */
+struct resource_table {
+	u32 ver;
+	u32 num;
+	u32 reserved[2];
+	u32 offset[0];
+} __packed;
+
 /* Basic function to verify ELF32 image format */
 int rproc_elf32_sanity_check(ulong addr, ulong size)
 {
@@ -275,3 +308,239 @@ ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
 	else
 		return rproc_elf32_get_boot_addr(addr);
 }
+
+/*
+ * Search for the resource table in an ELF32 image.
+ * Returns the address of the resource table section if found, NULL if there is
+ * no resource table section, or error pointer.
+ */
+static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
+					      ulong fw_addr, ulong fw_size)
+{
+	int ret;
+	unsigned int i;
+	const char *name_table;
+	struct resource_table *table;
+	const u8 *elf_data = (void *)fw_addr;
+	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
+	Elf32_Shdr *shdr;
+
+	ret = rproc_elf32_sanity_check(fw_addr, fw_size);
+	if (ret) {
+		pr_debug("Invalid ELF32 Image %d\n", ret);
+		return ERR_PTR(ret);
+	}
+
+	/* look for the resource table and handle it */
+	shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
+	name_table = (const char *)(elf_data +
+				    shdr[ehdr->e_shstrndx].sh_offset);
+
+	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
+		u32 size = shdr->sh_size;
+		u32 offset = shdr->sh_offset;
+
+		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+			continue;
+
+		table = (struct resource_table *)(elf_data + offset);
+
+		/* make sure we have the entire table */
+		if (offset + size > fw_size) {
+			pr_debug("resource table truncated\n");
+			return ERR_PTR(-ENOSPC);
+		}
+
+		/* make sure table has at least the header */
+		if (sizeof(*table) > size) {
+			pr_debug("header-less resource table\n");
+			return ERR_PTR(-ENOSPC);
+		}
+
+		/* we don't support any version beyond the first */
+		if (table->ver != 1) {
+			pr_debug("unsupported fw ver: %d\n", table->ver);
+			return ERR_PTR(-EPROTONOSUPPORT);
+		}
+
+		/* make sure reserved bytes are zeroes */
+		if (table->reserved[0] || table->reserved[1]) {
+			pr_debug("non zero reserved bytes\n");
+			return ERR_PTR(-EBADF);
+		}
+
+		/* make sure the offsets array isn't truncated */
+		if (table->num * sizeof(table->offset[0]) +
+				 sizeof(*table) > size) {
+			pr_debug("resource table incomplete\n");
+			return ERR_PTR(-ENOSPC);
+		}
+
+		return shdr;
+	}
+
+	return NULL;
+}
+
+/* Load the resource table from an ELF32 image */
+int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
+			       ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
+{
+	const struct dm_rproc_ops *ops;
+	Elf32_Shdr *shdr;
+	void *src, *dst;
+
+	shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
+	if (!shdr)
+		return -ENODATA;
+	if (IS_ERR(shdr))
+		return PTR_ERR(shdr);
+
+	ops = rproc_get_ops(dev);
+	*rsc_addr = (ulong)shdr->sh_addr;
+	*rsc_size = (ulong)shdr->sh_size;
+
+	src = (void *)fw_addr + shdr->sh_offset;
+	if (ops->device_to_virt)
+		dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
+	else
+		dst = (void *)rsc_addr;
+
+	dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
+		(ulong)dst, *rsc_size);
+
+	memcpy(dst, src, *rsc_size);
+	flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
+		    roundup((unsigned long)dst + *rsc_size,
+			    ARCH_DMA_MINALIGN) -
+		    rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
+
+	return 0;
+}
+
+/*
+ * Search for the resource table in an ELF64 image.
+ * Returns the address of the resource table section if found, NULL if there is
+ * no resource table section, or error pointer.
+ */
+static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
+					      ulong fw_addr, ulong fw_size)
+{
+	int ret;
+	unsigned int i;
+	const char *name_table;
+	struct resource_table *table;
+	const u8 *elf_data = (void *)fw_addr;
+	Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
+	Elf64_Shdr *shdr;
+
+	ret = rproc_elf64_sanity_check(fw_addr, fw_size);
+	if (ret) {
+		pr_debug("Invalid ELF64 Image %d\n", ret);
+		return ERR_PTR(ret);
+	}
+
+	/* look for the resource table and handle it */
+	shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
+	name_table = (const char *)(elf_data +
+				    shdr[ehdr->e_shstrndx].sh_offset);
+
+	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
+		u64 size = shdr->sh_size;
+		u64 offset = shdr->sh_offset;
+
+		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+			continue;
+
+		table = (struct resource_table *)(elf_data + offset);
+
+		/* make sure we have the entire table */
+		if (offset + size > fw_size) {
+			pr_debug("resource table truncated\n");
+			return ERR_PTR(-ENOSPC);
+		}
+
+		/* make sure table has at least the header */
+		if (sizeof(*table) > size) {
+			pr_debug("header-less resource table\n");
+			return ERR_PTR(-ENOSPC);
+		}
+
+		/* we don't support any version beyond the first */
+		if (table->ver != 1) {
+			pr_debug("unsupported fw ver: %d\n", table->ver);
+			return ERR_PTR(-EPROTONOSUPPORT);
+		}
+
+		/* make sure reserved bytes are zeroes */
+		if (table->reserved[0] || table->reserved[1]) {
+			pr_debug("non zero reserved bytes\n");
+			return ERR_PTR(-EBADF);
+		}
+
+		/* make sure the offsets array isn't truncated */
+		if (table->num * sizeof(table->offset[0]) +
+				 sizeof(*table) > size) {
+			pr_debug("resource table incomplete\n");
+			return ERR_PTR(-ENOSPC);
+		}
+
+		return shdr;
+	}
+
+	return NULL;
+}
+
+/* Load the resource table from an ELF64 image */
+int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
+			       ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
+{
+	const struct dm_rproc_ops *ops;
+	Elf64_Shdr *shdr;
+	void *src, *dst;
+
+	shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
+	if (!shdr)
+		return -ENODATA;
+	if (IS_ERR(shdr))
+		return PTR_ERR(shdr);
+
+	ops = rproc_get_ops(dev);
+	*rsc_addr = (ulong)shdr->sh_addr;
+	*rsc_size = (ulong)shdr->sh_size;
+
+	src = (void *)fw_addr + shdr->sh_offset;
+	if (ops->device_to_virt)
+		dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
+	else
+		dst = (void *)rsc_addr;
+
+	dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
+		(ulong)dst, *rsc_size);
+
+	memcpy(dst, src, *rsc_size);
+	flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
+		    roundup((unsigned long)dst + *rsc_size,
+			    ARCH_DMA_MINALIGN) -
+		    rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
+
+	return 0;
+}
+
+/* Load the resource table from an ELF32 or ELF64 image */
+int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
+			     ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
+
+{
+	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
+
+	if (!fw_addr)
+		return -EFAULT;
+
+	if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
+		return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
+						  rsc_addr, rsc_size);
+	else
+		return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,
+						  rsc_addr, rsc_size);
+}
include/remoteproc.h +70 -0

82 lines not on this page · raw message

test/dm/remoteproc.c +80 -11

124 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Hi Fabien,

Quoted 6 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
 drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
 include/remoteproc.h                  |  70 +++++++++
 test/dm/remoteproc.c                  |  91 ++++++++++--
 3 files changed, 419 insertions(+), 11 deletions(-)

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

Scanning the image for a table seems a bit horrible.

Regards,
Simon

#3 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Hi Simon,

Quoted 15 lines · Simon Glass

Hi Fabien,

Quoted 5 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
  drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
  include/remoteproc.h                  |  70 +++++++++
  test/dm/remoteproc.c                  |  91 ++++++++++--
  3 files changed, 419 insertions(+), 11 deletions(-)
Quoted 2 lines

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

The "resource table" may be located anywhere, there is no strict rule defining where it is expected to be.

Nevertheless the Linux remoteproc[1] and OpenAmp (running RTOS) [2] frameworks expect the resource table to be stored in a dedicated ELF section. Both of them run some ELF scanning to find out this section.

The proposed patch is for the "ELF section" variant of the resource table. Other variants like binman packing may be proposed as well, both implementations can coexist alongside.

BR

Fabien

[1] https://www.kernel.org/doc/Documentation/remoteproc.txt
[2]
https://github.com/OpenAMP/open-amp/blob/master/lib/remoteproc/elf_loader.c

Quoted 5 lines

Scanning the image for a table seems a bit horrible.

Regards,
Simon

#4 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Hi Fabien,

Quoted 27 lines · Fabien DESSENNE

Hi Simon,

Quoted 15 lines · Simon Glass

Hi Fabien,

Quoted 5 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
  drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
  include/remoteproc.h                  |  70 +++++++++
  test/dm/remoteproc.c                  |  91 ++++++++++--
  3 files changed, 419 insertions(+), 11 deletions(-)
Quoted 2 lines

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

The "resource table" may be located anywhere, there is no strict rule defining where it is expected to be.

Quoted 7 lines

Nevertheless the Linux remoteproc[1] and OpenAmp (running RTOS) [2] frameworks expect the resource table to be stored in a dedicated ELF section. Both of them run some ELF scanning to find out this section.

The proposed patch is for the "ELF section" variant of the resource table. Other variants like binman packing may be proposed as well, both implementations can coexist alongside.

So why not use binman to pack the image and find the components? This is U-Boot, after all.

Quoted 11 lines

BR

Fabien

[1] https://www.kernel.org/doc/Documentation/remoteproc.txt
[2]
https://github.com/OpenAMP/open-amp/blob/master/lib/remoteproc/elf_loader.c

Quoted 2 lines

Scanning the image for a table seems a bit horrible.

Regards,
Simon

#5 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Hi Simon

Quoted 32 lines · Simon Glass

Hi Fabien,

Quoted 29 lines · Fabien DESSENNE

Hi Simon,

Quoted 15 lines · Simon Glass

Hi Fabien,

Quoted 5 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
   drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
   include/remoteproc.h                  |  70 +++++++++
   test/dm/remoteproc.c                  |  91 ++++++++++--
   3 files changed, 419 insertions(+), 11 deletions(-)
Quoted 2 lines

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

The "resource table" may be located anywhere, there is no strict rule defining where it is expected to be.

Nevertheless the Linux remoteproc[1] and OpenAmp (running RTOS) [2] frameworks expect the resource table to be stored in a dedicated ELF section. Both of them run some ELF scanning to find out this section.

Quoted 6 lines
Quoted 3 lines

The proposed patch is for the "ELF section" variant of the resource table. Other variants like binman packing may be proposed as well, both implementations can coexist alongside.

So why not use binman to pack the image and find the components? This is U-Boot, after all.

Packing the firmware together with the other U-Boot components is acceptable if the firmware is controlled only by U-Boot. My requirement is that the coprocessor firmware shall be loaded by U-Boot or by Linux.

You can have a look at [1] for more details on the way this is handled on STM32 MPU. In that case, the .elf firmware is stored in a in File System that can be read by both U-Boot and Linux.

If we have the firmware packed in the image (for U-Boot), we need to have a copy in the FileSystem (for Linux) which would not be a good idea.

BR
Fabien

[1] https://wiki.st.com/stm32mpu/index.php/Boot_chains_overview

Quoted 11 lines
Quoted 9 lines

BR

Fabien

[1] https://www.kernel.org/doc/Documentation/remoteproc.txt
[2]
https://github.com/OpenAMP/open-amp/blob/master/lib/remoteproc/elf_loader.c

Quoted 1 line

Scanning the image for a table seems a bit horrible.

Regards,
Simon

#6 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Hi Fabien,

Quoted 43 lines · Fabien DESSENNE

Hi Simon

Quoted 32 lines · Simon Glass

Hi Fabien,

Quoted 29 lines · Fabien DESSENNE

Hi Simon,

Quoted 15 lines · Simon Glass

Hi Fabien,

Quoted 5 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
   drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
   include/remoteproc.h                  |  70 +++++++++
   test/dm/remoteproc.c                  |  91 ++++++++++--
   3 files changed, 419 insertions(+), 11 deletions(-)
Quoted 2 lines

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

The "resource table" may be located anywhere, there is no strict rule defining where it is expected to be.

Nevertheless the Linux remoteproc[1] and OpenAmp (running RTOS) [2] frameworks expect the resource table to be stored in a dedicated ELF section. Both of them run some ELF scanning to find out this section.

Quoted 6 lines
Quoted 3 lines

The proposed patch is for the "ELF section" variant of the resource table. Other variants like binman packing may be proposed as well, both implementations can coexist alongside.

So why not use binman to pack the image and find the components? This is U-Boot, after all.

Quoted 9 lines

Packing the firmware together with the other U-Boot components is acceptable if the firmware is controlled only by U-Boot. My requirement is that the coprocessor firmware shall be loaded by U-Boot or by Linux.

You can have a look at [1] for more details on the way this is handled on STM32 MPU. In that case, the .elf firmware is stored in a in File System that can be read by both U-Boot and Linux.

Where is the coprocessor firmware stored, then?

Quoted 2 lines

If we have the firmware packed in the image (for U-Boot), we need to have a copy in the FileSystem (for Linux) which would not be a good idea.

What type of filesystem do you use? I don't see any filesystem access in this patch though.

Quoted 5 lines

BR
Fabien

[1] https://wiki.st.com/stm32mpu/index.php/Boot_chains_overview

Regards,
Simon

#7 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

Hi Simon,

Quoted 48 lines · Simon Glass

Hi Fabien,

Quoted 45 lines · Fabien DESSENNE

Hi Simon

Quoted 31 lines · Simon Glass

Hi Fabien,

Quoted 28 lines · Fabien DESSENNE

Hi Simon,

Quoted 15 lines · Simon Glass

Hi Fabien,

Quoted 5 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
    drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
    include/remoteproc.h                  |  70 +++++++++
    test/dm/remoteproc.c                  |  91 ++++++++++--
    3 files changed, 419 insertions(+), 11 deletions(-)
Quoted 2 lines

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

The "resource table" may be located anywhere, there is no strict rule defining where it is expected to be.

Nevertheless the Linux remoteproc[1] and OpenAmp (running RTOS) [2] frameworks expect the resource table to be stored in a dedicated ELF section. Both of them run some ELF scanning to find out this section.

Quoted 6 lines
Quoted 3 lines

The proposed patch is for the "ELF section" variant of the resource table. Other variants like binman packing may be proposed as well, both implementations can coexist alongside.

So why not use binman to pack the image and find the components? This is U-Boot, after all.

Packing the firmware together with the other U-Boot components is acceptable if the firmware is controlled only by U-Boot. My requirement is that the coprocessor firmware shall be loaded by U-Boot or by Linux.

Quoted 5 lines
Quoted 4 lines

You can have a look at [1] for more details on the way this is handled on STM32 MPU. In that case, the .elf firmware is stored in a in File System that can be read by both U-Boot and Linux.

Where is the coprocessor firmware stored, then?

Please have a look to [STM32MP15_Flash_mapping] which describes how u-boot, linux and the coprocessor firmwares can be stored in flash memory.

Both U-boot and Linux use an EXT4 filesystem (eg bootfs partition) to read the coprocessor .elf firmware file.

When it is read (it is then in DDR), U-boot or Linux parses it, in order to copy the resource table and the different ELF segments into appropriate memory addresses.

[STM32MP15_Flash_mapping]
https://wiki.st.com/stm32mpu/wiki/STM32MP15_Flash_mapping

Quoted 5 lines
Quoted 3 lines

If we have the firmware packed in the image (for U-Boot), we need to have a copy in the FileSystem (for Linux) which would not be a good idea.

What type of filesystem do you use? I don't see any filesystem access in this patch though.

As described above, an EXT4 filesystem is a good example.

This patch is not about filesystem but about how the .elf contents available in DDR memory (after is has been loaded from somewhere) is parsed.

Quoted 7 lines
Quoted 5 lines

BR
Fabien

[1] https://wiki.st.com/stm32mpu/index.php/Boot_chains_overview

Regards,
Simon

#8 lore raw

PATCH 1/5 remoteproc: elf_loader: Add elf resource table load support

In reply to Simon Glass

Btw, I sent a v2 for this patch:

https://www.mail-archive.com/u-boot at lists.denx.de/msg346085.html

Quoted 65 lines · Simon Glass

Hi Fabien,

Quoted 40 lines · Fabien DESSENNE

Hi Simon

Quoted 31 lines · Simon Glass

Hi Fabien,

Quoted 28 lines · Fabien DESSENNE

Hi Simon,

Quoted 15 lines · Simon Glass

Hi Fabien,

Quoted 5 lines · Fabien Dessenne

Add rproc_elf_load_rsc_table(), which searches for a resource table in an elf64/elf32 image, and if found, copies it to device memory. Add also the elf32 and elf64 variants of this API.
Add a test for this.

Quoted 7 lines
---
    drivers/remoteproc/rproc-elf-loader.c | 269 ++++++++++++++++++++++++++++++++++
    include/remoteproc.h                  |  70 +++++++++
    test/dm/remoteproc.c                  |  91 ++++++++++--
    3 files changed, 419 insertions(+), 11 deletions(-)
Quoted 2 lines

If you are putting stuff in the image, should you use binman to build the image, then find the contents using the binman tables?

The "resource table" may be located anywhere, there is no strict rule defining where it is expected to be.

Nevertheless the Linux remoteproc[1] and OpenAmp (running RTOS) [2] frameworks expect the resource table to be stored in a dedicated ELF section. Both of them run some ELF scanning to find out this section.

Quoted 6 lines
Quoted 3 lines

The proposed patch is for the "ELF section" variant of the resource table. Other variants like binman packing may be proposed as well, both implementations can coexist alongside.

So why not use binman to pack the image and find the components? This is U-Boot, after all.

Quoted 9 lines

Packing the firmware together with the other U-Boot components is acceptable if the firmware is controlled only by U-Boot. My requirement is that the coprocessor firmware shall be loaded by U-Boot or by Linux.

You can have a look at [1] for more details on the way this is handled on STM32 MPU. In that case, the .elf firmware is stored in a in File System that can be read by both U-Boot and Linux.

Where is the coprocessor firmware stored, then?

Quoted 2 lines

If we have the firmware packed in the image (for U-Boot), we need to have a copy in the FileSystem (for Linux) which would not be a good idea.

What type of filesystem do you use? I don't see any filesystem access in this patch though.

Quoted 4 lines

BR
Fabien

[1] https://wiki.st.com/stm32mpu/index.php/Boot_chains_overview

Regards,
Simon

#9 lore raw

PATCH 2/5 stm32mp1: declare backup register for copro resource table address

In reply to Fabien Dessenne

Use the backup register #17 as coprocessor resource table address.

---
 arch/arm/mach-stm32mp/include/mach/stm32.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm/mach-stm32mp/include/mach/stm32.h +1 -0
index b3e9ccc..4392096 100644
@@ -86,6 +86,7 @@ enum boot_device {
 #define TAMP_BACKUP_REGISTER(x)		(STM32_TAMP_BASE + 0x100 + 4 * x)
 #define TAMP_BACKUP_MAGIC_NUMBER	TAMP_BACKUP_REGISTER(4)
 #define TAMP_BACKUP_BRANCH_ADDRESS	TAMP_BACKUP_REGISTER(5)
+#define TAMP_COPRO_RSC_TBL_ADDRESS	TAMP_BACKUP_REGISTER(17)
 #define TAMP_BOOT_CONTEXT		TAMP_BACKUP_REGISTER(20)
 #define TAMP_BOOTCOUNT			TAMP_BACKUP_REGISTER(21)
 
Signature · 1 line
2.7.4
#10 lore raw

PATCH 3/5 remoteproc: stm32: load resource table from firmware

In reply to Fabien Dessenne

Load the optional resource table from the firmware, and write its address in the dedicated backup register.

---
 drivers/remoteproc/stm32_copro.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/remoteproc/stm32_copro.c +13 -0
index 40bba37..eef3416 100644
@@ -23,6 +23,7 @@
  * @hold_boot_offset:	offset of the register controlling the hold boot setting
  * @hold_boot_mask:	bitmask of the register for the hold boot field
  * @is_running:		is the remote processor running
+ * @rsc_table_addr:	resource table address
  */
 struct stm32_copro_privdata {
 	struct reset_ctl reset_ctl;
@@ -30,6 +31,7 @@ struct stm32_copro_privdata {
 	uint hold_boot_offset;
 	uint hold_boot_mask;
 	bool is_running;
+	ulong rsc_table_addr;
 };
 
 /**
@@ -141,6 +143,7 @@ static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
 {
 	struct stm32_copro_privdata *priv;
+	ulong rsc_table_size;
 	int ret;
 
 	priv = dev_get_priv(dev);
@@ -155,6 +158,12 @@ static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
 		return ret;
 	}
 
+	if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
+				       &rsc_table_size)) {
+		priv->rsc_table_addr = 0;
+		dev_warn(dev, "No valid resource table for this firmware\n");
+	}
+
 	return rproc_elf32_load_image(dev, addr, size);
 }
 
@@ -180,6 +189,10 @@ static int stm32_copro_start(struct udevice *dev)
 	 * rebooting autonomously
 	 */
 	ret = stm32_copro_set_hold_boot(dev, true);
+	if (!ret)
+		/* Store rsc_address in bkp register */
+		writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
+
 	priv->is_running = !ret;
 	return ret;
 }
Signature · 1 line
2.7.4
#11 lore raw

PATCH 3/5 remoteproc: stm32: load resource table from firmware

Hi Fabien,

Quoted 2 lines · Fabien Dessenne

Load the optional resource table from the firmware, and write its address in the dedicated backup register.

What processor is this? Reason I ask is that you are using 0 as a no resource table address, and if it is a valid address for that processor?

regards
Suman

Quoted 59 lines
---
 drivers/remoteproc/stm32_copro.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/remoteproc/stm32_copro.c +13 -0
index 40bba37..eef3416 100644
@@ -23,6 +23,7 @@
  * @hold_boot_offset:	offset of the register controlling the hold boot setting
  * @hold_boot_mask:	bitmask of the register for the hold boot field
  * @is_running:		is the remote processor running
+ * @rsc_table_addr:	resource table address
  */
 struct stm32_copro_privdata {
 	struct reset_ctl reset_ctl;
@@ -30,6 +31,7 @@ struct stm32_copro_privdata {
 	uint hold_boot_offset;
 	uint hold_boot_mask;
 	bool is_running;
+	ulong rsc_table_addr;
 };
 
 /**
@@ -141,6 +143,7 @@ static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
 {
 	struct stm32_copro_privdata *priv;
+	ulong rsc_table_size;
 	int ret;
 
 	priv = dev_get_priv(dev);
@@ -155,6 +158,12 @@ static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
 		return ret;
 	}
 
+	if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
+				       &rsc_table_size)) {
+		priv->rsc_table_addr = 0;
+		dev_warn(dev, "No valid resource table for this firmware\n");
+	}
+
 	return rproc_elf32_load_image(dev, addr, size);
 }
 
@@ -180,6 +189,10 @@ static int stm32_copro_start(struct udevice *dev)
 	 * rebooting autonomously
 	 */
 	ret = stm32_copro_set_hold_boot(dev, true);
+	if (!ret)
+		/* Store rsc_address in bkp register */
+		writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
+
 	priv->is_running = !ret;
 	return ret;
 }
#12 lore raw

PATCH 3/5 remoteproc: stm32: load resource table from firmware

Quoted 7 lines · Suman Anna

Hi Fabien,

Quoted 2 lines · Fabien Dessenne

Load the optional resource table from the firmware, and write its address in the dedicated backup register.

What processor is this? Reason I ask is that you are using 0 as a no resource table address, and if it is a valid address for that processor?

Since on system reset, the vector table of the STM32MP Cortex-M4 co-processor is fixed at address 0, the ResourceTable can't be located here.

ResourceTable address = 0 can be safely interpreted as "No resource table".

BR

Fabien

Quoted 62 lines

regards
Suman

Quoted 58 lines
---
  drivers/remoteproc/stm32_copro.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/remoteproc/stm32_copro.c +13 -0
index 40bba37..eef3416 100644
@@ -23,6 +23,7 @@
   * @hold_boot_offset:	offset of the register controlling the hold boot setting
   * @hold_boot_mask:	bitmask of the register for the hold boot field
   * @is_running:		is the remote processor running
+ * @rsc_table_addr:	resource table address
   */
  struct stm32_copro_privdata {
  	struct reset_ctl reset_ctl;
@@ -30,6 +31,7 @@ struct stm32_copro_privdata {
  	uint hold_boot_offset;
  	uint hold_boot_mask;
  	bool is_running;
+	ulong rsc_table_addr;
  };
 
  /**
@@ -141,6 +143,7 @@ static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
  static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
  {
  	struct stm32_copro_privdata *priv;
+	ulong rsc_table_size;
  	int ret;
 
  	priv = dev_get_priv(dev);
@@ -155,6 +158,12 @@ static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
  		return ret;
  	}
 
+	if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
+				       &rsc_table_size)) {
+		priv->rsc_table_addr = 0;
+		dev_warn(dev, "No valid resource table for this firmware\n");
+	}
+
  	return rproc_elf32_load_image(dev, addr, size);
  }
 
@@ -180,6 +189,10 @@ static int stm32_copro_start(struct udevice *dev)
  	 * rebooting autonomously
  	 */
  	ret = stm32_copro_set_hold_boot(dev, true);
+	if (!ret)
+		/* Store rsc_address in bkp register */
+		writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
+
  	priv->is_running = !ret;
  	return ret;
  }
#13 lore raw

PATCH 4/5 stm32mp1: Fixup the Linux DeviceTree with coprocessor information

In reply to Fabien Dessenne

When the coprocessor has been started, provide the context to Linux kernel so it can handle it:
- update the coprocessor node of kernel DeviceTree with the "early-booted" property.
- write the resource table address in a dedicated backup register.

---
 board/st/stm32mp1/stm32mp1.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
board/st/stm32mp1/stm32mp1.c +13 -3
index 18f9b84..8c669d0 100644
@@ -891,6 +891,7 @@ void board_mtdparts_default(const char **mtdids, const char **mtdparts)
 #if defined(CONFIG_OF_BOARD_SETUP)
 int ft_board_setup(void *blob, bd_t *bd)
 {
+	int off, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
 	struct node_info nodes[] = {
 		{ "st,stm32f469-qspi",		MTD_DEV_TYPE_NOR,  },
@@ -899,6 +900,17 @@ int ft_board_setup(void *blob, bd_t *bd)
 	fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
 #endif
 
+	/* Update DT if coprocessor started */
+	off = fdt_path_offset(blob, "/mlahb/m4 at 10000000");
+	if (off > 0) {
+		if (!rproc_is_running(id)) {
+			fdt_setprop_empty(blob, off, "early-booted");
+		} else {
+			fdt_delprop(blob, off, "early-booted");
+			writel(0, TAMP_COPRO_RSC_TBL_ADDRESS);
+		}
+	}
+
 	return 0;
 }
 #endif
@@ -918,10 +930,8 @@ static void board_copro_image_process(ulong fw_image, size_t fw_size)
 	printf("Load Remote Processor %d with data at addr=0x%08lx %u bytes:%s\n",
 	       id, fw_image, fw_size, ret ? " Failed!" : " Success!");
 
-	if (!ret) {
+	if (!ret)
 		rproc_start(id);
-		env_set("copro_state", "booted");
-	}
 }
 
 U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
Signature · 1 line
2.7.4
#14 lore raw

PATCH 4/5 stm32mp1: Fixup the Linux DeviceTree with coprocessor information

Hi Fabien,

Quoted 4 lines · Fabien Dessenne

When the coprocessor has been started, provide the context to Linux kernel so it can handle it:
- update the coprocessor node of kernel DeviceTree with the "early-booted" property.

Has this property been acked by DT maintainers at the kernel-level? We have used something similar but moving away from it and instead just relying on reading the hardware reset status in the kernel remoteproc driver, and configuring the driver accordingly.

Quoted 1 line

- write the resource table address in a dedicated backup register.

Is this an actual register on the SoC or some block of memory dedicated for sharing information from U-Boot to kernel?

We have the same problem and I am currently going with a design-by-contract approach for early-booted usecases where I am expecting the resource table to be placed at a specific location in memory regions given to remoteproc.

regards
Suman

Quoted 49 lines
---
 board/st/stm32mp1/stm32mp1.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
board/st/stm32mp1/stm32mp1.c +13 -3
index 18f9b84..8c669d0 100644
@@ -891,6 +891,7 @@ void board_mtdparts_default(const char **mtdids, const char **mtdparts)
 #if defined(CONFIG_OF_BOARD_SETUP)
 int ft_board_setup(void *blob, bd_t *bd)
 {
+	int off, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
 	struct node_info nodes[] = {
 		{ "st,stm32f469-qspi",		MTD_DEV_TYPE_NOR,  },
@@ -899,6 +900,17 @@ int ft_board_setup(void *blob, bd_t *bd)
 	fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
 #endif
 
+	/* Update DT if coprocessor started */
+	off = fdt_path_offset(blob, "/mlahb/m4 at 10000000");
+	if (off > 0) {
+		if (!rproc_is_running(id)) {
+			fdt_setprop_empty(blob, off, "early-booted");
+		} else {
+			fdt_delprop(blob, off, "early-booted");
+			writel(0, TAMP_COPRO_RSC_TBL_ADDRESS);
+		}
+	}
+
 	return 0;
 }
 #endif
@@ -918,10 +930,8 @@ static void board_copro_image_process(ulong fw_image, size_t fw_size)
 	printf("Load Remote Processor %d with data at addr=0x%08lx %u bytes:%s\n",
 	       id, fw_image, fw_size, ret ? " Failed!" : " Success!");
 
-	if (!ret) {
+	if (!ret)
 		rproc_start(id);
-		env_set("copro_state", "booted");
-	}
 }
 
 U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
#15 lore raw

PATCH 4/5 stm32mp1: Fixup the Linux DeviceTree with coprocessor information

Hi Suman,

Thank you for your comments.

Quoted 11 lines · Suman Anna

Hi Fabien,

Quoted 4 lines · Fabien Dessenne

When the coprocessor has been started, provide the context to Linux kernel so it can handle it:
- update the coprocessor node of kernel DeviceTree with the "early-booted" property.

Has this property been acked by DT maintainers at the kernel-level? We have used something similar but moving away from it and instead just relying on reading the hardware reset status in the kernel remoteproc driver, and configuring the driver accordingly.

Good point, that "early-booted" property has not been reviewed by the kernel DT maintainers.
This property shall be read by some kernel remoteproc drivers, together with the kernel "remoteproc: add support for preloaded firmware" patchset [1] which is still under review/rework (I expect Loic to send an update version (very) soon).

I will nevertheless rework this u-boot patchset in order to get rid of that property. Instead of, I plan to use some additional HW register to track the processor state.

[1] https://lkml.org/lkml/2018/11/30/157

Quoted 9 lines
Quoted 2 lines

- write the resource table address in a dedicated backup register.

Is this an actual register on the SoC or some block of memory dedicated for sharing information from U-Boot to kernel?

We have the same problem and I am currently going with a design-by-contract approach for early-booted usecases where I am expecting the resource table to be placed at a specific location in memory regions given to remoteproc.

I am interested in having the resource table placed in a specific remoteproc memory region.
For the time being I write the RscTable in a backup register which is an actual SoC register. When the "specific memory region" dev is available, I may consider moving to that alternate implementation.

BR

Fabien

Quoted 52 lines

regards
Suman

Quoted 48 lines
---
  board/st/stm32mp1/stm32mp1.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
board/st/stm32mp1/stm32mp1.c +13 -3
index 18f9b84..8c669d0 100644
@@ -891,6 +891,7 @@ void board_mtdparts_default(const char **mtdids, const char **mtdparts)
  #if defined(CONFIG_OF_BOARD_SETUP)
  int ft_board_setup(void *blob, bd_t *bd)
  {
+	int off, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
  #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  	struct node_info nodes[] = {
  		{ "st,stm32f469-qspi",		MTD_DEV_TYPE_NOR,  },
@@ -899,6 +900,17 @@ int ft_board_setup(void *blob, bd_t *bd)
  	fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  #endif
 
+	/* Update DT if coprocessor started */
+	off = fdt_path_offset(blob, "/mlahb/m4 at 10000000");
+	if (off > 0) {
+		if (!rproc_is_running(id)) {
+			fdt_setprop_empty(blob, off, "early-booted");
+		} else {
+			fdt_delprop(blob, off, "early-booted");
+			writel(0, TAMP_COPRO_RSC_TBL_ADDRESS);
+		}
+	}
+
  	return 0;
  }
  #endif
@@ -918,10 +930,8 @@ static void board_copro_image_process(ulong fw_image, size_t fw_size)
  	printf("Load Remote Processor %d with data at addr=0x%08lx %u bytes:%s\n",
  	       id, fw_image, fw_size, ret ? " Failed!" : " Success!");
 
-	if (!ret) {
+	if (!ret)
  		rproc_start(id);
-		env_set("copro_state", "booted");
-	}
  }
 
  U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
#16 lore raw

PATCH 5/5 remoteproc: stm32: invert the is_running() return value

In reply to Fabien Dessenne

The .is_running() ops expects a return value of 0 if the processor is running, 1 if not running : align to this.

---
 drivers/remoteproc/stm32_copro.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/remoteproc/stm32_copro.c +2 -2
index eef3416..fce9653 100644
@@ -237,14 +237,14 @@ static int stm32_copro_stop(struct udevice *dev)
 /**
  * stm32_copro_is_running() - Is the STM32 remote processor running
  * @dev:	corresponding STM32 remote processor device
- * @return 1 if the remote processor is running, 0 otherwise
+ * @return 0 if the remote processor is running, 1 otherwise
  */
 static int stm32_copro_is_running(struct udevice *dev)
 {
 	struct stm32_copro_privdata *priv;
 
 	priv = dev_get_priv(dev);
-	return priv->is_running;
+	return !priv->is_running;
 }
 
 static const struct dm_rproc_ops stm32_copro_ops = {
Signature · 1 line
2.7.4
#17 lore raw

PATCH 5/5 remoteproc: stm32: invert the is_running() return value

Quoted 4 lines · Fabien Dessenne

The .is_running() ops expects a return value of 0 if the processor is running, 1 if not running : align to this.

This patch should be earlier than patch4, right?

Quoted 26 lines
---
 drivers/remoteproc/stm32_copro.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/remoteproc/stm32_copro.c +2 -2
index eef3416..fce9653 100644
@@ -237,14 +237,14 @@ static int stm32_copro_stop(struct udevice *dev)
 /**
  * stm32_copro_is_running() - Is the STM32 remote processor running
  * @dev:	corresponding STM32 remote processor device
- * @return 1 if the remote processor is running, 0 otherwise
+ * @return 0 if the remote processor is running, 1 otherwise
  */
 static int stm32_copro_is_running(struct udevice *dev)
 {
 	struct stm32_copro_privdata *priv;
 
 	priv = dev_get_priv(dev);
-	return priv->is_running;
+	return !priv->is_running;
 }
 
 static const struct dm_rproc_ops stm32_copro_ops = {
#18 lore raw

PATCH 5/5 remoteproc: stm32: invert the is_running() return value

Quoted 6 lines · Suman Anna
Quoted 4 lines · Fabien Dessenne

The .is_running() ops expects a return value of 0 if the processor is running, 1 if not running : align to this.

This patch should be earlier than patch4, right?

You're probably right. I will double check this in v2.

Quoted 29 lines
Quoted 26 lines
---
  drivers/remoteproc/stm32_copro.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/remoteproc/stm32_copro.c +2 -2
index eef3416..fce9653 100644
@@ -237,14 +237,14 @@ static int stm32_copro_stop(struct udevice *dev)
  /**
   * stm32_copro_is_running() - Is the STM32 remote processor running
   * @dev:	corresponding STM32 remote processor device
- * @return 1 if the remote processor is running, 0 otherwise
+ * @return 0 if the remote processor is running, 1 otherwise
   */
  static int stm32_copro_is_running(struct udevice *dev)
  {
  	struct stm32_copro_privdata *priv;
 
  	priv = dev_get_priv(dev);
-	return priv->is_running;
+	return !priv->is_running;
  }
 
  static const struct dm_rproc_ops stm32_copro_ops = {