linux-fpga.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ivan Bornyakov <i.bornyakov@metrotek.ru>
To: unlisted-recipients:; (no To-header on input)
Cc: Ivan Bornyakov <i.bornyakov@metrotek.ru>,
	mdf@kernel.org, hao.wu@intel.com, yilun.xu@intel.com,
	trix@redhat.com, conor.dooley@microchip.com, robh+dt@kernel.org,
	krzysztof.kozlowski+dt@linaro.org, system@metrotek.ru,
	linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org,
	devicetree@vger.kernel.org
Subject: [PATCH v9 1/3] fpga: fpga-mgr: support bitstream offset in image buffer
Date: Thu,  7 Apr 2022 16:36:56 +0300	[thread overview]
Message-ID: <20220407133658.15699-2-i.bornyakov@metrotek.ru> (raw)
In-Reply-To: <20220407133658.15699-1-i.bornyakov@metrotek.ru>

It is not always whole FPGA image buffer meant to be written to the
device.

Add bitstream_start and bitstream_size to the fpga_image_info struct and
adjust fpga_mgr_write() callers with respect to them.

If initial_header_size is not known beforehand, pass whole buffer to low
level driver's write_init() so it could setup info->bitstream_start and
info->bitstream_size regardless.

Signed-off-by: Ivan Bornyakov <i.bornyakov@metrotek.ru>
---
 drivers/fpga/fpga-mgr.c       | 48 +++++++++++++++++++++++++++++------
 include/linux/fpga/fpga-mgr.h |  5 ++++
 2 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
index d49a9ce34568..c64e60e23a71 100644
--- a/drivers/fpga/fpga-mgr.c
+++ b/drivers/fpga/fpga-mgr.c
@@ -139,7 +139,8 @@ EXPORT_SYMBOL_GPL(fpga_image_info_free);
  * Call the low level driver's write_init function.  This will do the
  * device-specific things to get the FPGA into the state where it is ready to
  * receive an FPGA image. The low level driver only gets to see the first
- * initial_header_size bytes in the buffer.
+ * initial_header_size bytes in the buffer, if initial_header_size is set.
+ * Otherwise, the whole buffer will be passed.
  */
 static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
 				   struct fpga_image_info *info,
@@ -148,12 +149,10 @@ static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
 	int ret;
 
 	mgr->state = FPGA_MGR_STATE_WRITE_INIT;
-	if (!mgr->mops->initial_header_size)
-		ret = fpga_mgr_write_init(mgr, info, NULL, 0);
-	else
-		ret = fpga_mgr_write_init(
-		    mgr, info, buf, min(mgr->mops->initial_header_size, count));
+	if (mgr->mops->initial_header_size)
+		count = min(mgr->mops->initial_header_size, count);
 
+	ret = fpga_mgr_write_init(mgr, info, buf, count);
 	if (ret) {
 		dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
 		mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
@@ -235,13 +234,33 @@ static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
 	if (mgr->mops->write_sg) {
 		ret = fpga_mgr_write_sg(mgr, sgt);
 	} else {
+		size_t offset, count, length, bitstream_size;
 		struct sg_mapping_iter miter;
 
+		offset = info->bitstream_start;
+		bitstream_size = info->bitstream_size;
+		count = 0;
+
 		sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
 		while (sg_miter_next(&miter)) {
-			ret = fpga_mgr_write(mgr, miter.addr, miter.length);
-			if (ret)
+			if (offset >= miter.length) {
+				offset -= miter.length;
+				continue;
+			}
+
+			if (bitstream_size)
+				length = min(miter.length - offset,
+					     bitstream_size - count);
+			else
+				length = miter.length - offset;
+
+			count += length;
+
+			ret = fpga_mgr_write(mgr, miter.addr + offset, length);
+			if (ret || count == bitstream_size)
 				break;
+
+			offset = 0;
 		}
 		sg_miter_stop(&miter);
 	}
@@ -265,6 +284,19 @@ static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
 	if (ret)
 		return ret;
 
+	if (info->bitstream_start > count) {
+		dev_err(&mgr->dev,
+			"Bitstream start %zd outruns firmware image %zd\n",
+			info->bitstream_start, count);
+		return -EINVAL;
+	}
+
+	if (info->bitstream_size)
+		count = min(info->bitstream_start + info->bitstream_size, count);
+
+	buf += info->bitstream_start;
+	count -= info->bitstream_start;
+
 	/*
 	 * Write the FPGA image to the FPGA.
 	 */
diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
index 0f9468771bb9..32464fd10cca 100644
--- a/include/linux/fpga/fpga-mgr.h
+++ b/include/linux/fpga/fpga-mgr.h
@@ -85,6 +85,9 @@ enum fpga_mgr_states {
  * @sgt: scatter/gather table containing FPGA image
  * @buf: contiguous buffer containing FPGA image
  * @count: size of buf
+ * @bitstream_start: offset in image buffer where bitstream data starts
+ * @bitstream_size: size of bitstream.
+ *	If 0, (count - bitstream_start) will be used.
  * @region_id: id of target region
  * @dev: device that owns this
  * @overlay: Device Tree overlay
@@ -98,6 +101,8 @@ struct fpga_image_info {
 	struct sg_table *sgt;
 	const char *buf;
 	size_t count;
+	size_t bitstream_start;
+	size_t bitstream_size;
 	int region_id;
 	struct device *dev;
 #ifdef CONFIG_OF
-- 
2.25.1



  reply	other threads:[~2022-04-07 13:39 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07 13:36 [PATCH v9 0/3] Microchip Polarfire FPGA manager Ivan Bornyakov
2022-04-07 13:36 ` Ivan Bornyakov [this message]
2022-04-09  5:04   ` [PATCH v9 1/3] fpga: fpga-mgr: support bitstream offset in image buffer Xu Yilun
2022-04-09 12:38     ` Ivan Bornyakov
2022-04-11  1:36       ` Xu Yilun
2022-04-07 13:36 ` [PATCH v9 2/3] fpga: microchip-spi: add Microchip MPF FPGA manager Ivan Bornyakov
2022-04-07 13:36 ` [PATCH v9 3/3] dt-bindings: fpga: add binding doc for microchip-spi fpga mgr Ivan Bornyakov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220407133658.15699-2-i.bornyakov@metrotek.ru \
    --to=i.bornyakov@metrotek.ru \
    --cc=conor.dooley@microchip.com \
    --cc=devicetree@vger.kernel.org \
    --cc=hao.wu@intel.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdf@kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=system@metrotek.ru \
    --cc=trix@redhat.com \
    --cc=yilun.xu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).