mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: tze.yee.ng@altera.com
To: Moritz Fischer <mdf@kernel.org>, Xu Yilun <yilun.xu@intel.com>,
	Tom Rix <trix@redhat.com>,
	linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3] fpga: altera-cvp: Handle truncated bitstream
Date: Wed,  2 Dec 2026 16:26:54 +0800	[thread overview]
Message-ID: <20261202082654.91189-1-tze.yee.ng@altera.com> (raw)

From: Tze Yee Ng <tze.yee.ng@altera.com>

Recovering from a truncated bitstream fails on CvP V2 devices for two
reasons.

First, the CvP firmware moves data internally in 4096-byte blocks, one
block per TX credit, and requires the host to send exactly 4096 bytes
per block. Sending less leaves the firmware DMA waiting for the rest of
the block, so configuration stalls. The driver sends the last block
with only the remaining bytes, so this happens whenever the image size
is not a multiple of 4096 bytes (e.g. 4096 + 1), which is typical of a
truncated bitstream. Pad the last block with zero words up to the block
size in altera_cvp_send_block(). Full blocks are still sent directly
from the caller's buffer, so no copy is needed. V1 devices use a 4-byte
block size, which the existing trailing-byte handling already fills, so
they are not affected.

Second, write_complete() detects a failed configuration by reading
CVP_CONFIG_ERROR_LATCHED (bit 5 of the Uncorrectable Internal Error
Status register). This bit is a sticky RW1CS copy of CVP_CONFIG_ERROR,
set whenever CVP_CONFIG_ERROR rises in CvP mode. The driver never
clears it, so after one failed configuration every later attempt is
reported as failed, even with a good image. On newer devices it is
also only reported on PCIe port 0, so an error on other ports goes
unnoticed.

CVP_CONFIG_ERROR (bit 19 of the CvP Status register) is driven by the
configuration firmware, which keeps it set until the next configuration
starts, so it is still valid after teardown. As the latched bit is only
a sticky copy of it, checking CVP_CONFIG_ERROR covers every error the
latched bit reports. Use altera_cvp_chk_error(), which already checks
CVP_CONFIG_ERROR for the optional checks in write(), for the STEP 16
check as well.

Co-developed-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com>
Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com>
Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com>
---
Changes in v3:
- Take authorship, with Nazim as Co-developed-by, as the code changed
  substantially.
- Drop send_buf and the per-block memcpy(); zero-pad only the last block
  in altera_cvp_send_block().
- Reuse altera_cvp_chk_error() for the STEP 16 check instead of
  duplicating it.
- Rewrite the commit log to explain the 4096-byte block requirement and
  the difference between the two error bits.

Changes in v2:
- Rebase from linux-fpga/for-next without code changes.

v2: https://lore.kernel.org/all/3c1bb384cb9cd5bb35b3c7e68a4feab95ef4ad60.1794987679.git.tze.yee.ng@altera.com/
v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/
---
 drivers/fpga/altera-cvp.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
index 103446465e0c..42febad9c4e4 100644
--- a/drivers/fpga/altera-cvp.c
+++ b/drivers/fpga/altera-cvp.c
@@ -297,6 +297,7 @@ static int altera_cvp_v2_wait_for_credit(struct fpga_manager *mgr,
 static int altera_cvp_send_block(struct altera_cvp_conf *conf,
 				 const u32 *data, size_t len)
 {
+	u32 block_words = conf->priv->block_size / sizeof(u32);
 	u32 words = len / sizeof(u32);
 	int i, remainder;
 
@@ -310,8 +311,16 @@ static int altera_cvp_send_block(struct altera_cvp_conf *conf,
 
 		memcpy(&word, data, remainder);
 		conf->write_data(conf, word);
+		words++;
 	}
 
+	/*
+	 * The CvP firmware only consumes whole blocks and stalls waiting
+	 * for the rest of a short one, so pad the last block with zeros.
+	 */
+	for (; words < block_words; words++)
+		conf->write_data(conf, 0);
+
 	return 0;
 }
 
@@ -550,19 +559,18 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr,
 				     struct fpga_image_info *info)
 {
 	struct altera_cvp_conf *conf = mgr->priv;
-	u32 mask, val;
+	u32 mask;
 	int ret;
 
 	ret = altera_cvp_recovery(mgr, info);
 	if (ret)
 		return ret;
 
-	/* STEP 16 - check CVP_CONFIG_ERROR_LATCHED bit */
-	altera_read_config_dword(conf, VSE_UNCOR_ERR_STATUS, &val);
-	if (val & VSE_UNCOR_ERR_CVP_CFG_ERR) {
-		dev_err(&mgr->dev, "detected CVP_CONFIG_ERROR_LATCHED!\n");
-		return -EPROTO;
-	}
+	/* STEP 16 - check CVP_CONFIG_ERROR bit */
+	ret = altera_cvp_chk_error(mgr, conf->sent_packets *
+				   conf->priv->block_size);
+	if (ret)
+		return ret;
 
 	/* STEP 17 - reset CVP_MODE and HIP_CLK_SEL bit */
 	altera_cvp_disable_cvp_mode(conf);
-- 
2.43.7


                 reply	other threads:[~2026-12-02  8:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261202082654.91189-1-tze.yee.ng@altera.com \
    --to=tze.yee.ng@altera.com \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdf@kernel.org \
    --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

all inboxes | Powered by JetHome®