Skip to contentSkip to conversation

firmware: add partial read support in request_firmware_into_buf

PATCH Superseded Scott Branden 7 participants 36 messages Aug 22, 2019 – Feb 22, 2020

Subsystems (11)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Scott Branden

author maintainer
#0 lore raw

cover firmware: add partial read support in request_firmware_into_buf

This patch series adds partial read support in request_firmware_into_buf. In order to accept the enhanced API it has been requested that kernel selftests and upstreamed driver utilize the API enhancement and so are included in this patch series.

Also, no tests existed for existing request_firmware_into_buf kernel API. Therefore tests have been created and submitted upstream here:
"[PATCH v2 0/2] firmware: selftest for request_firmware_into_buf"
https://lkml.org/lkml/2019/8/22/1367

The firmware selftests patches here require those patches to be applied first in order for the firmware selftest patches in this series to be valid.

Finally, in this patch series is the addition of a new Broadcom Valkyrie driver utilizing the new request_firmware_into_buf enhanced API.

Scott Branden (7):
fs: introduce kernel_pread_file* support
firmware: add offset to request_firmware_into_buf
test_firmware: add partial read support for request_firmware_into_buf
selftests: firmware: Test partial file reads of
request_firmware_into_buf
bcm-vk: add bcm_vk UAPI
misc: bcm-vk: add Broadcom Valkyrie driver
MAINTAINERS: bcm-vk: Add maintainer for Broadcom Valkyrie Driver

 MAINTAINERS                                   |    7 +
 drivers/base/firmware_loader/firmware.h       |    5 +
 drivers/base/firmware_loader/main.c           |   49 +-
 drivers/misc/Kconfig                          |    1 +
 drivers/misc/Makefile                         |    1 +
 drivers/misc/bcm-vk/Kconfig                   |   16 +
 drivers/misc/bcm-vk/Makefile                  |    7 +
 drivers/misc/bcm-vk/README                    |   29 +
 drivers/misc/bcm-vk/bcm_vk.h                  |  229 +++
 drivers/misc/bcm-vk/bcm_vk_dev.c              | 1558 +++++++++++++++++
 drivers/misc/bcm-vk/bcm_vk_msg.c              |  963 ++++++++++
 drivers/misc/bcm-vk/bcm_vk_msg.h              |  169 ++
 drivers/misc/bcm-vk/bcm_vk_sg.c               |  273 +++
 drivers/misc/bcm-vk/bcm_vk_sg.h               |   60 +
 drivers/soc/qcom/mdt_loader.c                 |    7 +-
 fs/exec.c                                     |   77 +-
 include/linux/firmware.h                      |    8 +-
 include/linux/fs.h                            |   15 +
 include/uapi/linux/misc/bcm_vk.h              |   88 +
 lib/test_firmware.c                           |  139 +-
 .../selftests/firmware/fw_filesystem.sh       |   80 +
 21 files changed, 3744 insertions(+), 37 deletions(-)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/README
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h
 create mode 100644 include/uapi/linux/misc/bcm_vk.h
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 1/7 fs: introduce kernel_pread_file* support

Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file. Existing kernel_read_file functions call new kernel_pread_file functions with offset=0 and flags=KERNEL_PREAD_FLAG_WHOLE.

---
 fs/exec.c          | 77 ++++++++++++++++++++++++++++++++++++----------
 include/linux/fs.h | 15 +++++++++
 2 files changed, 75 insertions(+), 17 deletions(-)
fs/exec.c +60 -17
index f7f6a140856a..21da8683e2c2 100644
@@ -893,10 +893,14 @@ struct file *open_exec(const char *name)
 }
 EXPORT_SYMBOL(open_exec);
 
-int kernel_read_file(struct file *file, void **buf, loff_t *size,
-		     loff_t max_size, enum kernel_read_file_id id)
-{
-	loff_t i_size, pos;
+int kernel_pread_file(struct file *file, void **buf, loff_t *size,
+		      loff_t pos, loff_t max_size, unsigned int flags,
+		      enum kernel_read_file_id id)
+{
+	loff_t alloc_size;
+	loff_t buf_pos;
+	loff_t read_end;
+	loff_t i_size;
 	ssize_t bytes = 0;
 	int ret;
 
@@ -916,21 +920,31 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 		ret = -EINVAL;
 		goto out;
 	}
-	if (i_size > SIZE_MAX || (max_size > 0 && i_size > max_size)) {
+
+	/* Default read to end of file */
+	read_end = i_size;
+
+	/* Allow reading partial portion of file */
+	if ((flags & KERNEL_PREAD_FLAG_PART) &&
+	    (i_size > (pos + max_size)))
+		read_end = pos + max_size;
+
+	alloc_size = read_end - pos;
+	if (i_size > SIZE_MAX || (max_size > 0 && alloc_size > max_size)) {
 		ret = -EFBIG;
 		goto out;
 	}
 
 	if (id != READING_FIRMWARE_PREALLOC_BUFFER)
-		*buf = vmalloc(i_size);
+		*buf = vmalloc(alloc_size);
 	if (!*buf) {
 		ret = -ENOMEM;
 		goto out;
 	}
 
-	pos = 0;
-	while (pos < i_size) {
-		bytes = kernel_read(file, *buf + pos, i_size - pos, &pos);
+	buf_pos = 0;
+	while (pos < read_end) {
+		bytes = kernel_read(file, *buf + buf_pos, read_end - pos, &pos);
 		if (bytes < 0) {
 			ret = bytes;
 			goto out_free;
@@ -938,14 +952,16 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 
 		if (bytes == 0)
 			break;
+
+		buf_pos += bytes;
 	}
 
-	if (pos != i_size) {
+	if (pos != read_end) {
 		ret = -EIO;
 		goto out_free;
 	}
 
-	ret = security_kernel_post_read_file(file, *buf, i_size, id);
+	ret = security_kernel_post_read_file(file, *buf, alloc_size, id);
 	if (!ret)
 		*size = pos;
 
@@ -961,10 +977,20 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 	allow_write_access(file);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(kernel_pread_file);
+
+int kernel_read_file(struct file *file, void **buf, loff_t *size,
+		     loff_t max_size, enum kernel_read_file_id id)
+{
+	return kernel_pread_file(file, buf, size, 0, max_size,
+				 KERNEL_PREAD_FLAG_WHOLE, id);
+}
 EXPORT_SYMBOL_GPL(kernel_read_file);
 
-int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
-			       loff_t max_size, enum kernel_read_file_id id)
+int kernel_pread_file_from_path(const char *path, void **buf,
+				loff_t *size, loff_t pos,
+				loff_t max_size, unsigned int flags,
+				enum kernel_read_file_id id)
 {
 	struct file *file;
 	int ret;
@@ -976,14 +1002,23 @@ int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
 	if (IS_ERR(file))
 		return PTR_ERR(file);
 
-	ret = kernel_read_file(file, buf, size, max_size, id);
+	ret = kernel_pread_file(file, buf, size, pos, max_size, flags, id);
 	fput(file);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(kernel_pread_file_from_path);
+
+int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
+			       loff_t max_size, enum kernel_read_file_id id)
+{
+	return kernel_pread_file_from_path(path, buf, size, 0, max_size,
+					   KERNEL_PREAD_FLAG_WHOLE, id);
+}
 EXPORT_SYMBOL_GPL(kernel_read_file_from_path);
 
-int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
-			     enum kernel_read_file_id id)
+int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size, loff_t pos,
+			      loff_t max_size, unsigned int flags,
+			      enum kernel_read_file_id id)
 {
 	struct fd f = fdget(fd);
 	int ret = -EBADF;
@@ -991,11 +1026,19 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
 	if (!f.file)
 		goto out;
 
-	ret = kernel_read_file(f.file, buf, size, max_size, id);
+	ret = kernel_pread_file(f.file, buf, size, pos, max_size, flags, id);
 out:
 	fdput(f);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(kernel_pread_file_from_fd);
+
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+			     enum kernel_read_file_id id)
+{
+	return kernel_pread_file_from_fd(fd, buf, size, 0, max_size,
+					 KERNEL_PREAD_FLAG_WHOLE, id);
+}
 EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);
 
 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
include/linux/fs.h +15 -0
index 997a530ff4e9..a257e98acc89 100644
@@ -2933,10 +2933,25 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)
 	return kernel_read_file_str[id];
 }
 
+/* Flags used by kernel_pread_file functions */
+#define KERNEL_PREAD_FLAG_WHOLE	0x0000 /* Only Allow reading of whole file */
+#define KERNEL_PREAD_FLAG_PART	0x0001 /* Allow reading part of file */
+
+extern int kernel_pread_file(struct file *file, void **buf, loff_t *size,
+			     loff_t pos, loff_t max_size, unsigned int flags,
+			     enum kernel_read_file_id id);
 extern int kernel_read_file(struct file *, void **, loff_t *, loff_t,
 			    enum kernel_read_file_id);
+extern int kernel_pread_file_from_path(const char *path, void **buf,
+				       loff_t *size, loff_t pos,
+				       loff_t max_size, unsigned int flags,
+				       enum kernel_read_file_id id);
 extern int kernel_read_file_from_path(const char *, void **, loff_t *, loff_t,
 				      enum kernel_read_file_id);
+extern int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size,
+				    loff_t pos, loff_t max_size,
+				    unsigned int flags,
+				    enum kernel_read_file_id id);
 extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t,
 				    enum kernel_read_file_id);
 extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/7] fs: introduce kernel_pread_file* support

Quoted 5 lines · Scott Branden

Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file. Existing kernel_read_file functions call new kernel_pread_file functions with offset=0 and flags=KERNEL_PREAD_FLAG_WHOLE.

Would this change passes the security check like ima?
I thought security_kernel_post_read_file() checks the whole content for calculating the hash...

thanks,

Takashi

author maintainer
#3 lore raw

Re: [PATCH 1/7] fs: introduce kernel_pread_file* support

Hi Takashi

Quoted 9 lines · Takashi Iwai
Quoted 4 lines · Scott Branden

Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file. Existing kernel_read_file functions call new kernel_pread_file functions with offset=0 and flags=KERNEL_PREAD_FLAG_WHOLE.

Would this change passes the security check like ima?
I thought security_kernel_post_read_file() checks the whole content for calculating the hash...

It passes the fw_run_tests.sh.  How do you test the firmware loader passes this security check?

What exactly does this ima do?  How do you enable/disable using it?

Any reasonable device would check the integrity of the firmware image being loaded to it.

And, if part of a security model, authenticate the image.

Whatever security check you are referring to is not needed by request_firmware_into_buf when loading

a partial file into a buffer.

Quoted 5 lines

thanks,

Takashi

maintainer
#4 lore raw

Re: [PATCH 1/7] fs: introduce kernel_pread_file* support

Quoted 9 lines · Scott Branden

Hi Takashi

Quoted 6 lines · Takashi Iwai
Quoted 4 lines · Scott Branden

Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file. Existing kernel_read_file functions call new kernel_pread_file functions with offset=0 and flags=KERNEL_PREAD_FLAG_WHOLE.

Quoted 6 lines
Quoted 3 lines

Would this change passes the security check like ima?
I thought security_kernel_post_read_file() checks the whole content for calculating the hash...

It passes the fw_run_tests.sh.  How do you test the firmware loader passes this security check?

Its not a security check per code, its an audit of the code, to ensure that no new cases are not covered and its why I had CC'd Mimi. The question lies in *if* the approach exposes a new interface which cannot be attested. Its unclear to me if we can attest currently through security modules the fallback interface, as there are not APIs with a respective callback yet.

Luis

author maintainer
#5 lore raw

PATCH 2/7 firmware: add offset to request_firmware_into_buf

In reply to Scott Branden

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

---
 drivers/base/firmware_loader/firmware.h |  5 +++
 drivers/base/firmware_loader/main.c     | 49 +++++++++++++++++--------
 drivers/soc/qcom/mdt_loader.c           |  7 +++-
 include/linux/firmware.h                |  8 +++-
 lib/test_firmware.c                     |  4 +-
 5 files changed, 53 insertions(+), 20 deletions(-)
drivers/base/firmware_loader/firmware.h +5 -0
index 7ecd590e67fe..4b8997e84ceb 100644
@@ -29,6 +29,8 @@
  *	firmware caching mechanism.
  * @FW_OPT_NOFALLBACK: Disable the fallback mechanism. Takes precedence over
  *	&FW_OPT_UEVENT and &FW_OPT_USERHELPER.
+ * @FW_OPT_PARTIAL: Allow partial read of firmware instead of needing to read
+ *	entire file.
  */
 enum fw_opt {
 	FW_OPT_UEVENT =         BIT(0),
@@ -37,6 +39,7 @@ enum fw_opt {
 	FW_OPT_NO_WARN =        BIT(3),
 	FW_OPT_NOCACHE =        BIT(4),
 	FW_OPT_NOFALLBACK =     BIT(5),
+	FW_OPT_PARTIAL =        BIT(6),
 };
 
 enum fw_status {
@@ -64,6 +67,8 @@ struct fw_priv {
 	void *data;
 	size_t size;
 	size_t allocated_size;
+	size_t offset;
+	unsigned int flags;
 #ifdef CONFIG_FW_LOADER_PAGED_BUF
 	bool is_paged_buf;
 	struct page **pages;
drivers/base/firmware_loader/main.c +34 -15

149 lines not on this page · raw message

drivers/soc/qcom/mdt_loader.c +5 -2
index 24cd193dec55..00f3359f4f61 100644
@@ -246,8 +246,11 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
 		} else if (phdr->p_filesz) {
 			/* Firmware not large enough, load split-out segments */
 			sprintf(fw_name + fw_name_len - 3, "b%02d", i);
-			ret = request_firmware_into_buf(&seg_fw, fw_name, dev,
-							ptr, phdr->p_filesz);
+			ret = request_firmware_into_buf
+						(&seg_fw, fw_name, dev,
+						 ptr, phdr->p_filesz,
+						 0,
+						 KERNEL_PREAD_FLAG_WHOLE);
 			if (ret) {
 				dev_err(dev, "failed to load %s\n", fw_name);
 				break;
include/linux/firmware.h +6 -2
index 2dd566c91d44..c81162a8d709 100644
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/compiler.h>
+#include <linux/fs.h>
 #include <linux/gfp.h>
 
 #define FW_ACTION_NOHOTPLUG 0
@@ -51,7 +52,9 @@ int request_firmware_nowait(
 int request_firmware_direct(const struct firmware **fw, const char *name,
 			    struct device *device);
 int request_firmware_into_buf(const struct firmware **firmware_p,
-	const char *name, struct device *device, void *buf, size_t size);
+			      const char *name, struct device *device,
+			      void *buf, size_t size,
+			      size_t offset, unsigned int pread_flags);
 
 void release_firmware(const struct firmware *fw);
 #else
@@ -89,7 +92,8 @@ static inline int request_firmware_direct(const struct firmware **fw,
 }
 
 static inline int request_firmware_into_buf(const struct firmware **firmware_p,
-	const char *name, struct device *device, void *buf, size_t size)
+	const char *name, struct device *device, void *buf, size_t size,
+	size_t offset, unsigned int pread_flags);
 {
 	return -EINVAL;
 }
lib/test_firmware.c +3 -1
index 251213c872b5..7d1d97fa9a23 100644
@@ -622,7 +622,9 @@ static int test_fw_run_batch_request(void *data)
 						    req->name,
 						    req->dev,
 						    test_buf,
-						    TEST_FIRMWARE_BUF_SIZE);
+						    TEST_FIRMWARE_BUF_SIZE,
+						    0,
+						    KERNEL_PREAD_FLAG_WHOLE);
 		if (!req->fw)
 			kfree(test_buf);
 	} else {
Signature · 1 line
2.17.1
maintainer
#6 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

drivers/base/firmware_loader/main.c EXPORT_SYMBOL_GPL(firmware_request_cache); · line 940

Quoted 7 lines · Scott Branden
drivers/base/firmware_loader/main.c +2 -1
@@ -923,16 +936,22 @@ EXPORT_SYMBOL_GPL(firmware_request_cache);
  */
 int
 request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
-			  struct device *device, void *buf, size_t size)
+			  struct device *device, void *buf, size_t size,
+			  size_t offset, unsigned int pread_flags)

This implies you having to change the other callers, and while currently our list of drivers is small, following the history of the firmware API and the long history of debate of *how* we should evolve its API, its preferred we add yet another new caller for this functionality. So please add a new caller, and use EXPORT_SYMBOL_GPL().

And while at it, pleaase use firmware_request_*() as the prefix, as we have want to use that as the instilled prefix. We have yet to complete the rename of the others older callers but its just a matter of time.

So something like: firmware_request_into_buf_offset()

And thanks for adding a test case!

Luis

author maintainer
#7 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Hi Luis,

Quoted 10 lines · Luis Chamberlain

drivers/base/firmware_loader/main.c EXPORT_SYMBOL_GPL(firmware_request_cache); · line 940

Quoted 7 lines · Scott Branden
drivers/base/firmware_loader/main.c +2 -1
@@ -923,16 +936,22 @@ EXPORT_SYMBOL_GPL(firmware_request_cache);
   */
  int
  request_firmware_into_buf(const struct firmware **firmware_p, const char *name,
-			  struct device *device, void *buf, size_t size)
+			  struct device *device, void *buf, size_t size,
+			  size_t offset, unsigned int pread_flags)

This implies you having to change the other callers, and while currently our list of drivers is small,

Yes, the list is small, very small.

There is a single driver making a call to the existing API.

And, the existing API was never tested until I submitted a test case.

And, the maintainer of that driver wanted

to start utilizing my enhanced API instead of the current API.

As such I think it is very reasonable to update the API right now.

Quoted 10 lines

following the history of the firmware API
and the long history of debate of *how* we should evolve its API, its preferred we add yet another new caller for this functionality. So please add a new caller, and use EXPORT_SYMBOL_GPL().

And while at it, pleaase use firmware_request_*() as the prefix, as we have want to use that as the instilled prefix. We have yet to complete the rename of the others older callers but its just a matter of time.

So something like: firmware_request_into_buf_offset()

I would prefer to rename the API at this time given there is only a single user.

Otherwise I would need to duplicate quite a bit in the test code to support testing

the single user of the old api and then enhanced API.

Or, I can leave existing API in place and change the test case to

just test the enhanced API to keep things simpler in the test code?

Quoted 4 lines

And thanks for adding a test case!

Luis

Regards,

 Scott

maintainer
#8 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 10 lines · Scott Branden
Quoted 2 lines · Luis Chamberlain

This implies you having to change the other callers, and while currently our list of drivers is small,

Yes, the list is small, very small.

There is a single driver making a call to the existing API.

And, the maintainer of that driver wanted
to start utilizing my enhanced API instead of the current API.

You mean in the near term future? Your change makes it use the full file. Just checking.

Quoted 1 line

As such I think it is very reasonable to update the API right now.

I'd prefer to see it separate, and we fix the race *before* we introduce the new functionality. I'll be poking at that shortly but I should note that I leave on vacation this weekend and won't be back for a good while. I already have an idea of how to approach this.

When the current user want to use the new API it can do so, and then we just kill the older caller.

Quoted 11 lines
Quoted 10 lines

following the history of the firmware API
and the long history of debate of *how* we should evolve its API, its preferred we add yet another new caller for this functionality. So please add a new caller, and use EXPORT_SYMBOL_GPL().

And while at it, pleaase use firmware_request_*() as the prefix, as we have want to use that as the instilled prefix. We have yet to complete the rename of the others older callers but its just a matter of time.

So something like: firmware_request_into_buf_offset()

Quoted 8 lines

I would prefer to rename the API at this time given there is only a single user.

Otherwise I would need to duplicate quite a bit in the test code to support testing the single user of the old api and then enhanced API.

Or, I can leave existing API in place and change the test case to just test the enhanced API to keep things simpler in the test code?

If the new user is going to move to the API once available I will be happy to then leave out testing for the older API. That would make sense.

But if you do want to keep testing for the old API, and allow an easy removal for it on the test driver, wouldn't a function pointer suffice for which API call to use based on a boolean?

But yeah if we're going to abandon the old mechanism I'm happy to skip its testing.

Luis

author maintainer
#9 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Hi Luis,

Quoted 4 lines · Luis Chamberlain
Quoted 3 lines · Scott Branden
Quoted 2 lines · Luis Chamberlain

This implies you having to change the other callers, and while currently our list of drivers is small,

Quoted 8 lines
Quoted 6 lines

Yes, the list is small, very small.

There is a single driver making a call to the existing API.

And, the maintainer of that driver wanted
to start utilizing my enhanced API instead of the current API.

You mean in the near term future? Your change makes it use the full file. Just checking.

The change in the patch keeps the existing functionality in the

qcom mdt_loader by reading the full file using the enhanced api.

I don't know when Bjorn will switch to use the partial firmware load:

https://lkml.org/lkml/2019/5/27/9

Quoted 9 lines
Quoted 2 lines

As such I think it is very reasonable to update the API right now.

I'd prefer to see it separate, and we fix the race *before* we introduce the new functionality. I'll be poking at that shortly but I should note that I leave on vacation this weekend and won't be back for a good while. I already have an idea of how to approach this.

When the current user want to use the new API it can do so, and then we just kill the older caller.

We can kill the older api right now as my patch in qcom mdt_loader

calls the new API which allows reading of full or partial files?

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines

following the history of the firmware API
and the long history of debate of *how* we should evolve its API, its preferred we add yet another new caller for this functionality. So please add a new caller, and use EXPORT_SYMBOL_GPL().

And while at it, pleaase use firmware_request_*() as the prefix, as we have want to use that as the instilled prefix. We have yet to complete the rename of the others older callers but its just a matter of time.

So something like: firmware_request_into_buf_offset()

I would prefer to rename the API at this time given there is only a single user.

Quoted 7 lines
Quoted 4 lines

Otherwise I would need to duplicate quite a bit in the test code to support testing the single user of the old api and then enhanced API. Or, I can leave existing API in place and change the test case to just test the enhanced API to keep things simpler in the test code?

If the new user is going to move to the API once available I will be happy to then leave out testing for the older API. That would make sense.

I have switched the single user of the existing api to the new

API in the patch already?  And both full and partial reads using

the new API are tested with this patch series.  If you really insist

on keeping the old API for a single user I can drop that change from the

patch series and have the old request_firmware_api call simply

be a wrapper calling the new API.

Quoted 7 lines

But if you do want to keep testing for the old API, and allow an easy removal for it on the test driver, wouldn't a function pointer suffice for which API call to use based on a boolean?

But yeah if we're going to abandon the old mechanism I'm happy to skip its te

We can skip right now then.  As enhanced API is a superset of old API.

If you want the old API left in place I can just add the wrapper described and

only test the newly named function and thus indirectly test the old

request_firmware_into_buf.

Quoted 3 lines

sting.

Luis

maintainer
#10 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 7 lines · Scott Branden
Quoted 6 lines · Luis Chamberlain
Quoted 5 lines · Scott Branden
Quoted 2 lines · Luis Chamberlain

This implies you having to change the other callers, and while currently our list of drivers is small,

Yes, the list is small, very small.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

There is a single driver making a call to the existing API.

And, the maintainer of that driver wanted
to start utilizing my enhanced API instead of the current API.

You mean in the near term future? Your change makes it use the full file. Just checking.

The change in the patch keeps the existing functionality in the

BTW for some reason your mailer keeps adding new lines per each line. I trim them below. Also for future emails please Cc:

Mimi Zohar <[email protected]>

As she'll be interested in some of this from the IMA security perspective.

Quoted 4 lines

qcom mdt_loader by reading the full file using the enhanced api. I don't know when Bjorn will switch to use the partial firmware load:

https://lkml.org/lkml/2019/5/27/9

OK I see he did he liked the approach. OK thanks! This will make evolutions much easier.

Quoted 11 lines
Quoted 8 lines
Quoted 1 line

As such I think it is very reasonable to update the API right now.

I'd prefer to see it separate, and we fix the race *before* we introduce the new functionality. I'll be poking at that shortly but I should note that I leave on vacation this weekend and won't be back for a good while. I already have an idea of how to approach this.

When the current user want to use the new API it can do so, and then we just kill the older caller.

We can kill the older api right now as my patch in qcom mdt_loader calls the new API which allows reading of full or partial files?

Yes its possible, but more on this below.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

following the history of the firmware API
and the long history of debate of *how* we should evolve its API, its preferred we add yet another new caller for this functionality. So please add a new caller, and use EXPORT_SYMBOL_GPL().

And while at it, pleaase use firmware_request_*() as the prefix, as we have want to use that as the instilled prefix. We have yet to complete the rename of the others older callers but its just a matter of time.

So something like: firmware_request_into_buf_offset()

I would prefer to rename the API at this time given there is only a single user.

Otherwise I would need to duplicate quite a bit in the test code to support testing the single user of the old api and then enhanced API. Or, I can leave existing API in place and change the test case to just test the enhanced API to keep things simpler in the test code?

Quoted 6 lines
Quoted 3 lines

If the new user is going to move to the API once available I will be happy to then leave out testing for the older API. That would make sense.

I have switched the single user of the existing api to the new API in the patch already?

Right, but in the new approach you'd use a newer function name with the new feature.

Quoted 4 lines

And both full and partial reads using the new API are tested with this patch series.  If you really insist on keeping the old API for a single user I can drop that change from the patch series and have the old request_firmware_api call simply be a wrapper calling the new API.

Yes please.

Quoted 11 lines
Quoted 6 lines

But if you do want to keep testing for the old API, and allow an easy removal for it on the test driver, wouldn't a function pointer suffice for which API call to use based on a boolean?

But yeah if we're going to abandon the old mechanism I'm happy to skip its testing.

We can skip right now then.  As enhanced API is a superset of old API. If you want the old API left in place I can just add the wrapper described and only test the newly named function and thus indirectly test the old request_firmware_into_buf.

Yes this makes sense. But I want to take a bit step back right now and think about this a bit more. I'm starting to wonder if this whole sysfs stuff should be replaced with a better scalable scheme. Consider all the fancy things you can do in userspace with a block device. Offsets are already supported, and so much more. So I'm starting to think that the firmware fallback upload sysfs interface is much better suited as a really simple block device long term.

I understand you want your solutions addressed upstream yesterday, but this is the *sort of review* on architecture that should have been done for the request_firmware_into_buf() long ago. But since you probably don't want to wait for a revamp of the interface, a middle ground might be in order for now, with the roadmap put in place to evaluate scalable alternatives.

Either way, we should consider the current bug you ran into for the solutions put forward, with the new functionality you are proposing.

The core of the issue you ran into was the duplicate named kobjects, which are reflected also on the sysfs hierarchy. The directory name created for each firmware request, when duplicate entries exist for one device collide. Upon a secondary request for firmware using the fallback interface, the kobject/directory already exists.

Its easier to understand this from a directory hierarchy perspective. For instance the test driver uses:

/sys/devices/virtual/misc/test_firmware/

The test script for the test_firmware driver uses:

DIR=/sys/devices/virtual/misc/test_firmware/

To load firmware we use a directory underneath this firmware name for the file name of the firmware requested, so to load firmware called $name on the test script we use:

echo 1 >"$DIR"/"$name"/loading
cat "$file" >"$DIR"/"$name"/data
echo 0 >"$DIR"/"$name"/loading

An issue no one has cared for, and also we have not hit yet is that, this implies no firmware names can be used which match other sysfs attributes exported by a driver. I'm not too concerned for this right now, but it is a worthy thing to consider long term under a new solution.

So the issue is that the firmware loader will try to create two equally named entries underneath the firmware loader directory. Yes we can sledge hammer the API to act serially, but this is will just just move one problem to another, your secondary call would have to wait until the first one not only completes the call, but also release_firmware() is called.

I'm looking at using a device name prefix if we do add a new API or functionality. This way userspace can expend and knows what extra tag to use other than the driver name.

Luis

author maintainer
#11 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Hi Luis,

Thanks for helping on this.

Enjoy your time off an we can work on it when you're back.

comments below.

Quoted 59 lines · Luis Chamberlain
Quoted 7 lines · Scott Branden
Quoted 6 lines · Luis Chamberlain
Quoted 5 lines · Scott Branden
Quoted 2 lines · Luis Chamberlain

This implies you having to change the other callers, and while currently our list of drivers is small,

Yes, the list is small, very small.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

There is a single driver making a call to the existing API.

And, the maintainer of that driver wanted
to start utilizing my enhanced API instead of the current API.

You mean in the near term future? Your change makes it use the full file. Just checking.

The change in the patch keeps the existing functionality in the

BTW for some reason your mailer keeps adding new lines per each line. I trim them below. Also for future emails please Cc:

Mimi Zohar <[email protected]>

As she'll be interested in some of this from the IMA security perspective.

Quoted 4 lines

qcom mdt_loader by reading the full file using the enhanced api. I don't know when Bjorn will switch to use the partial firmware load:

https://lkml.org/lkml/2019/5/27/9

OK I see he did he liked the approach. OK thanks! This will make evolutions much easier.

Quoted 10 lines
Quoted 8 lines
Quoted 1 line

As such I think it is very reasonable to update the API right now.

I'd prefer to see it separate, and we fix the race *before* we introduce the new functionality. I'll be poking at that shortly but I should note that I leave on vacation this weekend and won't be back for a good while. I already have an idea of how to approach this.

When the current user want to use the new API it can do so, and then we just kill the older caller.

We can kill the older api right now as my patch in qcom mdt_loader calls the new API which allows reading of full or partial files?

Yes its possible, but more on this below.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

following the history of the firmware API
and the long history of debate of *how* we should evolve its API, its preferred we add yet another new caller for this functionality. So please add a new caller, and use EXPORT_SYMBOL_GPL().

And while at it, pleaase use firmware_request_*() as the prefix, as we have want to use that as the instilled prefix. We have yet to complete the rename of the others older callers but its just a matter of time.

So something like: firmware_request_into_buf_offset()

I would prefer to rename the API at this time given there is only a single user.

Otherwise I would need to duplicate quite a bit in the test code to support testing the single user of the old api and then enhanced API. Or, I can leave existing API in place and change the test case to just test the enhanced API to keep things simpler in the test code?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

If the new user is going to move to the API once available I will be happy to then leave out testing for the older API. That would make sense.

I have switched the single user of the existing api to the new API in the patch already?

Right, but in the new approach you'd use a newer function name with the new feature.

Yes, I will send a new version with a new function name.

firmware_request_into_buf() is more appropriate than firmware_request_into_buf_offset() though.

The function accepts both partial or full file requests with or without an offset into the file.

Quoted 6 lines
Quoted 5 lines

And both full and partial reads using the new API are tested with this patch series.  If you really insist on keeping the old API for a single user I can drop that change from the patch series and have the old request_firmware_api call simply be a wrapper calling the new API.

Yes please.

Sure, if you want me to remove the change to the existing qcom driver to keep using the old api as well I'll do so.

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

But if you do want to keep testing for the old API, and allow an easy removal for it on the test driver, wouldn't a function pointer suffice for which API call to use based on a boolean?

But yeah if we're going to abandon the old mechanism I'm happy to skip its testing.

We can skip right now then.  As enhanced API is a superset of old API. If you want the old API left in place I can just add the wrapper described and only test the newly named function and thus indirectly test the old request_firmware_into_buf.

Quoted 5 lines

Yes this makes sense. But I want to take a bit step back right now and think about this a bit more. I'm starting to wonder if this whole sysfs stuff should be replaced with a better scalable scheme. Consider all the fancy things you can do in userspace with a block device. Offsets are already supported, and so much more.

Yes, if normal file operations worked in kernel space all would be good.

Quoted 9 lines

So I'm starting to think that the
firmware fallback upload sysfs interface is much better suited as a really simple block device long term.
I understand you want your solutions addressed upstream yesterday, but this is the *sort of review* on architecture that should have been done for the request_firmware_into_buf() long ago. But since you probably don't want to wait for a revamp of the interface, a middle ground might be in order for now, with the roadmap put in place to evaluate scalable alternatives.

Sounds very reasonable.

All I wish to do is request part of file into a pre-allocated memory location.

Quoted 44 lines

Either way, we should consider the current bug you ran into for the solutions put forward, with the new functionality you are proposing.

The core of the issue you ran into was the duplicate named kobjects, which are reflected also on the sysfs hierarchy. The directory name created for each firmware request, when duplicate entries exist for one device collide. Upon a secondary request for firmware using the fallback interface, the kobject/directory already exists.

Its easier to understand this from a directory hierarchy perspective. For instance the test driver uses:

/sys/devices/virtual/misc/test_firmware/

The test script for the test_firmware driver uses:

DIR=/sys/devices/virtual/misc/test_firmware/

To load firmware we use a directory underneath this firmware name for the file name of the firmware requested, so to load firmware called $name on the test script we use:

echo 1 >"$DIR"/"$name"/loading
cat "$file" >"$DIR"/"$name"/data
echo 0 >"$DIR"/"$name"/loading

An issue no one has cared for, and also we have not hit yet is that, this implies no firmware names can be used which match other sysfs attributes exported by a driver. I'm not too concerned for this right now, but it is a worthy thing to consider long term under a new solution.

So the issue is that the firmware loader will try to create two equally named entries underneath the firmware loader directory. Yes we can sledge hammer the API to act serially, but this is will just just move one problem to another, your secondary call would have to wait until the first one not only completes the call, but also release_firmware() is called.

I'm looking at using a device name prefix if we do add a new API or functionality. This way userspace can expend and knows what extra tag to use other than the driver name.

Luis

#12 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

In reply to Scott Branden
Quoted 4 lines · Scott Branden

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right? Also it won't work for the compressed firmware files as-is.

So this new API usage is for the limited use cases, hence it needs such checks and returns error/warns if the condition isn't met.

IOW, this can't be a simple extension of request_firmware_into_buf() to pass a new flag.

thanks,

Takashi

author maintainer
#13 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Hi Takashi,

Thanks for review.  comments below.

Quoted 6 lines · Takashi Iwai
Quoted 3 lines · Scott Branden

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

Quoted 1 line

Also it won't work for the compressed firmware files as-is.

Although unnecessary, seems to work fine in the fw_run_tests.sh with "both" and "xzonly" options.

Quoted 11 lines

So this new API usage is for the limited use cases, hence it needs such checks and returns error/warns if the condition isn't met.

IOW, this can't be a simple extension of request_firmware_into_buf() to pass a new flag.

thanks,

Takashi

#14 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 5 lines · Scott Branden

Hi Takashi,

Thanks for review.  comments below.

Quoted 8 lines
Quoted 6 lines · Takashi Iwai
Quoted 3 lines · Scott Branden

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

But how? You patch doesn't change anything about the fallback loading mechanism. Or, if the expected behavior is to load the whole content and then copy a part, what's the merit of this API?

Quoted 3 lines
Quoted 1 line

Also it won't work for the compressed firmware files as-is.

Although unnecessary, seems to work fine in the fw_run_tests.sh with "both" and "xzonly" options.

This looks also suspicious. Loading a part of the file from the middle and decompression won't work together, from obvious reasons.

If the test passes, it means that the test itself is more likely incorrect, I'm afraid.

thanks,

Takashi

author maintainer
#15 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

HI Takashi,

Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Scott Branden

Hi Takashi,

Thanks for review.  comments below.

Quoted 2 lines · Takashi Iwai

On Thu, 22 Aug 2019 21:24:46 +0200,
Scott Branden wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

But how? You patch doesn't change anything about the fallback loading mechanism.

Correct - I didn't change any of the underlying mechanisms, so however request_firmware_into_buf worked before it still does.

Quoted 2 lines

Or, if the expected behavior is to load the whole content
and then copy a part, what's the merit of this API?

The merit of the API is that the entire file is not copied into a buffer. In my use case, the buffer is a memory region in PCIe space that isn't even large enough for the whole file.  So the only way to get the file is to read it in portions.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Also it won't work for the compressed firmware files as-is.

Although unnecessary, seems to work fine in the fw_run_tests.sh with "both" and "xzonly" options.

This looks also suspicious. Loading a part of the file from the middle and decompression won't work together, from obvious reasons.

I don't know what the underlying mechanisms are doing right now. If they decompress the whole file then that is why it's working. An obvious improvement that could be made later is to only read a portion of the file before writing it into the buffer in the non-xz case.

Quoted 3 lines

If the test passes, it means that the test itself is more likely incorrect, I'm afraid.

Then all of the tests for "both" and "xzonly" could be broken.

Quoted 5 lines

thanks,

Takashi

Regards,
 Scott

#16 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 16 lines · Scott Branden

HI Takashi,

Quoted 12 lines · Takashi Iwai
Quoted 10 lines · Scott Branden

Hi Takashi,

Thanks for review.  comments below.

Quoted 5 lines · Takashi Iwai
Quoted 3 lines · Scott Branden

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

But how? You patch doesn't change anything about the fallback loading mechanism.

Correct - I didn't change any of the underlying mechanisms, so however request_firmware_into_buf worked before it still does.

But how? That's the question.

If I understand correctly, essentially your patch changes the call of kernel_read_file_from_path() with additional offset and partial size parameters. i.e. the partial read behavior itself purely relies on the kernel_read_file_from_path().
And, if the file isn't read via this function, the f/w loader falls back to the UMH. Since fallback.c has no idea about the partial read, it shall return the full content of the file. Then this must contradict against the expected result, no?

Quoted 7 lines
Quoted 2 lines

Or, if the expected behavior is to load the whole content
and then copy a part, what's the merit of this API?

The merit of the API is that the entire file is not copied into a buffer. In my use case, the buffer is a memory region in PCIe space that isn't even large enough for the whole file.  So the only way to get the file is to read it
in portions.

But you read not in portions but the whole content, in the case of fallback mode...

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Also it won't work for the compressed firmware files as-is.

Although unnecessary, seems to work fine in the fw_run_tests.sh with "both" and "xzonly" options.

This looks also suspicious. Loading a part of the file from the middle and decompression won't work together, from obvious reasons.

I don't know what the underlying mechanisms are doing right now. If they decompress the whole file then that is why it's working.

No, it shouldn't be a complete read. As already mentioned, the patch changes only the call pattern of kernel_read_file_from_path(). The decompression is done after that, so it must be applied to the partially read content which cannot be decompressed properly.

Quoted 6 lines

An obvious improvement that could be made later is to only read a portion of the file before writing it into the buffer in the non-xz case.

Quoted 2 lines

If the test passes, it means that the test itself is more likely incorrect, I'm afraid.

Then all of the tests for "both" and "xzonly" could be broken.

I suspect that the fallback test is also broken.

thanks,

Takashi

#17 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

In reply to Scott Branden
Quoted 22 lines · Scott Branden

HI Takashi,

Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Scott Branden

Hi Takashi,

Thanks for review.  comments below.

Quoted 2 lines · Takashi Iwai

On Thu, 22 Aug 2019 21:24:46 +0200,
Scott Branden wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

But how? You patch doesn't change anything about the fallback loading mechanism.

Correct - I didn't change any of the underlying mechanisms, so however request_firmware_into_buf worked before it still does.

Quoted 7 lines
Quoted 2 lines

Or, if the expected behavior is to load the whole content
and then copy a part, what's the merit of this API?

The merit of the API is that the entire file is not copied into a buffer. In my use case, the buffer is a memory region in PCIe space that isn't even large enough for the whole file.  So the only way to get the file is to read it
in portions.

BTW: does the use case above mean that the firmware API directly writes onto the given PCI iomem region? If so, I'm not sure whether it would work as expected on all architectures. There must be a reason of the presence of iomem-related API like memcpy_toio()...

thanks,

Takashi

author maintainer
#18 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Hi Takashi,

Quoted 30 lines · Takashi Iwai
Quoted 21 lines · Scott Branden

HI Takashi,

Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Scott Branden

Hi Takashi,

Thanks for review.  comments below.

Quoted 2 lines · Takashi Iwai

On Thu, 22 Aug 2019 21:24:46 +0200,
Scott Branden wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

But how? You patch doesn't change anything about the fallback loading mechanism.

Correct - I didn't change any of the underlying mechanisms, so however request_firmware_into_buf worked before it still does.

Quoted 7 lines
Quoted 2 lines

Or, if the expected behavior is to load the whole content
and then copy a part, what's the merit of this API?

The merit of the API is that the entire file is not copied into a buffer. In my use case, the buffer is a memory region in PCIe space that isn't even large enough for the whole file.  So the only way to get the file is to read it
in portions.

Quoted 4 lines

BTW: does the use case above mean that the firmware API directly writes onto the given PCI iomem region? If so, I'm not sure whether it would work as expected on all architectures. There must be a reason of the presence of iomem-related API like memcpy_toio()...

Yes, we access the PCI region directly in the driver and thus also through request_firmware_into_buf.
I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated. But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

Quoted 5 lines

thanks,

Takashi

#19 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 34 lines · Scott Branden

Hi Takashi,

Quoted 30 lines · Takashi Iwai
Quoted 28 lines · Scott Branden

HI Takashi,

Quoted 9 lines · Takashi Iwai
Quoted 7 lines · Scott Branden

Hi Takashi,

Thanks for review.  comments below.

Quoted 2 lines · Takashi Iwai

On Thu, 22 Aug 2019 21:24:46 +0200,
Scott Branden wrote:

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Add offset to request_firmware_into_buf to allow for portions of firmware file to be read into a buffer. Necessary where firmware needs to be loaded in portions from file in memory constrained systems.

AFAIU, this won't work with the fallback user helper, right?

Seems to work fine in the fw_run_tests.sh with fallbacks.

But how? You patch doesn't change anything about the fallback loading mechanism.

Correct - I didn't change any of the underlying mechanisms, so however request_firmware_into_buf worked before it still does.

Quoted 2 lines

Or, if the expected behavior is to load the whole content
and then copy a part, what's the merit of this API?

The merit of the API is that the entire file is not copied into a buffer. In my use case, the buffer is a memory region in PCIe space that isn't even large enough for the whole file.  So the only way to get the file is to read it
in portions.

Quoted 6 lines
Quoted 4 lines

BTW: does the use case above mean that the firmware API directly writes onto the given PCI iomem region? If so, I'm not sure whether it would work as expected on all architectures. There must be a reason of the presence of iomem-related API like memcpy_toio()...

Yes, we access the PCI region directly in the driver and thus also through request_firmware_into_buf.

Then you really need to access via the standard APIs for iomem. The normal memory copy would work only on some architectures like x86.

Quoted 5 lines

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

thanks,

Takashi

maintainer
#20 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 11 lines · Takashi Iwai
Quoted 6 lines · Scott Branden

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

Poke Scott.

Luis

author maintainer
#21 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 8 lines · Luis Chamberlain
Quoted 7 lines · Takashi Iwai
Quoted 5 lines · Scott Branden

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

Quoted 5 lines
Quoted 2 lines

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

Poke Scott.

Luis

Yes, this is exclusive to x86.
In particular, 64-bit x86 server class machines with PCIe gen3 support.
There is no reason for these PCIe boards to run in other lower end machines or architectures.

maintainer
#22 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 12 lines · Scott Branden
Quoted 11 lines · Luis Chamberlain
Quoted 9 lines · Takashi Iwai
Quoted 5 lines · Scott Branden

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

Quoted 6 lines
Quoted 1 line

Poke Scott.

Yes, this is exclusive to x86.
In particular, 64-bit x86 server class machines with PCIe gen3 support. There is no reason for these PCIe boards to run in other lower end machines or architectures.

It doesn't really matter that much what you expect your customers to do with your product, or what works a particular machine today, drivers should generally be written in a portable manner anyway and use the documented APIs. memcpy() into an __iomem pointer is not portable and while it probably works on any x86 machine today, please just don't do it. If you use 'sparse' to check your code, that would normally result in an address space warning, unless you add __force and a long comment explaining why you cannot just use memcpy_to_io() instead. At that point, you are already better off usingn memcpy_to_io() ;-)

Arnd

author maintainer
#23 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 18 lines · Arnd Bergmann
Quoted 9 lines · Scott Branden
Quoted 8 lines · Luis Chamberlain
Quoted 7 lines · Takashi Iwai
Quoted 5 lines · Scott Branden

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

Poke Scott.

Yes, this is exclusive to x86.
In particular, 64-bit x86 server class machines with PCIe gen3 support. There is no reason for these PCIe boards to run in other lower end machines or architectures.

Quoted 9 lines

It doesn't really matter that much what you expect your customers to do with your product, or what works a particular machine today, drivers should generally be written in a portable manner anyway and use the documented APIs. memcpy() into an __iomem pointer is not portable and while it probably works on any x86 machine today, please just don't do it. If you use 'sparse' to check your code, that would normally result in an address space warning, unless you add __force and a long comment explaining why you cannot just use memcpy_to_io() instead. At that point, you are already better off usingn memcpy_to_io() ;-)

We don't want to allocate to intermediate memory and do another memcpy just to write to pcie. I will have to look into the linux request_firmware_info_buf code and detect whether the buf being request to is in kernel or io memory and perform the operation there.  Hopefully such is possible.

Quoted 2 lines

Arnd

author maintainer
#24 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

In reply to Arnd Bergmann

Hi Arnd,

Quoted 28 lines · Arnd Bergmann
Quoted 9 lines · Scott Branden
Quoted 8 lines · Luis Chamberlain
Quoted 7 lines · Takashi Iwai
Quoted 5 lines · Scott Branden

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

Poke Scott.

Yes, this is exclusive to x86.
In particular, 64-bit x86 server class machines with PCIe gen3 support. There is no reason for these PCIe boards to run in other lower end machines or architectures.

It doesn't really matter that much what you expect your customers to do with your product, or what works a particular machine today, drivers should generally be written in a portable manner anyway and use the documented APIs. memcpy() into an __iomem pointer is not portable and while it probably works on any x86 machine today, please just don't do it. If you use 'sparse' to check your code, that would normally result in an address space warning, unless you add __force and a long comment explaining why you cannot just use memcpy_to_io() instead. At that point, you are already better off usingn memcpy_to_io() ;-)

Quoted 1 line

Arnd

I am a not performing a memcpy at all right now.
I am calling a request_firmware_into_buf call and do not need to make a copy.
This function eventually calls kernel_read_file, which then makes at indirect call in __vfs_read to perform the read to memory. From there I am lost as to what operation happens to achieve this.
The read function would need to detect the buf is in io space and perform the necessary operation.
Anyone with any knowledge on how to make this read to io space would be appreciated?

ssize_t __vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
    if (file->f_op->read)
        return file->f_op->read(file, buf, count, pos); else if (file->f_op->read_iter)
        return new_sync_read(file, buf, count, pos);
    else
        return -EINVAL;
}

ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) {
    mm_segment_t old_fs;
    ssize_t result;

    old_fs = get_fs();
    set_fs(KERNEL_DS);
    /* The cast to a user pointer is valid due to the set_fs() */ result = vfs_read(file, (void __user *)buf, count, pos); set_fs(old_fs);
    return result;
}

maintainer
#25 lore raw

Re: [PATCH 2/7] firmware: add offset to request_firmware_into_buf

Quoted 29 lines · Scott Branden
Quoted 28 lines · Arnd Bergmann
Quoted 9 lines · Scott Branden
Quoted 8 lines · Luis Chamberlain
Quoted 7 lines · Takashi Iwai
Quoted 5 lines · Scott Branden

I will admit I am not familiar with every subtlety of PCI accesses. Any comments to the Valkyrie driver in this patch series are appreciated.
But not all drivers need to work on all architectures. I can add a depends on x86 64bit architectures to the driver to limit it to such.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

But it's an individual board on PCIe, and should work no matter which architecture is? Or is this really exclusive to x86?

Poke Scott.

Yes, this is exclusive to x86.
In particular, 64-bit x86 server class machines with PCIe gen3 support. There is no reason for these PCIe boards to run in other lower end machines or architectures.

It doesn't really matter that much what you expect your customers to do with your product, or what works a particular machine today, drivers should generally be written in a portable manner anyway and use the documented APIs. memcpy() into an __iomem pointer is not portable and while it probably works on any x86 machine today, please just don't do it. If you use 'sparse' to check your code, that would normally result in an address space warning, unless you add __force and a long comment explaining why you cannot just use memcpy_to_io() instead. At that point, you are already better off usingn memcpy_to_io() ;-)

Quoted 6 lines
Quoted 1 line

Arnd

I am a not performing a memcpy at all right now.
I am calling a request_firmware_into_buf call and do not need to make a copy.
This function eventually calls kernel_read_file, which then makes at indirect call in __vfs_read to perform the read to memory.

Well, that comes down to a memcpy() in the end, even if you don't spell it like that in your driver. It may be a copy_from_user(), but clearly not a memcpy_to_io().

Quoted 5 lines

From there I am lost as to what operation happens to achieve this.
The read function would need to detect the buf is in io space and perform the necessary operation.
Anyone with any knowledge on how to make this read to io space would be appreciated?

I don't think modifying the common code is helpful in this case:
any access to PCI MMIO space is inevitably going to be slow, so an extra memcpy() in your driver is not going to cause any noticeable overhead, but the generic functions are meant to be fast for the normal use case and not gain any other features.

Arnd

author maintainer
#26 lore raw

PATCH 3/7 test_firmware: add partial read support for request_firmware_into_buf

In reply to Scott Branden

Add additional hooks to test_firmware to pass in support for partial file read using request_firmware_into_buf.
buf_size: size of buffer to request firmware into
partial: indicates that a partial file request is being made
file_offset: to indicate offset into file to request

---
 lib/test_firmware.c | 141 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 138 insertions(+), 3 deletions(-)
lib/test_firmware.c +138 -3
index 7d1d97fa9a23..6050d3113f92 100644
@@ -49,6 +49,9 @@ struct test_batched_req {
  * @name: the name of the firmware file to look for
  * @into_buf: when the into_buf is used if this is true
  *	request_firmware_into_buf() will be used instead.
+ * @buf_size: size of buf to allocate when into_buf is true
+ * @file_offset: file offset to request when calling request_firmware_into_buf
+ * @partial: partial read flag value when calling request_firmware_into_buf
  * @sync_direct: when the sync trigger is used if this is true
  *	request_firmware_direct() will be used instead.
  * @send_uevent: whether or not to send a uevent for async requests
@@ -88,6 +91,9 @@ struct test_batched_req {
 struct test_config {
 	char *name;
 	bool into_buf;
+	size_t buf_size;
+	size_t file_offset;
+	bool partial;
 	bool sync_direct;
 	bool send_uevent;
 	u8 num_requests;
@@ -182,6 +188,9 @@ static int __test_firmware_config_init(void)
 	test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
 	test_fw_config->send_uevent = true;
 	test_fw_config->into_buf = false;
+	test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE;
+	test_fw_config->file_offset = 0;
+	test_fw_config->partial = false;
 	test_fw_config->sync_direct = false;
 	test_fw_config->req_firmware = request_firmware;
 	test_fw_config->test_result = 0;
@@ -253,6 +262,13 @@ static ssize_t config_show(struct device *dev,
 	len += scnprintf(buf+len, PAGE_SIZE - len,
 			"into_buf:\t\t%s\n",
 			test_fw_config->into_buf ? "true" : "false");
+	len += scnprintf(buf+len, PAGE_SIZE - len,
+			"buf_size:\t%zu\n", test_fw_config->buf_size);
+	len += scnprintf(buf+len, PAGE_SIZE - len,
+			"file_offset:\t%zu\n", test_fw_config->file_offset);
+	len += scnprintf(buf+len, PAGE_SIZE - len,
+			"partial:\t\t%s\n",
+			test_fw_config->partial ? "true" : "false");
 	len += scnprintf(buf+len, PAGE_SIZE - len,
 			"sync_direct:\t\t%s\n",
 			test_fw_config->sync_direct ? "true" : "false");
@@ -322,6 +338,39 @@ test_dev_config_show_bool(char *buf,
 	return snprintf(buf, PAGE_SIZE, "%d\n", val);
 }
 
+static int test_dev_config_update_size_t(const char *buf,
+					 size_t size,
+					 size_t *cfg)
+{
+	int ret;
+	long new;
+
+	ret = kstrtol(buf, 10, &new);
+	if (ret)
+		return ret;
+
+	if (new > SIZE_MAX)
+		return -EINVAL;
+
+	mutex_lock(&test_fw_mutex);
+	*(size_t *)cfg = new;
+	mutex_unlock(&test_fw_mutex);
+
+	/* Always return full write size even if we didn't consume all */
+	return size;
+}
+
+static ssize_t test_dev_config_show_size_t(char *buf, int cfg)
+{
+	size_t val;
+
+	mutex_lock(&test_fw_mutex);
+	val = cfg;
+	mutex_unlock(&test_fw_mutex);
+
+	return snprintf(buf, PAGE_SIZE, "%zu\n", val);
+}
+
 static ssize_t test_dev_config_show_int(char *buf, int cfg)
 {
 	int val;
@@ -419,6 +468,83 @@ static ssize_t config_into_buf_show(struct device *dev,
 }
 static DEVICE_ATTR_RW(config_into_buf);
 
+static ssize_t config_buf_size_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	int rc;
+
+	mutex_lock(&test_fw_mutex);
+	if (test_fw_config->reqs) {
+		pr_err("Must call release_all_firmware prior to changing config\n");
+		rc = -EINVAL;
+		mutex_unlock(&test_fw_mutex);
+		goto out;
+	}
+	mutex_unlock(&test_fw_mutex);
+
+	rc = test_dev_config_update_size_t(buf, count,
+					   &test_fw_config->buf_size);
+
+out:
+	return rc;
+}
+
+static ssize_t config_buf_size_show(struct device *dev,
+				    struct device_attribute *attr,
+				    char *buf)
+{
+	return test_dev_config_show_size_t(buf, test_fw_config->buf_size);
+}
+static DEVICE_ATTR_RW(config_buf_size);
+
+static ssize_t config_file_offset_store(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf, size_t count)
+{
+	int rc;
+
+	mutex_lock(&test_fw_mutex);
+	if (test_fw_config->reqs) {
+		pr_err("Must call release_all_firmware prior to changing config\n");
+		rc = -EINVAL;
+		mutex_unlock(&test_fw_mutex);
+		goto out;
+	}
+	mutex_unlock(&test_fw_mutex);
+
+	rc = test_dev_config_update_size_t(buf, count,
+					   &test_fw_config->file_offset);
+
+out:
+	return rc;
+}
+
+static ssize_t config_file_offset_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	return test_dev_config_show_size_t(buf, test_fw_config->file_offset);
+}
+static DEVICE_ATTR_RW(config_file_offset);
+
+static ssize_t config_partial_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t count)
+{
+	return test_dev_config_update_bool(buf,
+					   count,
+					   &test_fw_config->partial);
+}
+
+static ssize_t config_partial_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	return test_dev_config_show_bool(buf, test_fw_config->partial);
+}
+static DEVICE_ATTR_RW(config_partial);
+
 static ssize_t config_sync_direct_store(struct device *dev,
 					struct device_attribute *attr,
 					const char *buf, size_t count)
@@ -613,18 +739,24 @@ static int test_fw_run_batch_request(void *data)
 
 	if (test_fw_config->into_buf) {
 		void *test_buf;
+		unsigned int pread_flags;
 
 		test_buf = kzalloc(TEST_FIRMWARE_BUF_SIZE, GFP_KERNEL);
 		if (!test_buf)
 			return -ENOSPC;
 
+		if (test_fw_config->partial)
+			pread_flags = KERNEL_PREAD_FLAG_PART;
+		else
+			pread_flags = KERNEL_PREAD_FLAG_WHOLE;
+
 		req->rc = request_firmware_into_buf(&req->fw,
 						    req->name,
 						    req->dev,
 						    test_buf,
-						    TEST_FIRMWARE_BUF_SIZE,
-						    0,
-						    KERNEL_PREAD_FLAG_WHOLE);
+						    test_fw_config->buf_size,
+						    test_fw_config->file_offset,
+						    pread_flags);
 		if (!req->fw)
 			kfree(test_buf);
 	} else {
@@ -897,6 +1029,9 @@ static struct attribute *test_dev_attrs[] = {
 	TEST_FW_DEV_ATTR(config_name),
 	TEST_FW_DEV_ATTR(config_num_requests),
 	TEST_FW_DEV_ATTR(config_into_buf),
+	TEST_FW_DEV_ATTR(config_buf_size),
+	TEST_FW_DEV_ATTR(config_file_offset),
+	TEST_FW_DEV_ATTR(config_partial),
 	TEST_FW_DEV_ATTR(config_sync_direct),
 	TEST_FW_DEV_ATTR(config_send_uevent),
 	TEST_FW_DEV_ATTR(config_read_fw_idx),
Signature · 1 line
2.17.1
author maintainer
#27 lore raw

PATCH 4/7 selftests: firmware: Test partial file reads of request_firmware_into_buf

In reply to Scott Branden

Add firmware tests for partial file reads of request_firmware_into_buf.

---
 .../selftests/firmware/fw_filesystem.sh       | 80 +++++++++++++++++++
 1 file changed, 80 insertions(+)
tools/testing/selftests/firmware/fw_filesystem.sh +80 -0
index 56894477c8bd..e973c658fe1a 100755
@@ -126,6 +126,26 @@ config_unset_into_buf()
 	echo 0 >  $DIR/config_into_buf
 }
 
+config_set_buf_size()
+{
+	echo $1 >  $DIR/config_buf_size
+}
+
+config_set_file_offset()
+{
+	echo $1 >  $DIR/config_file_offset
+}
+
+config_set_partial()
+{
+	echo 1 >  $DIR/config_partial
+}
+
+config_unset_partial()
+{
+	echo 0 >  $DIR/config_partial
+}
+
 config_set_sync_direct()
 {
 	echo 1 >  $DIR/config_sync_direct
@@ -184,6 +204,35 @@ read_firmwares()
 	done
 }
 
+read_firmwares_partial()
+{
+	if [ "$(cat $DIR/config_into_buf)" == "1" ]; then
+		fwfile="${FW_INTO_BUF}"
+	else
+		fwfile="${FW}"
+	fi
+
+	if [ "$1" = "xzonly" ]; then
+		fwfile="${fwfile}-orig"
+	fi
+
+	# Strip fwfile down to match partial offset and length
+	partial_data="$(cat $fwfile)"
+	partial_data="${partial_data:$2:$3}"
+
+	for i in $(seq 0 3); do
+		config_set_read_fw_idx $i
+
+		read_firmware="$(cat $DIR/read_firmware)"
+
+		# Verify the contents are what we expect.
+		if [ $read_firmware != $partial_data ]; then
+			echo "request #$i: partial firmware was not loaded" >&2
+			exit 1
+		fi
+	done
+}
+
 read_firmwares_expect_nofile()
 {
 	for i in $(seq 0 3); do
@@ -296,6 +345,21 @@ test_batched_request_firmware_into_buf()
 	echo "OK"
 }
 
+test_batched_request_firmware_into_buf_partial()
+{
+	echo -n "Batched request_firmware_into_buf_partial() $2 off=$3 size=$4 try #$1: "
+	config_reset
+	config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME
+	config_set_into_buf
+	config_set_partial
+	config_set_buf_size $4
+	config_set_file_offset $3
+	config_trigger_sync
+	read_firmwares_partial $2 $3 $4
+	release_all_firmware
+	echo "OK"
+}
+
 test_batched_request_firmware_direct()
 {
 	echo -n "Batched request_firmware_direct() $2 try #$1: "
@@ -348,6 +412,22 @@ for i in $(seq 1 5); do
 	test_batched_request_firmware_into_buf $i normal
 done
 
+for i in $(seq 1 5); do
+	test_batched_request_firmware_into_buf_partial $i normal 0 10
+done
+
+for i in $(seq 1 5); do
+	test_batched_request_firmware_into_buf_partial $i normal 0 5
+done
+
+for i in $(seq 1 5); do
+	test_batched_request_firmware_into_buf_partial $i normal 1 6
+done
+
+for i in $(seq 1 5); do
+	test_batched_request_firmware_into_buf_partial $i normal 2 10
+done
+
 for i in $(seq 1 5); do
 	test_batched_request_firmware_direct $i normal
 done
Signature · 1 line
2.17.1
author maintainer
#28 lore raw

PATCH 5/7 bcm-vk: add bcm_vk UAPI

In reply to Scott Branden

Add user space api for bcm-vk driver.

---
 include/uapi/linux/misc/bcm_vk.h | 88 ++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h
include/uapi/linux/misc/bcm_vk.h new file +88 -0
new file mode 100644
index 000000000000..df7dfd7f0702
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */
+/*
+ * Copyright(c) 2018 Broadcom
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+struct vk_metadata {
+	/* struct version, always backwards compatible */
+	__u32 version;
+
+	/* Version 0 fields */
+	__u32 card_status;
+#define VK_CARD_STATUS_FASTBOOT_READY BIT(0)
+#define VK_CARD_STATUS_FWLOADER_READY BIT(1)
+
+	__u32 firmware_version;
+	__u32 fw_status;
+	/* End version 0 fields */
+
+	__u64 reserved[14];
+	/* Total of 16*u64 for all versions */
+};
+
+struct vk_image {
+	__u32 type;     /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+	char filename[64]; /* Filename of image */
+};
+
+/* default firmware images names */
+#define VK_BOOT1_DEF_FILENAME	    "vk-boot1.bin"
+#define VK_BOOT2_DEF_FILENAME	    "vk-boot2.bin"
+
+struct vk_access {
+	__u8 barno;     /* BAR number to use */
+	__u8 type;      /* Type of access */
+#define VK_ACCESS_READ 0
+#define VK_ACCESS_WRITE 1
+	__u32 len;      /* length of data */
+	__u64 offset;   /* offset in BAR */
+	__u32 *data;    /* where to read/write data to */
+};
+
+struct vk_reset {
+	__u32 arg1;
+	__u32 arg2;
+};
+
+#define VK_MAGIC              0x5E
+
+/* Get metadata from Valkyrie (firmware version, card status, etc) */
+#define VK_IOCTL_GET_METADATA _IOR(VK_MAGIC, 0x1, struct vk_metadata)
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE   _IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Read data from Valkyrie */
+#define VK_IOCTL_ACCESS_BAR   _IOWR(VK_MAGIC, 0x3, struct vk_access)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET        _IOW(VK_MAGIC, 0x4, struct vk_reset)
+
+/*
+ * message block - basic unit in the message where a message's size is always
+ *		   N x sizeof(basic_block)
+ */
+struct vk_msg_blk {
+	__u8 function_id;
+#define VK_FID_TRANS_BUF 5
+#define VK_FID_SHUTDOWN  8
+	__u8 size;
+	__u16 queue_id:4;
+	__u16 msg_id:12;
+	__u32 context_id;
+	__u32 args[2];
+#define VK_CMD_PLANES_MASK 0x000F /* number of planes to up/download */
+#define VK_CMD_UPLOAD      0x0400 /* memory transfer to vk */
+#define VK_CMD_DOWNLOAD    0x0500 /* memory transfer from vk */
+#define VK_CMD_MASK        0x0F00 /* command mask */
+};
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
Signature · 1 line
2.17.1
maintainer
#29 lore raw

Re: [PATCH 5/7] bcm-vk: add bcm_vk UAPI

Quoted 10 lines · Scott Branden

Add user space api for bcm-vk driver.

include/uapi/linux/misc/bcm_vk.h new file +7 -0
+
+struct vk_metadata {
+       /* struct version, always backwards compatible */
+       __u32 version;
+
+       /* Version 0 fields */
+       __u32 card_status;

include/uapi/linux/misc/bcm_vk.h line 27

Quoted 10 lines
include/uapi/linux/misc/bcm_vk.h new file +10 -0
+#define VK_CARD_STATUS_FASTBOOT_READY BIT(0)
+#define VK_CARD_STATUS_FWLOADER_READY BIT(1)
+
+       __u32 firmware_version;
+       __u32 fw_status;
+       /* End version 0 fields */
+
+       __u64 reserved[14];
+       /* Total of 16*u64 for all versions */
+};

I'd suggest getting rid of the API version fields, just leave the version 0 fields here and add a new structure + ioctl if you need other fields

Versioning usually just adds complexity and is hard to get right.

include/uapi/linux/misc/bcm_vk.h line 48

Quoted 9 lines
include/uapi/linux/misc/bcm_vk.h new file +9 -0
+struct vk_access {
+       __u8 barno;     /* BAR number to use */
+       __u8 type;      /* Type of access */
+#define VK_ACCESS_READ 0
+#define VK_ACCESS_WRITE 1
+       __u32 len;      /* length of data */
+       __u64 offset;   /* offset in BAR */
+       __u32 *data;    /* where to read/write data to */
+};

The pointer in the last member makes the structure incompatible between 32-bit and 64-bit user space. You could work around that using a __u64 member and turning that into a pointer using the u64_to_user_ptr() macro in the driver in a portable way.

However, since this seems to be a read/write type interface, maybe it's better to just use read/write file operations.

I also wonder if the interface should be on a higher abstraction level here.

Arnd

#30 lore raw

Re: [PATCH 5/7] bcm-vk: add bcm_vk UAPI

In reply to Scott Branden

Hi Scott,

Quoted 71 lines · Scott Branden

Add user space api for bcm-vk driver.

---
 include/uapi/linux/misc/bcm_vk.h | 88 ++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h
include/uapi/linux/misc/bcm_vk.h new file +57 -0
new file mode 100644
index 000000000000..df7dfd7f0702
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */
+/*
+ * Copyright(c) 2018 Broadcom
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+struct vk_metadata {
+	/* struct version, always backwards compatible */
+	__u32 version;
+
+	/* Version 0 fields */
+	__u32 card_status;
+#define VK_CARD_STATUS_FASTBOOT_READY BIT(0)
+#define VK_CARD_STATUS_FWLOADER_READY BIT(1)
+
+	__u32 firmware_version;
+	__u32 fw_status;
+	/* End version 0 fields */
+
+	__u64 reserved[14];
+	/* Total of 16*u64 for all versions */
+};
+
+struct vk_image {
+	__u32 type;     /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+	char filename[64]; /* Filename of image */
+};
+
+/* default firmware images names */
+#define VK_BOOT1_DEF_FILENAME	    "vk-boot1.bin"
+#define VK_BOOT2_DEF_FILENAME	    "vk-boot2.bin"
+
+struct vk_access {
+	__u8 barno;     /* BAR number to use */
+	__u8 type;      /* Type of access */
+#define VK_ACCESS_READ 0
+#define VK_ACCESS_WRITE 1
+	__u32 len;      /* length of data */
+	__u64 offset;   /* offset in BAR */
+	__u32 *data;    /* where to read/write data to */
+};
+
+struct vk_reset {
+	__u32 arg1;
+	__u32 arg2;
+};
+
+#define VK_MAGIC              0x5E
+
+/* Get metadata from Valkyrie (firmware version, card status, etc) */

include/uapi/linux/misc/bcm_vk.h line 67

Quoted 10 lines
include/uapi/linux/misc/bcm_vk.h new file +10 -0
+#define VK_IOCTL_GET_METADATA _IOR(VK_MAGIC, 0x1, struct vk_metadata)
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE   _IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Read data from Valkyrie */
+#define VK_IOCTL_ACCESS_BAR   _IOWR(VK_MAGIC, 0x3, struct vk_access)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET        _IOW(VK_MAGIC, 0x4, struct vk_reset)

It sounds a bit like the valkyrie is a generic asynchronous coprocessor, does it merit using the remoteproc interfaces to control it ?

Or is it really just a single purpose cell doing video operations ?

--
Kieran

Quoted 22 lines
include/uapi/linux/misc/bcm_vk.h new file +21 -0
+
+/*
+ * message block - basic unit in the message where a message's size is always
+ *		   N x sizeof(basic_block)
+ */
+struct vk_msg_blk {
+	__u8 function_id;
+#define VK_FID_TRANS_BUF 5
+#define VK_FID_SHUTDOWN  8
+	__u8 size;
+	__u16 queue_id:4;
+	__u16 msg_id:12;
+	__u32 context_id;
+	__u32 args[2];
+#define VK_CMD_PLANES_MASK 0x000F /* number of planes to up/download */
+#define VK_CMD_UPLOAD      0x0400 /* memory transfer to vk */
+#define VK_CMD_DOWNLOAD    0x0500 /* memory transfer from vk */
+#define VK_CMD_MASK        0x0F00 /* command mask */
+};
+
+#endif /* __UAPI_LINUX_MISC_BCM_VK_H */
Signature · 3 lines
Regards
--
Kieran
#31 lore raw

Re: [PATCH 5/7] bcm-vk: add bcm_vk UAPI

Quoted 89 lines · Kieran Bingham

Hi Scott,

Quoted 71 lines · Scott Branden

Add user space api for bcm-vk driver.

---
 include/uapi/linux/misc/bcm_vk.h | 88 ++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 include/uapi/linux/misc/bcm_vk.h
include/uapi/linux/misc/bcm_vk.h new file +57 -0
new file mode 100644
index 000000000000..df7dfd7f0702
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */
+/*
+ * Copyright(c) 2018 Broadcom
+ */
+
+#ifndef __UAPI_LINUX_MISC_BCM_VK_H
+#define __UAPI_LINUX_MISC_BCM_VK_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+struct vk_metadata {
+     /* struct version, always backwards compatible */
+     __u32 version;
+
+     /* Version 0 fields */
+     __u32 card_status;
+#define VK_CARD_STATUS_FASTBOOT_READY BIT(0)
+#define VK_CARD_STATUS_FWLOADER_READY BIT(1)
+
+     __u32 firmware_version;
+     __u32 fw_status;
+     /* End version 0 fields */
+
+     __u64 reserved[14];
+     /* Total of 16*u64 for all versions */
+};
+
+struct vk_image {
+     __u32 type;     /* Type of image */
+#define VK_IMAGE_TYPE_BOOT1 1 /* 1st stage (load to SRAM) */
+#define VK_IMAGE_TYPE_BOOT2 2 /* 2nd stage (load to DDR) */
+     char filename[64]; /* Filename of image */
+};
+
+/* default firmware images names */
+#define VK_BOOT1_DEF_FILENAME            "vk-boot1.bin"
+#define VK_BOOT2_DEF_FILENAME            "vk-boot2.bin"
+
+struct vk_access {
+     __u8 barno;     /* BAR number to use */
+     __u8 type;      /* Type of access */
+#define VK_ACCESS_READ 0
+#define VK_ACCESS_WRITE 1
+     __u32 len;      /* length of data */
+     __u64 offset;   /* offset in BAR */
+     __u32 *data;    /* where to read/write data to */
+};
+
+struct vk_reset {
+     __u32 arg1;
+     __u32 arg2;
+};
+
+#define VK_MAGIC              0x5E
+
+/* Get metadata from Valkyrie (firmware version, card status, etc) */

include/uapi/linux/misc/bcm_vk.h line 67

Quoted 10 lines
include/uapi/linux/misc/bcm_vk.h new file +10 -0
+#define VK_IOCTL_GET_METADATA _IOR(VK_MAGIC, 0x1, struct vk_metadata)
+
+/* Load image to Valkyrie */
+#define VK_IOCTL_LOAD_IMAGE   _IOW(VK_MAGIC, 0x2, struct vk_image)
+
+/* Read data from Valkyrie */
+#define VK_IOCTL_ACCESS_BAR   _IOWR(VK_MAGIC, 0x3, struct vk_access)
+
+/* Send Reset to Valkyrie */
+#define VK_IOCTL_RESET        _IOW(VK_MAGIC, 0x4, struct vk_reset)

It sounds a bit like the valkyrie is a generic asynchronous coprocessor, does it merit using the remoteproc interfaces to control it ?

Quoted 1 line

Or is it really just a single purpose cell doing video operations ?

Remoteproc brings some useful shared functionality, in particular around loading and parsing firmware formats for platforms where the remote processor uses system carved out memory to run, etc.

For something like a PCIe device, it *can* be used but it really doesn't bring any immediate benefit, especially if there aren't multiple in-kernel drivers that need to talk to the hardware in an abstracted way.

-Olof

-Olof

author maintainer
#32 lore raw

PATCH 6/7 misc: bcm-vk: add Broadcom Valkyrie driver

In reply to Scott Branden

Add Broadcom Valkyrie driver offload engine.
This driver interfaces to the Valkyrie PCIe offload engine to perform should offload functions as video transcoding on multiple streams in parallel. Valkyrie device is booted from files loaded using request_firmware_into_buf mechanism. After booted card status is updated and messages can then be sent to the card.
Such messages contain scatter gather list of addresses
to pull data from the host to perform operations on.

---
 drivers/misc/Kconfig             |    1 +
 drivers/misc/Makefile            |    1 +
 drivers/misc/bcm-vk/Kconfig      |   16 +
 drivers/misc/bcm-vk/Makefile     |    7 +
 drivers/misc/bcm-vk/README       |   29 +
 drivers/misc/bcm-vk/bcm_vk.h     |  229 +++++
 drivers/misc/bcm-vk/bcm_vk_dev.c | 1558 ++++++++++++++++++++++++++++++
 drivers/misc/bcm-vk/bcm_vk_msg.c |  963 ++++++++++++++++++
 drivers/misc/bcm-vk/bcm_vk_msg.h |  169 ++++
 drivers/misc/bcm-vk/bcm_vk_sg.c  |  273 ++++++
 drivers/misc/bcm-vk/bcm_vk_sg.h  |   60 ++
 11 files changed, 3306 insertions(+)
 create mode 100644 drivers/misc/bcm-vk/Kconfig
 create mode 100644 drivers/misc/bcm-vk/Makefile
 create mode 100644 drivers/misc/bcm-vk/README
 create mode 100644 drivers/misc/bcm-vk/bcm_vk.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_dev.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_msg.h
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.c
 create mode 100644 drivers/misc/bcm-vk/bcm_vk_sg.h
drivers/misc/Kconfig +1 -0
index 16900357afc2..67c1c2154c31 100644
@@ -501,6 +501,7 @@ source "drivers/misc/genwqe/Kconfig"
 source "drivers/misc/echo/Kconfig"
 source "drivers/misc/cxl/Kconfig"
 source "drivers/misc/ocxl/Kconfig"
+source "drivers/misc/bcm-vk/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
 source "drivers/misc/habanalabs/Kconfig"
 endmenu
drivers/misc/Makefile +1 -0
index abd8ae249746..c72a9a5335ca 100644
@@ -55,6 +55,7 @@ obj-$(CONFIG_VEXPRESS_SYSCFG)	+= vexpress-syscfg.o
 obj-$(CONFIG_CXL_BASE)		+= cxl/
 obj-$(CONFIG_PCI_ENDPOINT_TEST)	+= pci_endpoint_test.o
 obj-$(CONFIG_OCXL)		+= ocxl/
+obj-$(CONFIG_BCM_VK)		+= bcm-vk/
 obj-y				+= cardreader/
 obj-$(CONFIG_PVPANIC)   	+= pvpanic.o
 obj-$(CONFIG_HABANA_AI)		+= habanalabs/
drivers/misc/bcm-vk/Kconfig new file +16 -0
new file mode 100644
index 000000000000..92a28f048d06
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Broadcom VK device
+#
+config BCM_VK
+	tristate "Support for Broadcom Valkyrie Accelerators (VK)"
+	depends on PCI_MSI
+	default m
+	help
+	  Select this option to enable support for Broadcom
+	  Valkyrie Accelerators (VK).  VK is used for performing
+	  specific video offload processing.  This driver enables
+	  userspace programs to access these accelerators via /dev/bcm-vk.N
+	  devices.
+
+	  If unsure, say N.
drivers/misc/bcm-vk/Makefile new file +7 -0
new file mode 100644
index 000000000000..7bc48ca9c60b
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Broadcom VK driver
+#
+
+obj-$(CONFIG_BCM_VK) += bcm_vk.o
+bcm_vk-objs := bcm_vk_dev.o bcm_vk_msg.o bcm_vk_sg.o
drivers/misc/bcm-vk/README new file +29 -0
new file mode 100644
index 000000000000..1ec806455965
@@ -0,0 +1,29 @@
+Valkyrie Card Status using SysFS
+================================
+
+The bcm-vk driver supports the query of the Valkyrie card status using the
+SysFs entry.
+
+A Group attribute is created to report the valkyrie card status
+(vk-card-status) and different status values are consolidated under the
+card status group.
+
+The organization of the card status is given below.
+
+The group is created under the following : /sysfs/devices/<pci device number>/
+
+vk-card-status/
+	firmware-status
+	firmware-version
+	voltage		---> reports the 1.8v and 3.3v voltage rail readings
+	temperature	---> reports the current temperature of the soc
+
+The sysfs entry supports only the read operation.
+
+Example:
+To read the temperature from a PCIe device at 65:00.0:
+cat /sys/devices/pci0000:64/0000:64:00.0/0000:65:00.0/vk-card-status/voltage
+
+where
+	/sys/devices/pci0000:64/0000:64:00.0/0000:65:00.0/ --> path of bcm-vk device
+	vk-card-status/voltage --> current voltage reading from the card
drivers/misc/bcm-vk/bcm_vk.h new file +229 -0

229 lines not on this page · raw message

drivers/misc/bcm-vk/bcm_vk_dev.c new file +1558 -0

1,558 lines not on this page · raw message

drivers/misc/bcm-vk/bcm_vk_msg.c new file +963 -0

963 lines not on this page · raw message

drivers/misc/bcm-vk/bcm_vk_msg.h new file +169 -0

169 lines not on this page · raw message

drivers/misc/bcm-vk/bcm_vk_sg.c new file +273 -0

15 lines not on this page · raw message

drivers/misc/bcm-vk/bcm_vk_sg.h new file +60 -0

60 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.17.1

Part of the diff is not shown here. View the raw message.

maintainer
#33 lore raw

Re: [PATCH 6/7] misc: bcm-vk: add Broadcom Valkyrie driver

Quoted 10 lines · Scott Branden

Add Broadcom Valkyrie driver offload engine.
This driver interfaces to the Valkyrie PCIe offload engine to perform should offload functions as video transcoding on multiple streams in parallel. Valkyrie device is booted from files loaded using request_firmware_into_buf mechanism. After booted card status is updated and messages can then be sent to the card.
Such messages contain scatter gather list of addresses
to pull data from the host to perform operations on.

Quoted 3 lines

Can you explain the decision to make this is a standalone misc driver rather than hooking into the existing framework in drivers/media?

There is an existing interface that looks like it could fit the hardware in include/media/v4l2-mem2mem.h. Have you considered using that?

There is also support for video transcoding using GPUs in driver/gpu/drm/, that could also be used in theory, though it sounds like a less optimal fit.

Arnd

#34 lore raw

Re: [PATCH 6/7] misc: bcm-vk: add Broadcom Valkyrie driver

Quoted 18 lines · Arnd Bergmann
Quoted 9 lines · Scott Branden

Add Broadcom Valkyrie driver offload engine.
This driver interfaces to the Valkyrie PCIe offload engine to perform should offload functions as video transcoding on multiple streams in parallel. Valkyrie device is booted from files loaded using request_firmware_into_buf mechanism. After booted card status is updated and messages can then be sent to the card.
Such messages contain scatter gather list of addresses
to pull data from the host to perform operations on.

Quoted 3 lines

Can you explain the decision to make this is a standalone misc driver rather than hooking into the existing framework in drivers/media?

Quoted 6 lines

There is an existing interface that looks like it could fit the hardware in include/media/v4l2-mem2mem.h. Have you considered using that?

There is also support for video transcoding using GPUs in driver/gpu/drm/, that could also be used in theory, though it sounds like a less optimal fit.

I believe that a major obstacle with this driver is usability. Even though I have read through, I believe it's just impossible for anyone to actually write Open Source userspace for it. The commit message does not even try to help in this regard.

Note that depending on the feature your transcoder has, there is also the option to model it around the media controller. That is notably useful for certain transcoders that will also do scaling and produce multiple streams (for adaptive streaming usecases were you want to share a single decoder).

An 1 to 1 transcoder modeled around m2m would eventually required documentation so that other transcoder can be implemented in a way that they would share the same userspace. This is currently being worked on for m2m encoder and decoders (including state-less variants).

regards,
Nicolas

author maintainer
#35 lore raw

PATCH 7/7 MAINTAINERS: bcm-vk: Add maintainer for Broadcom Valkyrie Driver

In reply to Scott Branden

Add maintainer entry for new Broadcom Valkyrie Driver

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index 08176d64eed5..6eb2e3accf1d 100644
@@ -3456,6 +3456,13 @@ L:	[email protected]
 S:	Supported
 F:	drivers/net/ethernet/broadcom/tg3.*
 
+BROADCOM VALKYRIE DRIVER
+M:	Scott Branden <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/misc/bcm-vk/
+F:	include/uapi/linux/misc/bcm_vk.h
+
 BROCADE BFA FC SCSI DRIVER
 M:	Anil Gurumurthy <[email protected]>
 M:	Sudarsana Kalluru <[email protected]>
Signature · 1 line
2.17.1