From: jgunthorpe@obsidianresearch.com (Jason Gunthorpe)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] fpga zynq: Check the bitstream for validity
Date: Wed, 26 Oct 2016 16:54:13 -0600 [thread overview]
Message-ID: <20161026225413.GA6220@obsidianresearch.com> (raw)
There is no sense in sending a bitstream we know will not work, and
with the variety of options for bitstream generation in Xilinx tools
it is not terribly clear or very well documented what the correct
input should be, especially since auto-detection was removed from this
driver.
All Zynq full configuration bitstreams must start with the sync word in
the correct byte order.
Zynq is also only able to DMA dword quantities, so bitstreams must be
a multiple of 4 bytes. This also fixes a DMA-past the end bug.
Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
---
drivers/fpga/zynq-fpga.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
index c2fb4120bd62..46a38772e7ee 100644
--- a/drivers/fpga/zynq-fpga.c
+++ b/drivers/fpga/zynq-fpga.c
@@ -184,12 +184,26 @@ static int zynq_fpga_ops_write_init(struct fpga_manager *mgr, u32 flags,
priv = mgr->priv;
+ /* All valid bitstreams are multiples of 32 bits */
+ if ((count % 4) != 0)
+ return -EINVAL;
+
err = clk_enable(priv->clk);
if (err)
return err;
/* don't globally reset PL if we're doing partial reconfig */
if (!(flags & FPGA_MGR_PARTIAL_RECONFIG)) {
+ /* Sanity check the proposed bitstream. It must start with the
+ * sync word in the correct byte order and be a multiple of 4
+ * bytes.
+ */
+ if (count <= 4 || buf[0] != 0x66 || buf[1] != 0x55 ||
+ buf[2] != 0x99 || buf[3] != 0xaa) {
+ err = -EINVAL;
+ goto out_err;
+ }
+
/* assert AXI interface resets */
regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
FPGA_RST_ALL_MASK);
@@ -287,12 +301,9 @@ static int zynq_fpga_ops_write(struct fpga_manager *mgr,
struct zynq_fpga_priv *priv;
int err;
char *kbuf;
- size_t in_count;
dma_addr_t dma_addr;
- u32 transfer_length;
u32 intr_status;
- in_count = count;
priv = mgr->priv;
kbuf = dma_alloc_coherent(priv->dev, count, &dma_addr, GFP_KERNEL);
@@ -318,11 +329,7 @@ static int zynq_fpga_ops_write(struct fpga_manager *mgr,
*/
zynq_fpga_write(priv, DMA_SRC_ADDR_OFFSET, (u32)(dma_addr) + 1);
zynq_fpga_write(priv, DMA_DST_ADDR_OFFSET, (u32)DMA_INVALID_ADDRESS);
-
- /* convert #bytes to #words */
- transfer_length = (count + 3) / 4;
-
- zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, transfer_length);
+ zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, count / 4);
zynq_fpga_write(priv, DMA_DEST_LEN_OFFSET, 0);
wait_for_completion(&priv->dma_done);
@@ -338,7 +345,7 @@ static int zynq_fpga_ops_write(struct fpga_manager *mgr,
clk_disable(priv->clk);
out_free:
- dma_free_coherent(priv->dev, in_count, kbuf, dma_addr);
+ dma_free_coherent(priv->dev, count, kbuf, dma_addr);
return err;
}
--
2.1.4
next reply other threads:[~2016-10-26 22:54 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-26 22:54 Jason Gunthorpe [this message]
2016-10-27 7:42 ` [PATCH] fpga zynq: Check the bitstream for validity Michal Simek
2016-10-27 14:32 ` Jason Gunthorpe
2016-10-27 8:50 ` Matthias Brugger
2016-10-27 14:39 ` Jason Gunthorpe
2016-10-28 11:06 ` Matthias Brugger
2016-10-28 15:47 ` Jason Gunthorpe
2016-10-28 16:36 ` Matthias Brugger
2016-10-28 16:55 ` Jason Gunthorpe
2016-10-28 18:23 ` Moritz Fischer
2016-10-28 20:26 ` Jason Gunthorpe
2016-10-28 21:00 ` Moritz Fischer
2016-10-28 22:05 ` Jason Gunthorpe
2016-10-29 0:09 ` Moritz Fischer
2016-10-31 16:23 ` Jason Gunthorpe
2016-11-01 6:39 ` Michal Simek
2016-11-01 15:33 ` Jason Gunthorpe
2016-11-01 17:48 ` Michal Simek
2016-11-08 0:05 ` Jason Gunthorpe
2016-11-09 14:21 ` Mike Looijmans
2016-11-09 15:18 ` Matthias Brugger
2016-11-09 16:00 ` Jason Gunthorpe
2016-11-09 15:56 ` Jason Gunthorpe
2016-11-09 17:31 ` Mike Looijmans
2016-11-28 18:00 ` Jason Gunthorpe
2016-11-08 0:46 ` Jason Gunthorpe
2016-11-08 9:59 ` Matthias Brugger
2016-11-08 16:24 ` Jason Gunthorpe
2016-10-28 11:12 ` Matthias Brugger
2016-10-28 15:48 ` Jason Gunthorpe
2016-10-28 16:37 ` Matthias Brugger
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=20161026225413.GA6220@obsidianresearch.com \
--to=jgunthorpe@obsidianresearch.com \
--cc=linux-arm-kernel@lists.infradead.org \
/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).