* [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer @ 2026-11-18 7:43 tze.yee.ng 2026-11-26 2:11 ` NG, TZE YEE 2026-11-28 14:15 ` Xu Yilun 0 siblings, 2 replies; 6+ messages in thread From: tze.yee.ng @ 2026-11-18 7:43 UTC (permalink / raw) To: Moritz Fischer, Xu Yilun, Tom Rix, linux-fpga, linux-kernel From: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> The AIB CvP firmware requires a minimum transfer size of 4096 bytes. When a bitstream smaller than this is sent, the firmware DMA engine stalls waiting for the remaining data, causing a hang. Allocate a 4096-byte send_buf at probe time and copy host data into it before each block transfer, ensuring the full block size is always sent regardless of the actual data length. Also improve error detection in write_complete() by checking VSE_CVP_STATUS_CFG_ERR instead of VSE_UNCOR_ERR_CVP_CFG_ERR to catch truncated or mismatched bitstream errors. Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com> --- Note: I am taking over this patch from Nazim Amirul, who is no longer working on it. Nazim is kept as the author. Changes in v2: - Rebase from linux-fpga/for-next without code changes. v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/ --- drivers/fpga/altera-cvp.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c index 103446465e0c..c9357cbe7fc0 100644 --- a/drivers/fpga/altera-cvp.c +++ b/drivers/fpga/altera-cvp.c @@ -82,6 +82,7 @@ struct altera_cvp_conf { u8 numclks; u32 sent_packets; u32 vsec_offset; + u8 *send_buf; const struct cvp_priv *priv; }; @@ -518,7 +519,9 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf, } len = min(conf->priv->block_size, remaining); - altera_cvp_send_block(conf, data, len); + memcpy(conf->send_buf, data, len); + altera_cvp_send_block(conf, (const u32 *)conf->send_buf, + conf->priv->block_size); data += len / sizeof(u32); done += len; remaining -= len; @@ -557,10 +560,10 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr, 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"); + /* STEP 16 - check CVP_CONFIG_ERROR bit */ + ret = altera_read_config_dword(conf, VSE_CVP_STATUS, &val); + if (ret || (val & VSE_CVP_STATUS_CFG_ERR)) { + dev_err(&mgr->dev, "CVP_CONFIG_ERROR!\n"); return -EPROTO; } @@ -729,6 +732,11 @@ static int altera_cvp_probe(struct pci_dev *pdev, pci_set_drvdata(pdev, mgr); + conf->send_buf = devm_kzalloc(&pdev->dev, conf->priv->block_size, GFP_KERNEL); + if (!conf->send_buf) { + ret = -ENOMEM; + goto err_unmap; + } return 0; err_unmap: -- 2.43.7 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer 2026-11-18 7:43 [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer tze.yee.ng @ 2026-11-26 2:11 ` NG, TZE YEE 2026-11-28 14:15 ` Xu Yilun 1 sibling, 0 replies; 6+ messages in thread From: NG, TZE YEE @ 2026-11-26 2:11 UTC (permalink / raw) To: Moritz Fischer, Xu Yilun, Tom Rix, linux-fpga, linux-kernel Hi, Gentle ping on this patch. Any chance for a review when you have time? Thanks, Tze Yee On 18/11/2026 3:43 pm, tze.yee.ng@altera.com wrote: > From: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> > > The AIB CvP firmware requires a minimum transfer size of 4096 bytes. > When a bitstream smaller than this is sent, the firmware DMA engine > stalls waiting for the remaining data, causing a hang. > > Allocate a 4096-byte send_buf at probe time and copy host data into > it before each block transfer, ensuring the full block size is always > sent regardless of the actual data length. Also improve error > detection in write_complete() by checking VSE_CVP_STATUS_CFG_ERR > instead of VSE_UNCOR_ERR_CVP_CFG_ERR to catch truncated or > mismatched bitstream errors. > > Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> > Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com> > --- > Note: I am taking over this patch from Nazim Amirul, who is no longer > working on it. Nazim is kept as the author. > > Changes in v2: > - Rebase from linux-fpga/for-next without code changes. > > v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/ > --- > drivers/fpga/altera-cvp.c | 18 +++++++++++++----- > 1 file changed, 13 insertions(+), 5 deletions(-) > > diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c > index 103446465e0c..c9357cbe7fc0 100644 > --- a/drivers/fpga/altera-cvp.c > +++ b/drivers/fpga/altera-cvp.c > @@ -82,6 +82,7 @@ struct altera_cvp_conf { > u8 numclks; > u32 sent_packets; > u32 vsec_offset; > + u8 *send_buf; > const struct cvp_priv *priv; > }; > > @@ -518,7 +519,9 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf, > } > > len = min(conf->priv->block_size, remaining); > - altera_cvp_send_block(conf, data, len); > + memcpy(conf->send_buf, data, len); > + altera_cvp_send_block(conf, (const u32 *)conf->send_buf, > + conf->priv->block_size); > data += len / sizeof(u32); > done += len; > remaining -= len; > @@ -557,10 +560,10 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr, > 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"); > + /* STEP 16 - check CVP_CONFIG_ERROR bit */ > + ret = altera_read_config_dword(conf, VSE_CVP_STATUS, &val); > + if (ret || (val & VSE_CVP_STATUS_CFG_ERR)) { > + dev_err(&mgr->dev, "CVP_CONFIG_ERROR!\n"); > return -EPROTO; > } > > @@ -729,6 +732,11 @@ static int altera_cvp_probe(struct pci_dev *pdev, > > pci_set_drvdata(pdev, mgr); > > + conf->send_buf = devm_kzalloc(&pdev->dev, conf->priv->block_size, GFP_KERNEL); > + if (!conf->send_buf) { > + ret = -ENOMEM; > + goto err_unmap; > + } > return 0; > > err_unmap: ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer 2026-11-18 7:43 [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer tze.yee.ng 2026-11-26 2:11 ` NG, TZE YEE @ 2026-11-28 14:15 ` Xu Yilun 2026-12-01 7:32 ` NG, TZE YEE 1 sibling, 1 reply; 6+ messages in thread From: Xu Yilun @ 2026-11-28 14:15 UTC (permalink / raw) To: tze.yee.ng; +Cc: Moritz Fischer, Xu Yilun, Tom Rix, linux-fpga, linux-kernel On Wed, Nov 18, 2026 at 03:43:15PM +0800, tze.yee.ng@altera.com wrote: > From: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> > > The AIB CvP firmware requires a minimum transfer size of 4096 bytes. > When a bitstream smaller than this is sent, the firmware DMA engine Only when < 4096? E.g. if a bitstream is 4096 + 1, it is good? > stalls waiting for the remaining data, causing a hang. > > Allocate a 4096-byte send_buf at probe time and copy host data into > it before each block transfer, ensuring the full block size is always > sent regardless of the actual data length. Also improve error > detection in write_complete() by checking VSE_CVP_STATUS_CFG_ERR > instead of VSE_UNCOR_ERR_CVP_CFG_ERR to catch truncated or > mismatched bitstream errors. What VSE_CVP_STATUS_CFG_ERR checks? and what VSE_UNCOR_ERR_CVP_CFG_ERR checks? why it is an improvement? Please specify in log. > > Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> > Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com> > --- > Note: I am taking over this patch from Nazim Amirul, who is no longer > working on it. Nazim is kept as the author. > > Changes in v2: > - Rebase from linux-fpga/for-next without code changes. I'm wondering if you agree with all his code/log so you don't change, or you just wait for me to find all the issues anyway. > > v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/ > --- > drivers/fpga/altera-cvp.c | 18 +++++++++++++----- > 1 file changed, 13 insertions(+), 5 deletions(-) > > diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c > index 103446465e0c..c9357cbe7fc0 100644 > --- a/drivers/fpga/altera-cvp.c > +++ b/drivers/fpga/altera-cvp.c > @@ -82,6 +82,7 @@ struct altera_cvp_conf { > u8 numclks; > u32 sent_packets; > u32 vsec_offset; > + u8 *send_buf; > const struct cvp_priv *priv; > }; > > @@ -518,7 +519,9 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf, > } > > len = min(conf->priv->block_size, remaining); > - altera_cvp_send_block(conf, data, len); > + memcpy(conf->send_buf, data, len); So you copied the whole bitstream just because of some bytes of unalignment? Frankly speaking, I really think the image or the firmware should fix, you can't produce a image that the FW can't handle. > + altera_cvp_send_block(conf, (const u32 *)conf->send_buf, > + conf->priv->block_size); > data += len / sizeof(u32); > done += len; > remaining -= len; > @@ -557,10 +560,10 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr, > 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"); > + /* STEP 16 - check CVP_CONFIG_ERROR bit */ > + ret = altera_read_config_dword(conf, VSE_CVP_STATUS, &val); > + if (ret || (val & VSE_CVP_STATUS_CFG_ERR)) { > + dev_err(&mgr->dev, "CVP_CONFIG_ERROR!\n"); Why we don't check the ret before and now we do check? Why there are many other reads that don't check? And I find nearly exact the same 5 lines, any reason to duplicate them. > return -EPROTO; > } > > @@ -729,6 +732,11 @@ static int altera_cvp_probe(struct pci_dev *pdev, > > pci_set_drvdata(pdev, mgr); > > + conf->send_buf = devm_kzalloc(&pdev->dev, conf->priv->block_size, GFP_KERNEL); > + if (!conf->send_buf) { > + ret = -ENOMEM; > + goto err_unmap; > + } > return 0; > > err_unmap: > -- > 2.43.7 > > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer 2026-11-28 14:15 ` Xu Yilun @ 2026-12-01 7:32 ` NG, TZE YEE 2026-12-02 12:48 ` Xu Yilun 0 siblings, 1 reply; 6+ messages in thread From: NG, TZE YEE @ 2026-12-01 7:32 UTC (permalink / raw) To: Xu Yilun; +Cc: Moritz Fischer, Xu Yilun, Tom Rix, linux-fpga, linux-kernel Hi Yilun, Thanks for the review. On 28/11/2026 10:15 pm, Xu Yilun wrote: > On Wed, Nov 18, 2026 at 03:43:15PM +0800, tze.yee.ng@altera.com wrote: >> From: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> >> >> The AIB CvP firmware requires a minimum transfer size of 4096 bytes. >> When a bitstream smaller than this is sent, the firmware DMA engine > > Only when < 4096? E.g. if a bitstream is 4096 + 1, it is good? > No, the log was wrong. The CvP firmware moves data in 4096-byte blocks, one per TX credit, and requires the host to send exactly 4096 bytes per block. The driver sends the last block with only the remaining bytes, so any image whose size is not a multiple of 4096 stalls on the last block, 4096 + 1 included. I'll correct this in v3. >> stalls waiting for the remaining data, causing a hang. >> >> Allocate a 4096-byte send_buf at probe time and copy host data into >> it before each block transfer, ensuring the full block size is always >> sent regardless of the actual data length. Also improve error >> detection in write_complete() by checking VSE_CVP_STATUS_CFG_ERR >> instead of VSE_UNCOR_ERR_CVP_CFG_ERR to catch truncated or >> mismatched bitstream errors. > > What VSE_CVP_STATUS_CFG_ERR checks? and what VSE_UNCOR_ERR_CVP_CFG_ERR > checks? why it is an improvement? Please specify in log. > VSE_UNCOR_ERR_CVP_CFG_ERR (CVP_CONFIG_ERROR_LATCHED) 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. VSE_CVP_STATUS_CFG_ERR (CVP_CONFIG_ERROR) is driven by the configuration firmware, which keeps it set until the next configuration starts, so it is still valid in write_complete(). Since the latched bit is only a sticky copy of it, checking CVP_CONFIG_ERROR covers every error the latched bit reports. I'll explain this in the v3 log. >> >> Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> >> Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com> >> --- >> Note: I am taking over this patch from Nazim Amirul, who is no longer >> working on it. Nazim is kept as the author. >> >> Changes in v2: >> - Rebase from linux-fpga/for-next without code changes. > > I'm wondering if you agree with all his code/log so you don't change, or > you just wait for me to find all the issues anyway. > That's fair. I should have reviewed the patch properly before resending it instead of only rebasing it. I've gone through it now and reworked it as described below. >> >> v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/ >> --- >> drivers/fpga/altera-cvp.c | 18 +++++++++++++----- >> 1 file changed, 13 insertions(+), 5 deletions(-) >> >> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c >> index 103446465e0c..c9357cbe7fc0 100644 >> --- a/drivers/fpga/altera-cvp.c >> +++ b/drivers/fpga/altera-cvp.c >> @@ -82,6 +82,7 @@ struct altera_cvp_conf { >> u8 numclks; >> u32 sent_packets; >> u32 vsec_offset; >> + u8 *send_buf; >> const struct cvp_priv *priv; >> }; >> >> @@ -518,7 +519,9 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf, >> } >> >> len = min(conf->priv->block_size, remaining); >> - altera_cvp_send_block(conf, data, len); >> + memcpy(conf->send_buf, data, len); > > So you copied the whole bitstream just because of some bytes of unalignment? > Agreed, that was unnecessary. The copy was there so the last block could be sent at full length without reading past the end of the image. It also meant the last block was padded with leftover data from the previous block rather than zeros. v3 drops it: full blocks are written directly from the image buffer, and the last short block is completed with zero words in altera_cvp_send_block(), so nothing is read beyond the data. V1 devices use a 4-byte block size and are not affected. > Frankly speaking, I really think the image or the firmware should fix, > you can't produce a image that the FW can't handle. > The firmware side has been fixed: when the host terminates early, the firmware now completes the session, reports CVP_CONFIG_ERROR, and accepts a new configuration afterwards. But the CvP host interface requires every block to be exactly 4096 bytes, and the driver currently breaks that rule for the last block. With full blocks, a truncated image is rejected with CVP_CONFIG_ERROR and a good image can be loaded afterwards. Without them, the firmware is left waiting for the rest of the last block and the device cannot be reconfigured. A truncated or corrupted image is input the driver can't rule out, so it should report an error rather than leave the device stuck. >> + altera_cvp_send_block(conf, (const u32 *)conf->send_buf, >> + conf->priv->block_size); >> data += len / sizeof(u32); >> done += len; >> remaining -= len; >> @@ -557,10 +560,10 @@ static int altera_cvp_write_complete(struct fpga_manager *mgr, >> 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"); >> + /* STEP 16 - check CVP_CONFIG_ERROR bit */ >> + ret = altera_read_config_dword(conf, VSE_CVP_STATUS, &val); >> + if (ret || (val & VSE_CVP_STATUS_CFG_ERR)) { >> + dev_err(&mgr->dev, "CVP_CONFIG_ERROR!\n"); > > Why we don't check the ret before and now we do check? Why there are > many other reads that don't check? > > And I find nearly exact the same 5 lines, any reason to duplicate them. > There was no good reason to open-code it. The lines duplicate altera_cvp_chk_error(), so v3 calls that helper instead. The helper treats a failed read as an error, as it already does for the checks in write(). That makes no practical difference here, because a failed config read returns all ones, so the error bit would read as set anyway. I've left the other reads as they are, since changing them is unrelated to this fix. I'll send out v3 later. Thanks, Tze Yee >> return -EPROTO; >> } >> >> @@ -729,6 +732,11 @@ static int altera_cvp_probe(struct pci_dev *pdev, >> >> pci_set_drvdata(pdev, mgr); >> >> + conf->send_buf = devm_kzalloc(&pdev->dev, conf->priv->block_size, GFP_KERNEL); >> + if (!conf->send_buf) { >> + ret = -ENOMEM; >> + goto err_unmap; >> + } >> return 0; >> >> err_unmap: >> -- >> 2.43.7 >> >> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer 2026-12-01 7:32 ` NG, TZE YEE @ 2026-12-02 12:48 ` Xu Yilun 2026-12-03 7:50 ` NG, TZE YEE 0 siblings, 1 reply; 6+ messages in thread From: Xu Yilun @ 2026-12-02 12:48 UTC (permalink / raw) To: NG, TZE YEE; +Cc: Moritz Fischer, Xu Yilun, Tom Rix, linux-fpga, linux-kernel On Tue, Dec 01, 2026 at 03:32:38PM +0800, NG, TZE YEE wrote: > Hi Yilun, > > Thanks for the review. > > On 28/11/2026 10:15 pm, Xu Yilun wrote: > > On Wed, Nov 18, 2026 at 03:43:15PM +0800, tze.yee.ng@altera.com wrote: > > > From: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> > > > > > > The AIB CvP firmware requires a minimum transfer size of 4096 bytes. > > > When a bitstream smaller than this is sent, the firmware DMA engine > > > > Only when < 4096? E.g. if a bitstream is 4096 + 1, it is good? > > > > No, the log was wrong. The CvP firmware moves data in 4096-byte blocks, > one per TX credit, and requires the host to send exactly 4096 bytes per > block. The driver sends the last block with only the remaining bytes, > so any image whose size is not a multiple of 4096 stalls on the last > block, 4096 + 1 included. I'll correct this in v3. > > > > stalls waiting for the remaining data, causing a hang. > > > > > > Allocate a 4096-byte send_buf at probe time and copy host data into > > > it before each block transfer, ensuring the full block size is always > > > sent regardless of the actual data length. Also improve error > > > detection in write_complete() by checking VSE_CVP_STATUS_CFG_ERR > > > instead of VSE_UNCOR_ERR_CVP_CFG_ERR to catch truncated or > > > mismatched bitstream errors. > > > > What VSE_CVP_STATUS_CFG_ERR checks? and what VSE_UNCOR_ERR_CVP_CFG_ERR > > checks? why it is an improvement? Please specify in log. > > > > VSE_UNCOR_ERR_CVP_CFG_ERR (CVP_CONFIG_ERROR_LATCHED) 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. > > VSE_CVP_STATUS_CFG_ERR (CVP_CONFIG_ERROR) is driven by the configuration > firmware, which keeps it set until the next configuration starts, so it is > still valid in write_complete(). Since the latched bit is only a sticky copy > of it, checking CVP_CONFIG_ERROR covers every error the latched bit reports. > I'll explain this in the v3 log. Then what's the connection to this 4096-byte requirement? Other errors doesn't trigger VSE_UNCOR_ERR_CVP_CFG_ERR? > > > > > > > Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> > > > Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com> > > > --- > > > Note: I am taking over this patch from Nazim Amirul, who is no longer > > > working on it. Nazim is kept as the author. > > > > > > Changes in v2: > > > - Rebase from linux-fpga/for-next without code changes. > > > > I'm wondering if you agree with all his code/log so you don't change, or > > you just wait for me to find all the issues anyway. > > > > That's fair. I should have reviewed the patch properly before resending > it instead of only rebasing it. I've gone through it now and reworked > it as described below. > > > > > > > v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/ > > > --- > > > drivers/fpga/altera-cvp.c | 18 +++++++++++++----- > > > 1 file changed, 13 insertions(+), 5 deletions(-) > > > > > > diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c > > > index 103446465e0c..c9357cbe7fc0 100644 > > > --- a/drivers/fpga/altera-cvp.c > > > +++ b/drivers/fpga/altera-cvp.c > > > @@ -82,6 +82,7 @@ struct altera_cvp_conf { > > > u8 numclks; > > > u32 sent_packets; > > > u32 vsec_offset; > > > + u8 *send_buf; > > > const struct cvp_priv *priv; > > > }; > > > @@ -518,7 +519,9 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf, > > > } > > > len = min(conf->priv->block_size, remaining); > > > - altera_cvp_send_block(conf, data, len); > > > + memcpy(conf->send_buf, data, len); > > > > So you copied the whole bitstream just because of some bytes of unalignment? > > > Agreed, that was unnecessary. The copy was there so the last block > could be sent at full length without reading past the end of the > image. It also meant the last block was padded with leftover data from > the previous block rather than zeros. > > v3 drops it: full blocks are written directly from the image buffer, > and the last short block is completed with zero words in > altera_cvp_send_block(), so nothing is read beyond the data. V1 devices > use a 4-byte block size and are not affected. > > > Frankly speaking, I really think the image or the firmware should fix, > > you can't produce a image that the FW can't handle. > > > > The firmware side has been fixed: when the host terminates early, the > firmware now completes the session, reports CVP_CONFIG_ERROR, and > accepts a new configuration afterwards. But the CvP host interface > requires every block to be exactly 4096 bytes, and the driver currently > breaks that rule for the last block. With full blocks, a truncated No, it's not the driver's responsibility. If you want the rule, the image should obey the rule, otherwise it's a broken image. > image is rejected with CVP_CONFIG_ERROR and a good image can be loaded > afterwards. Without them, the firmware is left waiting for the rest of > the last block and the device cannot be reconfigured. > > A truncated or corrupted image is input the driver can't rule out, so > it should report an error rather than leave the device stuck. The corrupted image are expected to fail, is it? As you said the firmware can already fail the case correctly. Why adding zero words? ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer 2026-12-02 12:48 ` Xu Yilun @ 2026-12-03 7:50 ` NG, TZE YEE 0 siblings, 0 replies; 6+ messages in thread From: NG, TZE YEE @ 2026-12-03 7:50 UTC (permalink / raw) To: Xu Yilun; +Cc: Moritz Fischer, Xu Yilun, Tom Rix, linux-fpga, linux-kernel Hi Yilun, On 2/12/2026 8:48 pm, Xu Yilun wrote: > On Tue, Dec 01, 2026 at 03:32:38PM +0800, NG, TZE YEE wrote: >> Hi Yilun, >> >> Thanks for the review. >> >> On 28/11/2026 10:15 pm, Xu Yilun wrote: >>> On Wed, Nov 18, 2026 at 03:43:15PM +0800, tze.yee.ng@altera.com wrote: >>>> From: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> >>>> >>>> The AIB CvP firmware requires a minimum transfer size of 4096 bytes. >>>> When a bitstream smaller than this is sent, the firmware DMA engine >>> >>> Only when < 4096? E.g. if a bitstream is 4096 + 1, it is good? >>> >> >> No, the log was wrong. The CvP firmware moves data in 4096-byte blocks, >> one per TX credit, and requires the host to send exactly 4096 bytes per >> block. The driver sends the last block with only the remaining bytes, >> so any image whose size is not a multiple of 4096 stalls on the last >> block, 4096 + 1 included. I'll correct this in v3. >> >>>> stalls waiting for the remaining data, causing a hang. >>>> >>>> Allocate a 4096-byte send_buf at probe time and copy host data into >>>> it before each block transfer, ensuring the full block size is always >>>> sent regardless of the actual data length. Also improve error >>>> detection in write_complete() by checking VSE_CVP_STATUS_CFG_ERR >>>> instead of VSE_UNCOR_ERR_CVP_CFG_ERR to catch truncated or >>>> mismatched bitstream errors. >>> >>> What VSE_CVP_STATUS_CFG_ERR checks? and what VSE_UNCOR_ERR_CVP_CFG_ERR >>> checks? why it is an improvement? Please specify in log. >>> >> >> VSE_UNCOR_ERR_CVP_CFG_ERR (CVP_CONFIG_ERROR_LATCHED) 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. >> >> VSE_CVP_STATUS_CFG_ERR (CVP_CONFIG_ERROR) is driven by the configuration >> firmware, which keeps it set until the next configuration starts, so it is >> still valid in write_complete(). Since the latched bit is only a sticky copy >> of it, checking CVP_CONFIG_ERROR covers every error the latched bit reports. >> I'll explain this in the v3 log. > > Then what's the connection to this 4096-byte requirement? Other errors > doesn't trigger VSE_UNCOR_ERR_CVP_CFG_ERR? > They are independent. Any configuration error sets the latched bit, and since the driver never clears it, every later load fails after one failed configuration, even with a good image. The padding lets the firmware detect a truncated image; the CVP_CONFIG_ERROR check lets the driver recover afterwards. Recovering from a truncated image needs both, which is why they are in one patch. >> >>>> >>>> Signed-off-by: Nazim Amirul <muhammad.nazim.amirul.nazle.asmade@altera.com> >>>> Signed-off-by: Tze Yee Ng <tze.yee.ng@altera.com> >>>> --- >>>> Note: I am taking over this patch from Nazim Amirul, who is no longer >>>> working on it. Nazim is kept as the author. >>>> >>>> Changes in v2: >>>> - Rebase from linux-fpga/for-next without code changes. >>> >>> I'm wondering if you agree with all his code/log so you don't change, or >>> you just wait for me to find all the issues anyway. >>> >> >> That's fair. I should have reviewed the patch properly before resending >> it instead of only rebasing it. I've gone through it now and reworked >> it as described below. >> >>>> >>>> v1: https://lore.kernel.org/all/20260619040811.20038-1-muhammad.nazim.amirul.nazle.asmade@altera.com/ >>>> --- >>>> drivers/fpga/altera-cvp.c | 18 +++++++++++++----- >>>> 1 file changed, 13 insertions(+), 5 deletions(-) >>>> >>>> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c >>>> index 103446465e0c..c9357cbe7fc0 100644 >>>> --- a/drivers/fpga/altera-cvp.c >>>> +++ b/drivers/fpga/altera-cvp.c >>>> @@ -82,6 +82,7 @@ struct altera_cvp_conf { >>>> u8 numclks; >>>> u32 sent_packets; >>>> u32 vsec_offset; >>>> + u8 *send_buf; >>>> const struct cvp_priv *priv; >>>> }; >>>> @@ -518,7 +519,9 @@ static int altera_cvp_write(struct fpga_manager *mgr, const char *buf, >>>> } >>>> len = min(conf->priv->block_size, remaining); >>>> - altera_cvp_send_block(conf, data, len); >>>> + memcpy(conf->send_buf, data, len); >>> >>> So you copied the whole bitstream just because of some bytes of unalignment? >>> >> Agreed, that was unnecessary. The copy was there so the last block >> could be sent at full length without reading past the end of the >> image. It also meant the last block was padded with leftover data from >> the previous block rather than zeros. >> >> v3 drops it: full blocks are written directly from the image buffer, >> and the last short block is completed with zero words in >> altera_cvp_send_block(), so nothing is read beyond the data. V1 devices >> use a 4-byte block size and are not affected. >> >>> Frankly speaking, I really think the image or the firmware should fix, >>> you can't produce a image that the FW can't handle. >>> >> >> The firmware side has been fixed: when the host terminates early, the >> firmware now completes the session, reports CVP_CONFIG_ERROR, and >> accepts a new configuration afterwards. But the CvP host interface >> requires every block to be exactly 4096 bytes, and the driver currently >> breaks that rule for the last block. With full blocks, a truncated > > No, it's not the driver's responsibility. If you want the rule, the > image should obey the rule, otherwise it's a broken image. > >> image is rejected with CVP_CONFIG_ERROR and a good image can be loaded >> afterwards. Without them, the firmware is left waiting for the rest of >> the last block and the device cannot be reconfigured. >> >> A truncated or corrupted image is input the driver can't rule out, so >> it should report an error rather than leave the device stuck. > > The corrupted image are expected to fail, is it? As you said the > firmware can already fail the case correctly. Why adding zero words? Yes, a broken image is expected to fail, and the padding does not make it pass. The rule is on the transfer between the host and the device, not on the image. The CvP specification says: The device transfers data between internal elements in exactly 4096 byte blocks. [...] the internal FIFO sizes combined with the Credit Tracking Mechanism [...] drive the requirement that transfers occur in 4096 byte blocks. Sending less than 4096 bytes at a time can result in a CVP stall due to a DMA waiting for completion. This is a hardware requirement of the CvP host interface, set by the device's internal FIFO sizes and credit tracking. The driver splits the image into blocks and sends one block per credit, so keeping every block at 4096 bytes is the driver's job, whatever the image contains. Today the driver sends the last block with only the remaining bytes, which breaks this requirement whenever the image size is not a multiple of 4096. The firmware can only fail an image on data it receives. A short last block can leave its DMA waiting for the rest of the block, as the specification describes, so the firmware may never raise CVP_CONFIG_ERROR and the device cannot be reconfigured. With the zero words, the firmware receives a full block, finds the image is bad and raises CVP_CONFIG_ERROR. Valid images are always a multiple of 4096 bytes, so the padding never triggers for them. The driver already zero-fills the last 1-3 bytes up to a full word; this extends it to a full block. v3 is already posted with these changes, and its commit log explains the 4096-byte requirement and the two error bits: https://lore.kernel.org/all/20261202082654.91189-1-tze.yee.ng@altera.com/ Could you take a look at v3 when you have time? If the explanation above should be in the commit log in more detail, I'll add it in v4. Thanks, Tze Yee ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-03 7:50 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-18 7:43 [PATCH v2] fpga: altera-cvp: Handle truncated bitstream with padding buffer tze.yee.ng 2026-11-26 2:11 ` NG, TZE YEE 2026-11-28 14:15 ` Xu Yilun 2026-12-01 7:32 ` NG, TZE YEE 2026-12-02 12:48 ` Xu Yilun 2026-12-03 7:50 ` NG, TZE YEE
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®