2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-01-13 10:55:37 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 - 2016, Xilinx, Inc,
|
|
|
|
* Michal Simek <michal.simek@xilinx.com>
|
|
|
|
* Siva Durga Prasad <siva.durga.paladugu@xilinx.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <console.h>
|
|
|
|
#include <common.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <compiler.h>
|
2019-11-14 19:57:39 +00:00
|
|
|
#include <cpu_func.h>
|
2022-07-22 14:16:13 +00:00
|
|
|
#include <fpga.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2016-01-13 10:55:37 +00:00
|
|
|
#include <zynqmppl.h>
|
2019-09-27 10:37:01 +00:00
|
|
|
#include <zynqmp_firmware.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2016-01-13 10:55:37 +00:00
|
|
|
#include <linux/sizes.h>
|
2017-02-17 10:46:01 +00:00
|
|
|
#include <asm/arch/sys_proto.h>
|
2018-03-14 18:47:24 +00:00
|
|
|
#include <memalign.h>
|
2016-01-13 10:55:37 +00:00
|
|
|
|
|
|
|
#define DUMMY_WORD 0xffffffff
|
|
|
|
|
|
|
|
/* Xilinx binary format header */
|
|
|
|
static const u32 bin_format[] = {
|
|
|
|
DUMMY_WORD, /* Dummy words */
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
0x000000bb, /* Sync word */
|
|
|
|
0x11220044, /* Sync word */
|
|
|
|
DUMMY_WORD,
|
|
|
|
DUMMY_WORD,
|
|
|
|
0xaa995566, /* Sync word */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SWAP_NO 1
|
|
|
|
#define SWAP_DONE 2
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the whole word from unaligned buffer
|
|
|
|
* Keep in your mind that it is byte loading on little-endian system
|
|
|
|
*/
|
|
|
|
static u32 load_word(const void *buf, u32 swap)
|
|
|
|
{
|
|
|
|
u32 word = 0;
|
|
|
|
u8 *bitc = (u8 *)buf;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
if (swap == SWAP_NO) {
|
|
|
|
for (p = 0; p < 4; p++) {
|
|
|
|
word <<= 8;
|
|
|
|
word |= bitc[p];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (p = 3; p >= 0; p--) {
|
|
|
|
word <<= 8;
|
|
|
|
word |= bitc[p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 check_header(const void *buf)
|
|
|
|
{
|
|
|
|
u32 i, pattern;
|
|
|
|
int swap = SWAP_NO;
|
|
|
|
u32 *test = (u32 *)buf;
|
|
|
|
|
|
|
|
debug("%s: Let's check bitstream header\n", __func__);
|
|
|
|
|
|
|
|
/* Checking that passing bin is not a bitstream */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
|
|
|
|
pattern = load_word(&test[i], swap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bitstreams in binary format are swapped
|
|
|
|
* compare to regular bistream.
|
|
|
|
* Do not swap dummy word but if swap is done assume
|
|
|
|
* that parsing buffer is binary format
|
|
|
|
*/
|
|
|
|
if ((__swab32(pattern) != DUMMY_WORD) &&
|
|
|
|
(__swab32(pattern) == bin_format[i])) {
|
|
|
|
swap = SWAP_DONE;
|
|
|
|
debug("%s: data swapped - let's swap\n", __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
|
|
|
|
&test[i], pattern, bin_format[i]);
|
|
|
|
}
|
|
|
|
debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
|
|
|
|
buf, swap == SWAP_NO ? "without" : "with");
|
|
|
|
|
|
|
|
return swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *check_data(u8 *buf, size_t bsize, u32 *swap)
|
|
|
|
{
|
|
|
|
u32 word, p = 0; /* possition */
|
|
|
|
|
|
|
|
/* Because buf doesn't need to be aligned let's read it by chars */
|
|
|
|
for (p = 0; p < bsize; p++) {
|
|
|
|
word = load_word(&buf[p], SWAP_NO);
|
|
|
|
debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
|
|
|
|
|
|
|
|
/* Find the first bitstream dummy word */
|
|
|
|
if (word == DUMMY_WORD) {
|
|
|
|
debug("%s: Found dummy word at position %x/%px\n",
|
|
|
|
__func__, p, &buf[p]);
|
|
|
|
*swap = check_header(&buf[p]);
|
|
|
|
if (*swap) {
|
|
|
|
/* FIXME add full bitstream checking here */
|
|
|
|
return &buf[p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Loop can be huge - support CTRL + C */
|
|
|
|
if (ctrlc())
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
|
|
|
|
{
|
|
|
|
u32 *new_buf;
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
|
|
|
|
new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This might be dangerous but permits to flash if
|
|
|
|
* ARCH_DMA_MINALIGN is greater than header size
|
|
|
|
*/
|
|
|
|
if (new_buf > (u32 *)buf) {
|
|
|
|
debug("%s: Aligned buffer is after buffer start\n",
|
|
|
|
__func__);
|
|
|
|
new_buf -= ARCH_DMA_MINALIGN;
|
|
|
|
}
|
|
|
|
printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
|
|
|
|
buf, new_buf, swap);
|
|
|
|
|
|
|
|
for (i = 0; i < (len/4); i++)
|
|
|
|
new_buf[i] = load_word(&buf[i], swap);
|
|
|
|
|
|
|
|
buf = new_buf;
|
2018-08-21 10:14:50 +00:00
|
|
|
} else if ((swap != SWAP_DONE) &&
|
2019-09-27 10:37:02 +00:00
|
|
|
(zynqmp_firmware_version() <= PMUFW_V1_0)) {
|
2016-01-13 10:55:37 +00:00
|
|
|
/* For bitstream which are aligned */
|
2019-08-02 10:43:29 +00:00
|
|
|
new_buf = buf;
|
2016-01-13 10:55:37 +00:00
|
|
|
|
|
|
|
printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
|
|
|
|
swap);
|
|
|
|
|
|
|
|
for (i = 0; i < (len/4); i++)
|
|
|
|
new_buf[i] = load_word(&buf[i], swap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ulong)buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
|
|
|
|
size_t bsize, u32 blocksize, u32 *swap)
|
|
|
|
{
|
|
|
|
ulong *buf_start;
|
|
|
|
ulong diff;
|
|
|
|
|
|
|
|
buf_start = check_data((u8 *)buf, blocksize, swap);
|
|
|
|
|
|
|
|
if (!buf_start)
|
|
|
|
return FPGA_FAIL;
|
|
|
|
|
|
|
|
/* Check if data is postpone from start */
|
|
|
|
diff = (ulong)buf_start - (ulong)buf;
|
|
|
|
if (diff) {
|
|
|
|
printf("%s: Bitstream is not validated yet (diff %lx)\n",
|
|
|
|
__func__, diff);
|
|
|
|
return FPGA_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ulong)buf < SZ_1M) {
|
|
|
|
printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
|
|
|
|
__func__, buf);
|
|
|
|
return FPGA_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:16:12 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
|
|
|
|
static int zynqmp_check_compatible(xilinx_desc *desc, int flags)
|
|
|
|
{
|
2022-07-22 14:16:13 +00:00
|
|
|
/*
|
|
|
|
* If no flags set, the image may be legacy, but we need to
|
|
|
|
* signal caller this situation with specific error code.
|
|
|
|
*/
|
2022-07-22 14:16:12 +00:00
|
|
|
if (!flags)
|
2022-07-22 14:16:13 +00:00
|
|
|
return -ENODATA;
|
2022-07-22 14:16:12 +00:00
|
|
|
|
|
|
|
/* For legacy bitstream images no need for other methods exist */
|
|
|
|
if ((flags & desc->flags) && flags == FPGA_LEGACY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Other images are handled in secure callback loads(). Check
|
|
|
|
* callback existence besides image type support.
|
|
|
|
*/
|
|
|
|
if (desc->operations->loads && (flags & desc->flags))
|
|
|
|
return 0;
|
|
|
|
|
2022-07-22 14:16:13 +00:00
|
|
|
return -ENODEV;
|
2022-07-22 14:16:12 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-13 10:55:37 +00:00
|
|
|
static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
|
2022-07-22 14:16:10 +00:00
|
|
|
bitstream_type bstype, int flags)
|
2016-01-13 10:55:37 +00:00
|
|
|
{
|
2018-03-14 18:47:24 +00:00
|
|
|
ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1);
|
2018-08-21 10:14:50 +00:00
|
|
|
u32 swap = 0;
|
2017-02-17 10:46:01 +00:00
|
|
|
ulong bin_buf;
|
2016-01-13 10:55:37 +00:00
|
|
|
int ret;
|
2017-02-17 10:46:01 +00:00
|
|
|
u32 buf_lo, buf_hi;
|
2022-07-22 14:16:11 +00:00
|
|
|
u32 bsize_req = (u32)bsize;
|
2017-02-17 10:46:01 +00:00
|
|
|
u32 ret_payload[PAYLOAD_ARG_CNT];
|
2022-07-22 14:16:12 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
|
2022-07-22 14:16:13 +00:00
|
|
|
struct fpga_secure_info info = { 0 };
|
|
|
|
|
2022-07-22 14:16:12 +00:00
|
|
|
ret = zynqmp_check_compatible(desc, flags);
|
|
|
|
if (ret) {
|
|
|
|
if (ret != -ENODATA) {
|
|
|
|
puts("Missing loads() operation or unsupported bitstream type\n");
|
|
|
|
return FPGA_FAIL;
|
|
|
|
}
|
|
|
|
/* If flags is not set, the image treats as legacy */
|
|
|
|
flags = FPGA_LEGACY;
|
|
|
|
}
|
2022-07-22 14:16:13 +00:00
|
|
|
|
|
|
|
switch (flags) {
|
|
|
|
case FPGA_LEGACY:
|
|
|
|
break; /* Handle the legacy image later in this function */
|
|
|
|
#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
|
|
|
|
case FPGA_XILINX_ZYNQMP_DDRAUTH:
|
|
|
|
/* DDR authentication */
|
|
|
|
info.authflag = ZYNQMP_FPGA_AUTH_DDR;
|
|
|
|
info.encflag = FPGA_NO_ENC_OR_NO_AUTH;
|
|
|
|
return desc->operations->loads(desc, buf, bsize, &info);
|
2022-07-22 14:16:14 +00:00
|
|
|
case FPGA_XILINX_ZYNQMP_ENC:
|
|
|
|
/* Encryption using device key */
|
|
|
|
info.authflag = FPGA_NO_ENC_OR_NO_AUTH;
|
|
|
|
info.encflag = FPGA_ENC_DEV_KEY;
|
|
|
|
return desc->operations->loads(desc, buf, bsize, &info);
|
2022-07-22 14:16:13 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
printf("Unsupported bitstream type %d\n", flags);
|
|
|
|
return FPGA_FAIL;
|
|
|
|
}
|
2022-07-22 14:16:12 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-27 10:37:02 +00:00
|
|
|
if (zynqmp_firmware_version() <= PMUFW_V1_0) {
|
2018-08-21 10:14:50 +00:00
|
|
|
puts("WARN: PMUFW v1.0 or less is detected\n");
|
|
|
|
puts("WARN: Not all bitstream formats are supported\n");
|
|
|
|
puts("WARN: Please upgrade PMUFW\n");
|
|
|
|
if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
|
|
|
|
return FPGA_FAIL;
|
|
|
|
bsizeptr = (u32 *)&bsize;
|
|
|
|
flush_dcache_range((ulong)bsizeptr,
|
|
|
|
(ulong)bsizeptr + sizeof(size_t));
|
2022-07-22 14:16:11 +00:00
|
|
|
bsize_req = (u32)(uintptr_t)bsizeptr;
|
2018-08-21 10:14:50 +00:00
|
|
|
bstype |= BIT(ZYNQMP_FPGA_BIT_NS);
|
2022-07-22 14:16:11 +00:00
|
|
|
} else {
|
|
|
|
bstype = 0;
|
2018-08-21 10:14:50 +00:00
|
|
|
}
|
2016-01-13 10:55:37 +00:00
|
|
|
|
|
|
|
bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
|
|
|
|
|
|
|
|
flush_dcache_range(bin_buf, bin_buf + bsize);
|
|
|
|
|
2017-02-17 10:46:01 +00:00
|
|
|
buf_lo = (u32)bin_buf;
|
|
|
|
buf_hi = upper_32_bits(bin_buf);
|
2018-08-21 10:14:50 +00:00
|
|
|
|
2022-07-22 14:16:11 +00:00
|
|
|
ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo, buf_hi,
|
|
|
|
bsize_req, bstype, ret_payload);
|
2016-01-13 10:55:37 +00:00
|
|
|
if (ret)
|
2020-05-14 13:49:36 +00:00
|
|
|
printf("PL FPGA LOAD failed with err: 0x%08x\n", ret);
|
2016-01-13 10:55:37 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:16:02 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
|
2018-05-31 09:40:23 +00:00
|
|
|
static int zynqmp_loads(xilinx_desc *desc, const void *buf, size_t bsize,
|
|
|
|
struct fpga_secure_info *fpga_sec_info)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 buf_lo, buf_hi;
|
|
|
|
u32 ret_payload[PAYLOAD_ARG_CNT];
|
|
|
|
u8 flag = 0;
|
|
|
|
|
|
|
|
flush_dcache_range((ulong)buf, (ulong)buf +
|
|
|
|
ALIGN(bsize, CONFIG_SYS_CACHELINE_SIZE));
|
|
|
|
|
|
|
|
if (!fpga_sec_info->encflag)
|
|
|
|
flag |= BIT(ZYNQMP_FPGA_BIT_ENC_DEV_KEY);
|
|
|
|
|
|
|
|
if (fpga_sec_info->userkey_addr &&
|
|
|
|
fpga_sec_info->encflag == FPGA_ENC_USR_KEY) {
|
|
|
|
flush_dcache_range((ulong)fpga_sec_info->userkey_addr,
|
|
|
|
(ulong)fpga_sec_info->userkey_addr +
|
|
|
|
ALIGN(KEY_PTR_LEN,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE));
|
|
|
|
flag |= BIT(ZYNQMP_FPGA_BIT_ENC_USR_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fpga_sec_info->authflag)
|
|
|
|
flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_OCM);
|
|
|
|
|
|
|
|
if (fpga_sec_info->authflag == ZYNQMP_FPGA_AUTH_DDR)
|
|
|
|
flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_DDR);
|
|
|
|
|
|
|
|
buf_lo = lower_32_bits((ulong)buf);
|
|
|
|
buf_hi = upper_32_bits((ulong)buf);
|
|
|
|
|
2023-02-14 13:19:59 +00:00
|
|
|
if ((u32)(uintptr_t)fpga_sec_info->userkey_addr)
|
|
|
|
ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo,
|
2019-10-04 13:35:45 +00:00
|
|
|
buf_hi,
|
2023-02-14 13:19:59 +00:00
|
|
|
(u32)(uintptr_t)fpga_sec_info->userkey_addr,
|
|
|
|
flag, ret_payload);
|
|
|
|
else
|
|
|
|
ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo,
|
|
|
|
buf_hi, (u32)bsize,
|
|
|
|
flag, ret_payload);
|
|
|
|
|
2018-05-31 09:40:23 +00:00
|
|
|
if (ret)
|
|
|
|
puts("PL FPGA LOAD fail\n");
|
|
|
|
else
|
|
|
|
puts("Bitstream successfully loaded\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-16 11:59:54 +00:00
|
|
|
static int zynqmp_pcap_info(xilinx_desc *desc)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 ret_payload[PAYLOAD_ARG_CNT];
|
|
|
|
|
2020-09-09 11:25:40 +00:00
|
|
|
ret = xilinx_pm_request(PM_FPGA_GET_STATUS, 0, 0, 0,
|
2019-10-04 13:35:45 +00:00
|
|
|
0, ret_payload);
|
2018-02-16 11:59:54 +00:00
|
|
|
if (!ret)
|
|
|
|
printf("PCAP status\t0x%x\n", ret_payload[1]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:16:05 +00:00
|
|
|
static int __maybe_unused zynqmp_str2flag(xilinx_desc *desc, const char *str)
|
|
|
|
{
|
|
|
|
if (!strncmp(str, "u-boot,fpga-legacy", 18))
|
|
|
|
return FPGA_LEGACY;
|
2022-07-22 14:16:13 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
|
|
|
|
if (!strncmp(str, "u-boot,zynqmp-fpga-ddrauth", 26))
|
|
|
|
return FPGA_XILINX_ZYNQMP_DDRAUTH;
|
2022-07-22 14:16:14 +00:00
|
|
|
|
|
|
|
if (!strncmp(str, "u-boot,zynqmp-fpga-enc", 22))
|
|
|
|
return FPGA_XILINX_ZYNQMP_ENC;
|
2022-07-22 14:16:13 +00:00
|
|
|
#endif
|
2022-07-22 14:16:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-13 10:55:37 +00:00
|
|
|
struct xilinx_fpga_op zynqmp_op = {
|
|
|
|
.load = zynqmp_load,
|
2022-07-22 14:16:05 +00:00
|
|
|
.info = zynqmp_pcap_info,
|
2022-07-22 14:16:02 +00:00
|
|
|
#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
|
2018-05-31 09:40:23 +00:00
|
|
|
.loads = zynqmp_loads,
|
2022-07-22 14:16:05 +00:00
|
|
|
.str2flag = zynqmp_str2flag,
|
2018-05-31 09:40:23 +00:00
|
|
|
#endif
|
2016-01-13 10:55:37 +00:00
|
|
|
};
|