summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-imx/atf.c
blob: 8b804602681b3200023fe1a838f435414bfb135a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// SPDX-License-Identifier: GPL-2.0-only

#include <asm/sections.h>
#include <common.h>
#include <firmware.h>
#include <mach/imx/atf.h>
#include <mach/imx/generic.h>
#include <mach/imx/xload.h>
#include <mach/imx/romapi.h>
#include <mach/imx/esdctl.h>
#include <asm-generic/memory_layout.h>
#include <asm/barebox-arm.h>
#include <mach/imx/imx8m-regs.h>
#include <mach/imx/imx9-regs.h>
#include <soc/fsl/fsl_udc.h>
#include <soc/fsl/caam.h>
#include <tee/optee.h>
#include <mach/imx/ele.h>

/**
 * imx8m_atf_load_bl31 - Load ATF BL31 blob and transfer control to it
 *
 * @fw:		Pointer to the BL31 blob
 * @fw_size:	Size of the BL31 blob
 * @atf_dest:	Place where the BL31 is copied to and executed
 *
 * This function:
 *
 *     1. Copies built-in BL31 blob to an address i.MX8M's BL31
 *        expects to be placed (TF-A v2.8+ is position-independent)
 *
 *     2. Sets up temporary stack pointer for EL2, which is execution
 *        level that BL31 will drop us off at after it completes its
 *        initialization routine
 *
 *     3. Transfers control to BL31
 */

static __noreturn void imx8m_atf_start_bl31(const void *fw, size_t fw_size, void *atf_dest)
{
	void __noreturn (*bl31)(void) = atf_dest;
	int ret;

	BUG_ON(fw_size > MX8M_ATF_BL31_SIZE_LIMIT);

	if (IS_ENABLED(CONFIG_FSL_CAAM_RNG_PBL_INIT)) {
		ret = imx_early_caam_init(MX8M_CAAM_BASE_ADDR);
		if (ret)
			pr_debug("CAAM early init failed: %d\n", ret);
		else
			pr_debug("CAAM early init successful\n");
	}

	memcpy(bl31, fw, fw_size);

	asm volatile("msr sp_el2, %0" : :
		     "r" (atf_dest - 16) :
		     "cc");
	bl31();
	__builtin_unreachable();
}

void imx8mm_load_bl33(void *bl33)
{
	enum bootsource src;
	int instance;

	imx8mm_get_boot_source(&src, &instance);
	switch (src) {
	case BOOTSOURCE_MMC:
		imx8m_esdhc_load_image(instance, bl33);
		break;
	case BOOTSOURCE_SERIAL:
		if (!IS_ENABLED(CONFIG_USB_GADGET_DRIVER_ARC_PBL)) {
			printf("Serial bootmode not supported\n");
			break;
		}

		/*
		 * Traditionally imx-usb-loader sends the PBL twice. The first
		 * PBL is loaded to OCRAM and executed. Then the full barebox
		 * image including the PBL is sent again and received here. We
		 * might change that in the future in imx-usb-loader so that the
		 * PBL is sent only once and we only receive the rest of the
		 * image here. To prepare that step we check if we get a full
		 * barebox image or piggydata only. When it's piggydata only move
		 * it to the place where it would be if it would have been a
		 * full image.
		 */
		imx8mm_barebox_load_usb(bl33);

		if (!strcmp("barebox", bl33 + 0x20)) {
			/* Found the barebox marker, so this is a PBL + piggydata */
			pr_debug("received PBL + piggydata\n");
		} else {
			/* no barebox marker, so this is piggydata only */
			pr_debug("received piggydata\n");
			memmove(bl33 + barebox_pbl_size, bl33,
				barebox_image_size - barebox_pbl_size);
		}

		break;
	case BOOTSOURCE_SPI:
		imx8mm_qspi_load_image(instance, bl33);
		break;
	default:
		printf("Unsupported bootsource BOOTSOURCE_%d\n", src);
		hang();
	}

	/*
	 * On completion the TF-A will jump to MX8M_ATF_BL33_BASE_ADDR
	 * in EL2. Copy the image there, but replace the PBL part of
	 * that image with ourselves. On a high assurance boot only the
	 * currently running code is validated and contains the checksum
	 * for the piggy data, so we need to ensure that we are running
	 * the same code in DRAM.
	 */
	memcpy(bl33, __image_start, barebox_pbl_size);
}

static void imx_adjust_optee_memory(void **bl32, void **bl32_image, size_t *bl32_size)
{
	struct optee_header *hdr = *bl32_image;
	u64 membase;

	if (optee_verify_header(hdr))
		return;

	imx_scratch_save_optee_hdr(hdr);

	membase = (u64)hdr->init_load_addr_hi << 32;
	membase |= hdr->init_load_addr_lo;

	*bl32 = (void *)membase;
	*bl32_size -= sizeof(*hdr);
	*bl32_image += sizeof(*hdr);
}

__noreturn void imx8mm_load_and_start_image_via_tfa(void)
{
	__imx8mm_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
}

__noreturn void __imx8mm_load_and_start_image_via_tfa(void *bl33)
{
	const void *bl31;
	size_t bl31_size;
	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);

	imx_set_cpu_type(IMX_CPU_IMX8MM);
	imx8mm_init_scratch_space();
	imx8m_save_bootrom_log();
	imx8mm_load_bl33(bl33);

	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MM_OPTEE)) {
		void *bl32 = (void *)arm_mem_optee(endmem);
		size_t bl32_size;
		void *bl32_image;

		imx8m_tzc380_init();
		get_builtin_firmware_ext(imx8mm_bl32_bin,
				bl33, &bl32_image,
				&bl32_size);

		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);

		memcpy(bl32, bl32_image, bl32_size);

		get_builtin_firmware(imx8mm_bl31_bin_optee, &bl31, &bl31_size);
	} else {
		get_builtin_firmware(imx8mm_bl31_bin, &bl31, &bl31_size);
	}

	imx8m_atf_start_bl31(bl31, bl31_size, (void *)MX8MM_ATF_BL31_BASE_ADDR);
}

void imx8mp_load_bl33(void *bl33)
{
	enum bootsource src;
	int instance;

	imx8mp_get_boot_source(&src, &instance);
	switch (src) {
	case BOOTSOURCE_MMC:
		imx8mp_esdhc_load_image(instance, bl33);
		break;
	case BOOTSOURCE_SERIAL:
		imx8mp_romapi_load_image(bl33);
		break;
	case BOOTSOURCE_SPI:
		imx8mp_qspi_load_image(instance, bl33);
		break;
	default:
		printf("Unhandled bootsource BOOTSOURCE_%d\n", src);
		hang();
	}


	/*
	 * On completion the TF-A will jump to MX8M_ATF_BL33_BASE_ADDR
	 * in EL2. Copy the image there, but replace the PBL part of
	 * that image with ourselves. On a high assurance boot only the
	 * currently running code is validated and contains the checksum
	 * for the piggy data, so we need to ensure that we are running
	 * the same code in DRAM.
	 */
	memcpy(bl33, __image_start, barebox_pbl_size);
}

__noreturn void imx8mp_load_and_start_image_via_tfa(void)
{
	__imx8mp_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
}

__noreturn void __imx8mp_load_and_start_image_via_tfa(void *bl33)
{
	const void *bl31;
	size_t bl31_size;
	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);

	imx_set_cpu_type(IMX_CPU_IMX8MP);
	imx8mp_init_scratch_space();
	imx8m_save_bootrom_log();
	imx8mp_load_bl33(bl33);

	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MP_OPTEE)) {
		void *bl32 = (void *)arm_mem_optee(endmem);
		size_t bl32_size;
		void *bl32_image;

		imx8m_tzc380_init();
		get_builtin_firmware_ext(imx8mp_bl32_bin,
				bl33, &bl32_image,
				&bl32_size);

		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);

		memcpy(bl32, bl32_image, bl32_size);

		get_builtin_firmware(imx8mp_bl31_bin_optee, &bl31, &bl31_size);
	} else {
		get_builtin_firmware(imx8mp_bl31_bin, &bl31, &bl31_size);
	}

	imx8m_atf_start_bl31(bl31, bl31_size, (void *)MX8MP_ATF_BL31_BASE_ADDR);
}


void imx8mn_load_bl33(void *bl33)
{
	enum bootsource src;
	int instance;

	imx8mn_get_boot_source(&src, &instance);
	switch (src) {
	case BOOTSOURCE_MMC:
		imx8mn_esdhc_load_image(instance, bl33);
		break;
	case BOOTSOURCE_SERIAL:
		imx8mn_romapi_load_image(bl33);
		break;
	case BOOTSOURCE_SPI:
		imx8mn_qspi_load_image(instance, bl33);
		break;
	default:
		printf("Unhandled bootsource BOOTSOURCE_%d\n", src);
		hang();
	}


	/*
	 * On completion the TF-A will jump to MX8M_ATF_BL33_BASE_ADDR
	 * in EL2. Copy the image there, but replace the PBL part of
	 * that image with ourselves. On a high assurance boot only the
	 * currently running code is validated and contains the checksum
	 * for the piggy data, so we need to ensure that we are running
	 * the same code in DRAM.
	 */
	memcpy(bl33, __image_start, barebox_pbl_size);
}

__noreturn void imx8mn_load_and_start_image_via_tfa(void)
{
	__imx8mn_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
}

__noreturn void __imx8mn_load_and_start_image_via_tfa(void *bl33)
{
	const void *bl31;
	size_t bl31_size;
	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);

	imx_set_cpu_type(IMX_CPU_IMX8MN);
	imx8mn_init_scratch_space();
	imx8m_save_bootrom_log();
	imx8mn_load_bl33(bl33);

	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MN_OPTEE)) {
		void *bl32 = (void *)arm_mem_optee(endmem);
		size_t bl32_size;
		void *bl32_image;

		imx8m_tzc380_init();
		get_builtin_firmware_ext(imx8mn_bl32_bin,
				bl33, &bl32_image,
				&bl32_size);

		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);

		memcpy(bl32, bl32_image, bl32_size);

		get_builtin_firmware(imx8mn_bl31_bin_optee, &bl31, &bl31_size);
	} else {
		get_builtin_firmware(imx8mn_bl31_bin, &bl31, &bl31_size);
	}

	imx8m_atf_start_bl31(bl31, bl31_size, (void *)MX8MN_ATF_BL31_BASE_ADDR);
}

void imx8mq_load_bl33(void *bl33)
{
	enum bootsource src;
	int instance;

	imx8mq_get_boot_source(&src, &instance);
	switch (src) {
	case BOOTSOURCE_MMC:
		imx8m_esdhc_load_image(instance, bl33);
		break;
	default:
		printf("Unhandled bootsource BOOTSOURCE_%d\n", src);
		hang();
	}


	/*
	 * On completion the TF-A will jump to MX8M_ATF_BL33_BASE_ADDR
	 * in EL2. Copy the image there, but replace the PBL part of
	 * that image with ourselves. On a high assurance boot only the
	 * currently running code is validated and contains the checksum
	 * for the piggy data, so we need to ensure that we are running
	 * the same code in DRAM.
	 */
	memcpy(bl33, __image_start, barebox_pbl_size);
}

__noreturn void imx8mq_load_and_start_image_via_tfa(void)
{
	__imx8mq_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
}

__noreturn void __imx8mq_load_and_start_image_via_tfa(void *bl33)
{
	const void *bl31;
	size_t bl31_size;
	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);

	imx_set_cpu_type(IMX_CPU_IMX8MQ);
	imx8mq_init_scratch_space();
	imx8m_save_bootrom_log();
	imx8mq_load_bl33(bl33);

	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MQ_OPTEE)) {
		void *bl32 = (void *)arm_mem_optee(endmem);
		size_t bl32_size;
		void *bl32_image;

		imx8m_tzc380_init();
		get_builtin_firmware_ext(imx8mq_bl32_bin,
				bl33, &bl32_image,
				&bl32_size);

		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);

		memcpy(bl32, bl32_image, bl32_size);

		get_builtin_firmware(imx8mq_bl31_bin_optee, &bl31, &bl31_size);
	} else {
		get_builtin_firmware(imx8mq_bl31_bin, &bl31, &bl31_size);
	}

	imx8m_atf_start_bl31(bl31, bl31_size, (void *)MX8MQ_ATF_BL31_BASE_ADDR);
}

void __noreturn imx93_load_and_start_image_via_tfa(void)
{
	unsigned long atf_dest = MX93_ATF_BL31_BASE_ADDR;
	void __noreturn (*bl31)(void) = (void *)atf_dest;
	const void *tfa;
	size_t tfa_size;
	void *bl33 = (void *)MX93_ATF_BL33_BASE_ADDR;
	unsigned long endmem = MX9_DDR_CSD1_BASE_ADDR + imx9_ddrc_sdram_size();

	imx_set_cpu_type(IMX_CPU_IMX93);
	imx93_init_scratch_space(true);

	/*
	 * On completion the TF-A will jump to MX93_ATF_BL33_BASE_ADDR
	 * in EL2. Copy the image there, but replace the PBL part of
	 * that image with ourselves. On a high assurance boot only the
	 * currently running code is validated and contains the checksum
	 * for the piggy data, so we need to ensure that we are running
	 * the same code in DRAM.
	 *
	 * The second purpose of this memcpy is for USB booting. When booting
	 * from USB the image comes in as a stream, so the PBL is transferred
	 * only once. As we jump into the PBL again in SDRAM we need to copy
	 * it there. The USB protocol transfers data in chunks of 1024 bytes,
	 * so align the copy size up to the next 1KiB boundary.
	 */
	memcpy((void *)MX93_ATF_BL33_BASE_ADDR, __image_start, ALIGN(barebox_pbl_size, 1024));

	if (IS_ENABLED(CONFIG_FIRMWARE_IMX93_OPTEE)) {
		void *bl32 = (void *)arm_mem_optee(endmem);
		size_t bl32_size;
		void *bl32_image;

		imx93_ele_load_fw(bl33);

		get_builtin_firmware_ext(imx93_bl32_bin,
				bl33, &bl32_image,
				&bl32_size);

		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);

		memcpy(bl32, bl32_image, bl32_size);

		get_builtin_firmware(imx93_bl31_bin_optee, &tfa, &tfa_size);
	} else {
		get_builtin_firmware(imx93_bl31_bin, &tfa, &tfa_size);
	}

	memcpy(bl31, tfa, tfa_size);

	asm volatile("msr sp_el2, %0" : :
		     "r" (MX93_ATF_BL33_BASE_ADDR - 16) :
		     "cc");
	bl31();
	__builtin_unreachable();
}