[RFC PATCH 4/4] fpga: zynqmp: Add user-key encrypted FPGA Image loading support

From: Nava kishore Manne
Date: Tue May 04 2021 - 06:23:40 EST


This patch adds support to load the user-key encrypted FPGA
Image loading to the Xilinx ZynqMP Soc.

Signed-off-by: Nava kishore Manne <nava.manne@xxxxxxxxxx>
---
drivers/fpga/zynqmp-fpga.c | 24 ++++++++++++++++++++++--
include/linux/firmware/xlnx-zynqmp.h | 2 ++
2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
index 125743c9797f..565ebe9e1610 100644
--- a/drivers/fpga/zynqmp-fpga.c
+++ b/drivers/fpga/zynqmp-fpga.c
@@ -22,6 +22,8 @@
*/
struct zynqmp_fpga_priv {
struct device *dev;
+ const char *key_buf;
+ size_t key_size;
u32 flags;
};

@@ -33,6 +35,8 @@ static int zynqmp_fpga_ops_write_init(struct fpga_manager *mgr,

priv = mgr->priv;
priv->flags = info->flags;
+ priv->key_buf = info->enc_key_buf;
+ priv->key_size = info->enc_key_buf_size;

return 0;
}
@@ -41,9 +45,9 @@ static int zynqmp_fpga_ops_write(struct fpga_manager *mgr,
const char *buf, size_t size)
{
struct zynqmp_fpga_priv *priv;
- dma_addr_t dma_addr;
+ dma_addr_t dma_addr, key_addr;
u32 eemi_flags = 0;
- char *kbuf;
+ char *kbuf, *key_kbuf;
int ret;

priv = mgr->priv;
@@ -54,13 +58,29 @@ static int zynqmp_fpga_ops_write(struct fpga_manager *mgr,

memcpy(kbuf, buf, size);

+ if (priv->flags & FPGA_MGR_ENCRYPTED_USER_KEY_BITSTREAM) {
+ eemi_flags |= XILINX_ZYNQMP_PM_FPGA_ENC_USER_KEY;
+ key_kbuf = dma_alloc_coherent(priv->dev, size, &key_addr,
+ GFP_KERNEL);
+ if (!key_kbuf)
+ return -ENOMEM;
+ memcpy(key_kbuf, priv->key_buf, priv->key_size);
+ }
+
wmb(); /* ensure all writes are done before initiate FW call */

if (priv->flags & FPGA_MGR_PARTIAL_RECONFIG)
eemi_flags |= XILINX_ZYNQMP_PM_FPGA_PARTIAL;

+ if (priv->flags & FPGA_MGR_ENCRYPTED_USER_KEY_BITSTREAM)
+ ret = zynqmp_pm_fpga_enc_key_load(key_addr, priv->key_size);
+
ret = zynqmp_pm_fpga_load(dma_addr, size, eemi_flags);

+ if (priv->flags & FPGA_MGR_ENCRYPTED_USER_KEY_BITSTREAM)
+ dma_free_coherent(priv->dev, priv->key_size,
+ key_kbuf, key_addr);
+
dma_free_coherent(priv->dev, size, kbuf, dma_addr);

return ret;
diff --git a/include/linux/firmware/xlnx-zynqmp.h b/include/linux/firmware/xlnx-zynqmp.h
index 7aa9ad40ff53..a767386d930a 100644
--- a/include/linux/firmware/xlnx-zynqmp.h
+++ b/include/linux/firmware/xlnx-zynqmp.h
@@ -56,9 +56,11 @@
* Firmware FPGA Manager flags
* XILINX_ZYNQMP_PM_FPGA_FULL: FPGA full reconfiguration
* XILINX_ZYNQMP_PM_FPGA_PARTIAL: FPGA partial reconfiguration
+ * XILINX_ZYNQMP_PM_FPGA_ENC_USER_KEY: User-key Encrypted FPGA reconfiguration
*/
#define XILINX_ZYNQMP_PM_FPGA_FULL 0x0U
#define XILINX_ZYNQMP_PM_FPGA_PARTIAL BIT(0)
+#define XILINX_ZYNQMP_PM_FPGA_ENC_USER_KEY BIT(3)

enum pm_api_id {
PM_GET_API_VERSION = 1,
--
2.17.1