blob: 2b62bbbe3cf5d24608cddb58adea24cca123f059 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +05302/*
3 * (C) Copyright 2015 - 2016, Xilinx, Inc,
Michal Simeka8c94362023-07-10 14:35:49 +02004 * Michal Simek <michal.simek@amd.com>
Michal Simek7359cc22023-09-22 12:35:35 +02005 * Siva Durga Prasad Paladugu <siva.durga.prasad.paladugu@amd.com>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +05306 */
7
8#include <console.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <compiler.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Oleksandr Suvorov17773572022-07-22 17:16:13 +030011#include <fpga.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +053013#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010014#include <zynqmp_firmware.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +053017#include <linux/sizes.h>
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +053018#include <asm/arch/sys_proto.h>
Siva Durga Prasad Paladugue4d16c22018-03-15 00:17:24 +053019#include <memalign.h>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +053020
21#define DUMMY_WORD 0xffffffff
22
23/* Xilinx binary format header */
24static const u32 bin_format[] = {
25 DUMMY_WORD, /* Dummy words */
26 DUMMY_WORD,
27 DUMMY_WORD,
28 DUMMY_WORD,
29 DUMMY_WORD,
30 DUMMY_WORD,
31 DUMMY_WORD,
32 DUMMY_WORD,
33 DUMMY_WORD,
34 DUMMY_WORD,
35 DUMMY_WORD,
36 DUMMY_WORD,
37 DUMMY_WORD,
38 DUMMY_WORD,
39 DUMMY_WORD,
40 DUMMY_WORD,
41 0x000000bb, /* Sync word */
42 0x11220044, /* Sync word */
43 DUMMY_WORD,
44 DUMMY_WORD,
45 0xaa995566, /* Sync word */
46};
47
48#define SWAP_NO 1
49#define SWAP_DONE 2
50
51/*
52 * Load the whole word from unaligned buffer
53 * Keep in your mind that it is byte loading on little-endian system
54 */
55static u32 load_word(const void *buf, u32 swap)
56{
57 u32 word = 0;
58 u8 *bitc = (u8 *)buf;
59 int p;
60
61 if (swap == SWAP_NO) {
62 for (p = 0; p < 4; p++) {
63 word <<= 8;
64 word |= bitc[p];
65 }
66 } else {
67 for (p = 3; p >= 0; p--) {
68 word <<= 8;
69 word |= bitc[p];
70 }
71 }
72
73 return word;
74}
75
76static u32 check_header(const void *buf)
77{
78 u32 i, pattern;
79 int swap = SWAP_NO;
80 u32 *test = (u32 *)buf;
81
82 debug("%s: Let's check bitstream header\n", __func__);
83
84 /* Checking that passing bin is not a bitstream */
85 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
86 pattern = load_word(&test[i], swap);
87
88 /*
89 * Bitstreams in binary format are swapped
90 * compare to regular bistream.
91 * Do not swap dummy word but if swap is done assume
92 * that parsing buffer is binary format
93 */
94 if ((__swab32(pattern) != DUMMY_WORD) &&
95 (__swab32(pattern) == bin_format[i])) {
96 swap = SWAP_DONE;
97 debug("%s: data swapped - let's swap\n", __func__);
98 }
99
100 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
101 &test[i], pattern, bin_format[i]);
102 }
103 debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
104 buf, swap == SWAP_NO ? "without" : "with");
105
106 return swap;
107}
108
109static void *check_data(u8 *buf, size_t bsize, u32 *swap)
110{
111 u32 word, p = 0; /* possition */
112
113 /* Because buf doesn't need to be aligned let's read it by chars */
114 for (p = 0; p < bsize; p++) {
115 word = load_word(&buf[p], SWAP_NO);
116 debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
117
118 /* Find the first bitstream dummy word */
119 if (word == DUMMY_WORD) {
120 debug("%s: Found dummy word at position %x/%px\n",
121 __func__, p, &buf[p]);
122 *swap = check_header(&buf[p]);
123 if (*swap) {
124 /* FIXME add full bitstream checking here */
125 return &buf[p];
126 }
127 }
128 /* Loop can be huge - support CTRL + C */
129 if (ctrlc())
130 return NULL;
131 }
132 return NULL;
133}
134
135static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
136{
137 u32 *new_buf;
138 u32 i;
139
140 if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
141 new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
142
143 /*
144 * This might be dangerous but permits to flash if
145 * ARCH_DMA_MINALIGN is greater than header size
146 */
147 if (new_buf > (u32 *)buf) {
148 debug("%s: Aligned buffer is after buffer start\n",
149 __func__);
150 new_buf -= ARCH_DMA_MINALIGN;
151 }
152 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
153 buf, new_buf, swap);
154
155 for (i = 0; i < (len/4); i++)
156 new_buf[i] = load_word(&buf[i], swap);
157
158 buf = new_buf;
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530159 } else if ((swap != SWAP_DONE) &&
Ibai Erkiaga6aa5bc82019-09-27 11:37:02 +0100160 (zynqmp_firmware_version() <= PMUFW_V1_0)) {
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530161 /* For bitstream which are aligned */
Michal Simek27121142019-08-02 12:43:29 +0200162 new_buf = buf;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530163
164 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
165 swap);
166
167 for (i = 0; i < (len/4); i++)
168 new_buf[i] = load_word(&buf[i], swap);
169 }
170
171 return (ulong)buf;
172}
173
174static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
175 size_t bsize, u32 blocksize, u32 *swap)
176{
177 ulong *buf_start;
178 ulong diff;
179
180 buf_start = check_data((u8 *)buf, blocksize, swap);
181
182 if (!buf_start)
183 return FPGA_FAIL;
184
185 /* Check if data is postpone from start */
186 diff = (ulong)buf_start - (ulong)buf;
187 if (diff) {
188 printf("%s: Bitstream is not validated yet (diff %lx)\n",
189 __func__, diff);
190 return FPGA_FAIL;
191 }
192
193 if ((ulong)buf < SZ_1M) {
194 printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
195 __func__, buf);
196 return FPGA_FAIL;
197 }
198
199 return 0;
200}
201
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300202#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
203static int zynqmp_check_compatible(xilinx_desc *desc, int flags)
204{
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300205 /*
206 * If no flags set, the image may be legacy, but we need to
207 * signal caller this situation with specific error code.
208 */
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300209 if (!flags)
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300210 return -ENODATA;
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300211
212 /* For legacy bitstream images no need for other methods exist */
213 if ((flags & desc->flags) && flags == FPGA_LEGACY)
214 return 0;
215
216 /*
217 * Other images are handled in secure callback loads(). Check
218 * callback existence besides image type support.
219 */
220 if (desc->operations->loads && (flags & desc->flags))
221 return 0;
222
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300223 return -ENODEV;
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300224}
225#endif
226
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530227static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
Oleksandr Suvorovc0806cc2022-07-22 17:16:10 +0300228 bitstream_type bstype, int flags)
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530229{
Siva Durga Prasad Paladugue4d16c22018-03-15 00:17:24 +0530230 ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1);
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530231 u32 swap = 0;
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530232 ulong bin_buf;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530233 int ret;
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530234 u32 buf_lo, buf_hi;
Oleksandr Suvorovfd4c2722022-07-22 17:16:11 +0300235 u32 bsize_req = (u32)bsize;
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530236 u32 ret_payload[PAYLOAD_ARG_CNT];
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300237#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300238 struct fpga_secure_info info = { 0 };
239
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300240 ret = zynqmp_check_compatible(desc, flags);
241 if (ret) {
242 if (ret != -ENODATA) {
243 puts("Missing loads() operation or unsupported bitstream type\n");
244 return FPGA_FAIL;
245 }
246 /* If flags is not set, the image treats as legacy */
247 flags = FPGA_LEGACY;
248 }
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300249
250 switch (flags) {
251 case FPGA_LEGACY:
252 break; /* Handle the legacy image later in this function */
253#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
254 case FPGA_XILINX_ZYNQMP_DDRAUTH:
255 /* DDR authentication */
256 info.authflag = ZYNQMP_FPGA_AUTH_DDR;
257 info.encflag = FPGA_NO_ENC_OR_NO_AUTH;
258 return desc->operations->loads(desc, buf, bsize, &info);
Adrian Fiergolski0d6bb432022-07-22 17:16:14 +0300259 case FPGA_XILINX_ZYNQMP_ENC:
260 /* Encryption using device key */
261 info.authflag = FPGA_NO_ENC_OR_NO_AUTH;
262 info.encflag = FPGA_ENC_DEV_KEY;
263 return desc->operations->loads(desc, buf, bsize, &info);
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300264#endif
265 default:
266 printf("Unsupported bitstream type %d\n", flags);
267 return FPGA_FAIL;
268 }
Oleksandr Suvorovf1c9a7e2022-07-22 17:16:12 +0300269#endif
270
Ibai Erkiaga6aa5bc82019-09-27 11:37:02 +0100271 if (zynqmp_firmware_version() <= PMUFW_V1_0) {
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530272 puts("WARN: PMUFW v1.0 or less is detected\n");
273 puts("WARN: Not all bitstream formats are supported\n");
274 puts("WARN: Please upgrade PMUFW\n");
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530275 if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
276 return FPGA_FAIL;
277 bsizeptr = (u32 *)&bsize;
278 flush_dcache_range((ulong)bsizeptr,
279 (ulong)bsizeptr + sizeof(size_t));
Oleksandr Suvorovfd4c2722022-07-22 17:16:11 +0300280 bsize_req = (u32)(uintptr_t)bsizeptr;
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530281 bstype |= BIT(ZYNQMP_FPGA_BIT_NS);
Oleksandr Suvorovfd4c2722022-07-22 17:16:11 +0300282 } else {
283 bstype = 0;
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530284 }
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530285
286 bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
287
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530288 flush_dcache_range(bin_buf, bin_buf + bsize);
289
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530290 buf_lo = (u32)bin_buf;
291 buf_hi = upper_32_bits(bin_buf);
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530292
Oleksandr Suvorovfd4c2722022-07-22 17:16:11 +0300293 ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo, buf_hi,
294 bsize_req, bstype, ret_payload);
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530295 if (ret)
T Karthik Reddy274410a2020-05-14 07:49:36 -0600296 printf("PL FPGA LOAD failed with err: 0x%08x\n", ret);
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530297
298 return ret;
299}
300
Oleksandr Suvorovfbe31bb2022-07-22 17:16:02 +0300301#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530302static int zynqmp_loads(xilinx_desc *desc, const void *buf, size_t bsize,
303 struct fpga_secure_info *fpga_sec_info)
304{
305 int ret;
306 u32 buf_lo, buf_hi;
307 u32 ret_payload[PAYLOAD_ARG_CNT];
308 u8 flag = 0;
309
310 flush_dcache_range((ulong)buf, (ulong)buf +
311 ALIGN(bsize, CONFIG_SYS_CACHELINE_SIZE));
312
313 if (!fpga_sec_info->encflag)
314 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_DEV_KEY);
315
316 if (fpga_sec_info->userkey_addr &&
317 fpga_sec_info->encflag == FPGA_ENC_USR_KEY) {
318 flush_dcache_range((ulong)fpga_sec_info->userkey_addr,
319 (ulong)fpga_sec_info->userkey_addr +
320 ALIGN(KEY_PTR_LEN,
321 CONFIG_SYS_CACHELINE_SIZE));
322 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_USR_KEY);
323 }
324
325 if (!fpga_sec_info->authflag)
326 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_OCM);
327
328 if (fpga_sec_info->authflag == ZYNQMP_FPGA_AUTH_DDR)
329 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_DDR);
330
331 buf_lo = lower_32_bits((ulong)buf);
332 buf_hi = upper_32_bits((ulong)buf);
333
Neal Frager759e8302023-02-14 13:19:59 +0000334 if ((u32)(uintptr_t)fpga_sec_info->userkey_addr)
335 ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo,
Michal Simek4c3de372019-10-04 15:35:45 +0200336 buf_hi,
Neal Frager759e8302023-02-14 13:19:59 +0000337 (u32)(uintptr_t)fpga_sec_info->userkey_addr,
338 flag, ret_payload);
339 else
340 ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo,
341 buf_hi, (u32)bsize,
342 flag, ret_payload);
343
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530344 if (ret)
345 puts("PL FPGA LOAD fail\n");
346 else
347 puts("Bitstream successfully loaded\n");
348
349 return ret;
350}
351#endif
352
Nitin Jaind9361d42018-02-16 17:29:54 +0530353static int zynqmp_pcap_info(xilinx_desc *desc)
354{
355 int ret;
356 u32 ret_payload[PAYLOAD_ARG_CNT];
357
Michal Simek74076ba2020-09-09 13:25:40 +0200358 ret = xilinx_pm_request(PM_FPGA_GET_STATUS, 0, 0, 0,
Michal Simek4c3de372019-10-04 15:35:45 +0200359 0, ret_payload);
Nitin Jaind9361d42018-02-16 17:29:54 +0530360 if (!ret)
361 printf("PCAP status\t0x%x\n", ret_payload[1]);
362
363 return ret;
364}
365
Oleksandr Suvorov60ae6c12022-07-22 17:16:05 +0300366static int __maybe_unused zynqmp_str2flag(xilinx_desc *desc, const char *str)
367{
368 if (!strncmp(str, "u-boot,fpga-legacy", 18))
369 return FPGA_LEGACY;
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300370#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
371 if (!strncmp(str, "u-boot,zynqmp-fpga-ddrauth", 26))
372 return FPGA_XILINX_ZYNQMP_DDRAUTH;
Adrian Fiergolski0d6bb432022-07-22 17:16:14 +0300373
374 if (!strncmp(str, "u-boot,zynqmp-fpga-enc", 22))
375 return FPGA_XILINX_ZYNQMP_ENC;
Oleksandr Suvorov17773572022-07-22 17:16:13 +0300376#endif
Oleksandr Suvorov60ae6c12022-07-22 17:16:05 +0300377 return 0;
378}
379
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530380struct xilinx_fpga_op zynqmp_op = {
381 .load = zynqmp_load,
Oleksandr Suvorov60ae6c12022-07-22 17:16:05 +0300382 .info = zynqmp_pcap_info,
Oleksandr Suvorovfbe31bb2022-07-22 17:16:02 +0300383#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530384 .loads = zynqmp_loads,
Oleksandr Suvorov60ae6c12022-07-22 17:16:05 +0300385 .str2flag = zynqmp_str2flag,
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530386#endif
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530387};