xref: /rk3399_ARM-atf/lib/aarch64/misc_helpers.S (revision 931f7c615643e5d0fb2cbab68e4093c980b0e271)
14ecca339SDan Handley/*
2*931f7c61SSoby Mathew * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
34ecca339SDan Handley *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
54ecca339SDan Handley */
64ecca339SDan Handley
797043ac9SDan Handley#include <arch.h>
84ecca339SDan Handley#include <asm_macros.S>
9bc920128SSoby Mathew#include <assert_macros.S>
10*931f7c61SSoby Mathew#include <xlat_tables_defs.h>
114ecca339SDan Handley
124ecca339SDan Handley	.globl	get_afflvl_shift
134ecca339SDan Handley	.globl	mpidr_mask_lower_afflvls
144ecca339SDan Handley	.globl	eret
154ecca339SDan Handley	.globl	smc
164ecca339SDan Handley
17308d359bSDouglas Raillard	.globl	zero_normalmem
18308d359bSDouglas Raillard	.globl	zeromem
194ecca339SDan Handley	.globl	zeromem16
204ecca339SDan Handley	.globl	memcpy16
214ecca339SDan Handley
22ec0c8fdaSAntonio Nino Diaz	.globl	disable_mmu_el1
232f5dcfefSAndrew Thoelke	.globl	disable_mmu_el3
24ec0c8fdaSAntonio Nino Diaz	.globl	disable_mmu_icache_el1
252f5dcfefSAndrew Thoelke	.globl	disable_mmu_icache_el3
262f5dcfefSAndrew Thoelke
27*931f7c61SSoby Mathew	.globl	fixup_gdt_reloc
28*931f7c61SSoby Mathew
295c3272a7SAndrew Thoelke#if SUPPORT_VFP
305c3272a7SAndrew Thoelke	.globl	enable_vfp
315c3272a7SAndrew Thoelke#endif
325c3272a7SAndrew Thoelke
334ecca339SDan Handleyfunc get_afflvl_shift
344ecca339SDan Handley	cmp	x0, #3
354ecca339SDan Handley	cinc	x0, x0, eq
364ecca339SDan Handley	mov	x1, #MPIDR_AFFLVL_SHIFT
374ecca339SDan Handley	lsl	x0, x0, x1
384ecca339SDan Handley	ret
398b779620SKévin Petitendfunc get_afflvl_shift
404ecca339SDan Handley
414ecca339SDan Handleyfunc mpidr_mask_lower_afflvls
424ecca339SDan Handley	cmp	x1, #3
434ecca339SDan Handley	cinc	x1, x1, eq
444ecca339SDan Handley	mov	x2, #MPIDR_AFFLVL_SHIFT
454ecca339SDan Handley	lsl	x2, x1, x2
464ecca339SDan Handley	lsr	x0, x0, x2
474ecca339SDan Handley	lsl	x0, x0, x2
484ecca339SDan Handley	ret
498b779620SKévin Petitendfunc mpidr_mask_lower_afflvls
504ecca339SDan Handley
514ecca339SDan Handley
524ecca339SDan Handleyfunc eret
534ecca339SDan Handley	eret
548b779620SKévin Petitendfunc eret
554ecca339SDan Handley
564ecca339SDan Handley
574ecca339SDan Handleyfunc smc
584ecca339SDan Handley	smc	#0
598b779620SKévin Petitendfunc smc
604ecca339SDan Handley
614ecca339SDan Handley/* -----------------------------------------------------------------------
62308d359bSDouglas Raillard * void zero_normalmem(void *mem, unsigned int length);
63308d359bSDouglas Raillard *
64308d359bSDouglas Raillard * Initialise a region in normal memory to 0. This functions complies with the
65308d359bSDouglas Raillard * AAPCS and can be called from C code.
66308d359bSDouglas Raillard *
67308d359bSDouglas Raillard * NOTE: MMU must be enabled when using this function as it can only operate on
68308d359bSDouglas Raillard *       normal memory. It is intended to be mainly used from C code when MMU
69308d359bSDouglas Raillard *       is usually enabled.
70308d359bSDouglas Raillard * -----------------------------------------------------------------------
71308d359bSDouglas Raillard */
72308d359bSDouglas Raillard.equ	zero_normalmem, zeromem_dczva
73308d359bSDouglas Raillard
74308d359bSDouglas Raillard/* -----------------------------------------------------------------------
75308d359bSDouglas Raillard * void zeromem(void *mem, unsigned int length);
76308d359bSDouglas Raillard *
77308d359bSDouglas Raillard * Initialise a region of device memory to 0. This functions complies with the
78308d359bSDouglas Raillard * AAPCS and can be called from C code.
79308d359bSDouglas Raillard *
80308d359bSDouglas Raillard * NOTE: When data caches and MMU are enabled, zero_normalmem can usually be
81308d359bSDouglas Raillard *       used instead for faster zeroing.
82308d359bSDouglas Raillard *
83308d359bSDouglas Raillard * -----------------------------------------------------------------------
84308d359bSDouglas Raillard */
85308d359bSDouglas Raillardfunc zeromem
86308d359bSDouglas Raillard	/* x2 is the address past the last zeroed address */
87308d359bSDouglas Raillard	add	x2, x0, x1
88308d359bSDouglas Raillard	/*
89308d359bSDouglas Raillard	 * Uses the fallback path that does not use DC ZVA instruction and
90308d359bSDouglas Raillard	 * therefore does not need enabled MMU
91308d359bSDouglas Raillard	 */
92308d359bSDouglas Raillard	b	.Lzeromem_dczva_fallback_entry
93308d359bSDouglas Raillardendfunc zeromem
94308d359bSDouglas Raillard
95308d359bSDouglas Raillard/* -----------------------------------------------------------------------
96308d359bSDouglas Raillard * void zeromem_dczva(void *mem, unsigned int length);
97308d359bSDouglas Raillard *
98308d359bSDouglas Raillard * Fill a region of normal memory of size "length" in bytes with null bytes.
99308d359bSDouglas Raillard * MMU must be enabled and the memory be of
100308d359bSDouglas Raillard * normal type. This is because this function internally uses the DC ZVA
101308d359bSDouglas Raillard * instruction, which generates an Alignment fault if used on any type of
102308d359bSDouglas Raillard * Device memory (see section D3.4.9 of the ARMv8 ARM, issue k). When the MMU
103308d359bSDouglas Raillard * is disabled, all memory behaves like Device-nGnRnE memory (see section
104308d359bSDouglas Raillard * D4.2.8), hence the requirement on the MMU being enabled.
105308d359bSDouglas Raillard * NOTE: The code assumes that the block size as defined in DCZID_EL0
106308d359bSDouglas Raillard *       register is at least 16 bytes.
107308d359bSDouglas Raillard *
108308d359bSDouglas Raillard * -----------------------------------------------------------------------
109308d359bSDouglas Raillard */
110308d359bSDouglas Raillardfunc zeromem_dczva
111308d359bSDouglas Raillard
112308d359bSDouglas Raillard	/*
113308d359bSDouglas Raillard	 * The function consists of a series of loops that zero memory one byte
114308d359bSDouglas Raillard	 * at a time, 16 bytes at a time or using the DC ZVA instruction to
115308d359bSDouglas Raillard	 * zero aligned block of bytes, which is assumed to be more than 16.
116308d359bSDouglas Raillard	 * In the case where the DC ZVA instruction cannot be used or if the
117308d359bSDouglas Raillard	 * first 16 bytes loop would overflow, there is fallback path that does
118308d359bSDouglas Raillard	 * not use DC ZVA.
119308d359bSDouglas Raillard	 * Note: The fallback path is also used by the zeromem function that
120308d359bSDouglas Raillard	 *       branches to it directly.
121308d359bSDouglas Raillard	 *
122308d359bSDouglas Raillard	 *              +---------+   zeromem_dczva
123308d359bSDouglas Raillard	 *              |  entry  |
124308d359bSDouglas Raillard	 *              +----+----+
125308d359bSDouglas Raillard	 *                   |
126308d359bSDouglas Raillard	 *                   v
127308d359bSDouglas Raillard	 *              +---------+
128308d359bSDouglas Raillard	 *              | checks  |>o-------+ (If any check fails, fallback)
129308d359bSDouglas Raillard	 *              +----+----+         |
130308d359bSDouglas Raillard	 *                   |              |---------------+
131308d359bSDouglas Raillard	 *                   v              | Fallback path |
132308d359bSDouglas Raillard	 *            +------+------+       |---------------+
133308d359bSDouglas Raillard	 *            | 1 byte loop |       |
134308d359bSDouglas Raillard	 *            +------+------+ .Lzeromem_dczva_initial_1byte_aligned_end
135308d359bSDouglas Raillard	 *                   |              |
136308d359bSDouglas Raillard	 *                   v              |
137308d359bSDouglas Raillard	 *           +-------+-------+      |
138308d359bSDouglas Raillard	 *           | 16 bytes loop |      |
139308d359bSDouglas Raillard	 *           +-------+-------+      |
140308d359bSDouglas Raillard	 *                   |              |
141308d359bSDouglas Raillard	 *                   v              |
142308d359bSDouglas Raillard	 *            +------+------+ .Lzeromem_dczva_blocksize_aligned
143308d359bSDouglas Raillard	 *            | DC ZVA loop |       |
144308d359bSDouglas Raillard	 *            +------+------+       |
145308d359bSDouglas Raillard	 *       +--------+  |              |
146308d359bSDouglas Raillard	 *       |        |  |              |
147308d359bSDouglas Raillard	 *       |        v  v              |
148308d359bSDouglas Raillard	 *       |   +-------+-------+ .Lzeromem_dczva_final_16bytes_aligned
149308d359bSDouglas Raillard	 *       |   | 16 bytes loop |      |
150308d359bSDouglas Raillard	 *       |   +-------+-------+      |
151308d359bSDouglas Raillard	 *       |           |              |
152308d359bSDouglas Raillard	 *       |           v              |
153308d359bSDouglas Raillard	 *       |    +------+------+ .Lzeromem_dczva_final_1byte_aligned
154308d359bSDouglas Raillard	 *       |    | 1 byte loop |       |
155308d359bSDouglas Raillard	 *       |    +-------------+       |
156308d359bSDouglas Raillard	 *       |           |              |
157308d359bSDouglas Raillard	 *       |           v              |
158308d359bSDouglas Raillard	 *       |       +---+--+           |
159308d359bSDouglas Raillard	 *       |       | exit |           |
160308d359bSDouglas Raillard	 *       |       +------+           |
161308d359bSDouglas Raillard	 *       |			    |
162308d359bSDouglas Raillard	 *       |           +--------------+    +------------------+ zeromem
163308d359bSDouglas Raillard	 *       |           |  +----------------| zeromem function |
164308d359bSDouglas Raillard	 *       |           |  |                +------------------+
165308d359bSDouglas Raillard	 *       |           v  v
166308d359bSDouglas Raillard	 *       |    +-------------+ .Lzeromem_dczva_fallback_entry
167308d359bSDouglas Raillard	 *       |    | 1 byte loop |
168308d359bSDouglas Raillard	 *       |    +------+------+
169308d359bSDouglas Raillard	 *       |           |
170308d359bSDouglas Raillard	 *       +-----------+
171308d359bSDouglas Raillard	 */
172308d359bSDouglas Raillard
173308d359bSDouglas Raillard	/*
174308d359bSDouglas Raillard	 * Readable names for registers
175308d359bSDouglas Raillard	 *
176308d359bSDouglas Raillard	 * Registers x0, x1 and x2 are also set by zeromem which
177308d359bSDouglas Raillard	 * branches into the fallback path directly, so cursor, length and
178308d359bSDouglas Raillard	 * stop_address should not be retargeted to other registers.
179308d359bSDouglas Raillard	 */
180308d359bSDouglas Raillard	cursor       .req x0 /* Start address and then current address */
181308d359bSDouglas Raillard	length       .req x1 /* Length in bytes of the region to zero out */
182308d359bSDouglas Raillard	/* Reusing x1 as length is never used after block_mask is set */
183308d359bSDouglas Raillard	block_mask   .req x1 /* Bitmask of the block size read in DCZID_EL0 */
184308d359bSDouglas Raillard	stop_address .req x2 /* Address past the last zeroed byte */
185308d359bSDouglas Raillard	block_size   .req x3 /* Size of a block in bytes as read in DCZID_EL0 */
186308d359bSDouglas Raillard	tmp1         .req x4
187308d359bSDouglas Raillard	tmp2         .req x5
188308d359bSDouglas Raillard
189044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
190308d359bSDouglas Raillard	/*
191308d359bSDouglas Raillard	 * Check for M bit (MMU enabled) of the current SCTLR_EL(1|3)
192308d359bSDouglas Raillard	 * register value and panic if the MMU is disabled.
193308d359bSDouglas Raillard	 */
19479c7e728SMasahiro Yamada#if defined(IMAGE_BL1) || defined(IMAGE_BL31) || (defined(IMAGE_BL2) && BL2_AT_EL3)
195308d359bSDouglas Raillard	mrs	tmp1, sctlr_el3
196308d359bSDouglas Raillard#else
197308d359bSDouglas Raillard	mrs	tmp1, sctlr_el1
198308d359bSDouglas Raillard#endif
199308d359bSDouglas Raillard
200308d359bSDouglas Raillard	tst	tmp1, #SCTLR_M_BIT
201308d359bSDouglas Raillard	ASM_ASSERT(ne)
202044bb2faSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */
203308d359bSDouglas Raillard
204308d359bSDouglas Raillard	/* stop_address is the address past the last to zero */
205308d359bSDouglas Raillard	add	stop_address, cursor, length
206308d359bSDouglas Raillard
207308d359bSDouglas Raillard	/*
208308d359bSDouglas Raillard	 * Get block_size = (log2(<block size>) >> 2) (see encoding of
209308d359bSDouglas Raillard	 * dczid_el0 reg)
210308d359bSDouglas Raillard	 */
211308d359bSDouglas Raillard	mrs	block_size, dczid_el0
212308d359bSDouglas Raillard
213308d359bSDouglas Raillard	/*
214308d359bSDouglas Raillard	 * Select the 4 lowest bits and convert the extracted log2(<block size
215308d359bSDouglas Raillard	 * in words>) to <block size in bytes>
216308d359bSDouglas Raillard	 */
217308d359bSDouglas Raillard	ubfx	block_size, block_size, #0, #4
218308d359bSDouglas Raillard	mov	tmp2, #(1 << 2)
219308d359bSDouglas Raillard	lsl	block_size, tmp2, block_size
220308d359bSDouglas Raillard
221044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
222308d359bSDouglas Raillard	/*
223308d359bSDouglas Raillard	 * Assumes block size is at least 16 bytes to avoid manual realignment
224308d359bSDouglas Raillard	 * of the cursor at the end of the DCZVA loop.
225308d359bSDouglas Raillard	 */
226308d359bSDouglas Raillard	cmp	block_size, #16
227308d359bSDouglas Raillard	ASM_ASSERT(hs)
228308d359bSDouglas Raillard#endif
229308d359bSDouglas Raillard	/*
230308d359bSDouglas Raillard	 * Not worth doing all the setup for a region less than a block and
231308d359bSDouglas Raillard	 * protects against zeroing a whole block when the area to zero is
232308d359bSDouglas Raillard	 * smaller than that. Also, as it is assumed that the block size is at
233308d359bSDouglas Raillard	 * least 16 bytes, this also protects the initial aligning loops from
234308d359bSDouglas Raillard	 * trying to zero 16 bytes when length is less than 16.
235308d359bSDouglas Raillard	 */
236308d359bSDouglas Raillard	cmp	length, block_size
237308d359bSDouglas Raillard	b.lo	.Lzeromem_dczva_fallback_entry
238308d359bSDouglas Raillard
239308d359bSDouglas Raillard	/*
240308d359bSDouglas Raillard	 * Calculate the bitmask of the block alignment. It will never
241308d359bSDouglas Raillard	 * underflow as the block size is between 4 bytes and 2kB.
242308d359bSDouglas Raillard	 * block_mask = block_size - 1
243308d359bSDouglas Raillard	 */
244308d359bSDouglas Raillard	sub	block_mask, block_size, #1
245308d359bSDouglas Raillard
246308d359bSDouglas Raillard	/*
247308d359bSDouglas Raillard	 * length alias should not be used after this point unless it is
248308d359bSDouglas Raillard	 * defined as a register other than block_mask's.
249308d359bSDouglas Raillard	 */
250308d359bSDouglas Raillard	 .unreq length
251308d359bSDouglas Raillard
252308d359bSDouglas Raillard	/*
253308d359bSDouglas Raillard	 * If the start address is already aligned to zero block size, go
254308d359bSDouglas Raillard	 * straight to the cache zeroing loop. This is safe because at this
255308d359bSDouglas Raillard	 * point, the length cannot be smaller than a block size.
256308d359bSDouglas Raillard	 */
257308d359bSDouglas Raillard	tst	cursor, block_mask
258308d359bSDouglas Raillard	b.eq	.Lzeromem_dczva_blocksize_aligned
259308d359bSDouglas Raillard
260308d359bSDouglas Raillard	/*
261308d359bSDouglas Raillard	 * Calculate the first block-size-aligned address. It is assumed that
262308d359bSDouglas Raillard	 * the zero block size is at least 16 bytes. This address is the last
263308d359bSDouglas Raillard	 * address of this initial loop.
264308d359bSDouglas Raillard	 */
265308d359bSDouglas Raillard	orr	tmp1, cursor, block_mask
266308d359bSDouglas Raillard	add	tmp1, tmp1, #1
267308d359bSDouglas Raillard
268308d359bSDouglas Raillard	/*
269308d359bSDouglas Raillard	 * If the addition overflows, skip the cache zeroing loops. This is
270308d359bSDouglas Raillard	 * quite unlikely however.
271308d359bSDouglas Raillard	 */
272308d359bSDouglas Raillard	cbz	tmp1, .Lzeromem_dczva_fallback_entry
273308d359bSDouglas Raillard
274308d359bSDouglas Raillard	/*
275308d359bSDouglas Raillard	 * If the first block-size-aligned address is past the last address,
276308d359bSDouglas Raillard	 * fallback to the simpler code.
277308d359bSDouglas Raillard	 */
278308d359bSDouglas Raillard	cmp	tmp1, stop_address
279308d359bSDouglas Raillard	b.hi	.Lzeromem_dczva_fallback_entry
280308d359bSDouglas Raillard
281308d359bSDouglas Raillard	/*
282308d359bSDouglas Raillard	 * If the start address is already aligned to 16 bytes, skip this loop.
283308d359bSDouglas Raillard	 * It is safe to do this because tmp1 (the stop address of the initial
284308d359bSDouglas Raillard	 * 16 bytes loop) will never be greater than the final stop address.
285308d359bSDouglas Raillard	 */
286308d359bSDouglas Raillard	tst	cursor, #0xf
287308d359bSDouglas Raillard	b.eq	.Lzeromem_dczva_initial_1byte_aligned_end
288308d359bSDouglas Raillard
289308d359bSDouglas Raillard	/* Calculate the next address aligned to 16 bytes */
290308d359bSDouglas Raillard	orr	tmp2, cursor, #0xf
291308d359bSDouglas Raillard	add	tmp2, tmp2, #1
292308d359bSDouglas Raillard	/* If it overflows, fallback to the simple path (unlikely) */
293308d359bSDouglas Raillard	cbz	tmp2, .Lzeromem_dczva_fallback_entry
294308d359bSDouglas Raillard	/*
295308d359bSDouglas Raillard	 * Next aligned address cannot be after the stop address because the
296308d359bSDouglas Raillard	 * length cannot be smaller than 16 at this point.
297308d359bSDouglas Raillard	 */
298308d359bSDouglas Raillard
299308d359bSDouglas Raillard	/* First loop: zero byte per byte */
300308d359bSDouglas Raillard1:
301308d359bSDouglas Raillard	strb	wzr, [cursor], #1
302308d359bSDouglas Raillard	cmp	cursor, tmp2
303308d359bSDouglas Raillard	b.ne	1b
304308d359bSDouglas Raillard.Lzeromem_dczva_initial_1byte_aligned_end:
305308d359bSDouglas Raillard
306308d359bSDouglas Raillard	/*
307308d359bSDouglas Raillard	 * Second loop: we need to zero 16 bytes at a time from cursor to tmp1
308308d359bSDouglas Raillard	 * before being able to use the code that deals with block-size-aligned
309308d359bSDouglas Raillard	 * addresses.
310308d359bSDouglas Raillard	 */
311308d359bSDouglas Raillard	cmp	cursor, tmp1
312308d359bSDouglas Raillard	b.hs	2f
313308d359bSDouglas Raillard1:
314308d359bSDouglas Raillard	stp	xzr, xzr, [cursor], #16
315308d359bSDouglas Raillard	cmp	cursor, tmp1
316308d359bSDouglas Raillard	b.lo	1b
317308d359bSDouglas Raillard2:
318308d359bSDouglas Raillard
319308d359bSDouglas Raillard	/*
320308d359bSDouglas Raillard	 * Third loop: zero a block at a time using DC ZVA cache block zeroing
321308d359bSDouglas Raillard	 * instruction.
322308d359bSDouglas Raillard	 */
323308d359bSDouglas Raillard.Lzeromem_dczva_blocksize_aligned:
324308d359bSDouglas Raillard	/*
325308d359bSDouglas Raillard	 * Calculate the last block-size-aligned address. If the result equals
326308d359bSDouglas Raillard	 * to the start address, the loop will exit immediately.
327308d359bSDouglas Raillard	 */
328308d359bSDouglas Raillard	bic	tmp1, stop_address, block_mask
329308d359bSDouglas Raillard
330308d359bSDouglas Raillard	cmp	cursor, tmp1
331308d359bSDouglas Raillard	b.hs	2f
332308d359bSDouglas Raillard1:
333308d359bSDouglas Raillard	/* Zero the block containing the cursor */
334308d359bSDouglas Raillard	dc	zva, cursor
335308d359bSDouglas Raillard	/* Increment the cursor by the size of a block */
336308d359bSDouglas Raillard	add	cursor, cursor, block_size
337308d359bSDouglas Raillard	cmp	cursor, tmp1
338308d359bSDouglas Raillard	b.lo	1b
339308d359bSDouglas Raillard2:
340308d359bSDouglas Raillard
341308d359bSDouglas Raillard	/*
342308d359bSDouglas Raillard	 * Fourth loop: zero 16 bytes at a time and then byte per byte the
343308d359bSDouglas Raillard	 * remaining area
344308d359bSDouglas Raillard	 */
345308d359bSDouglas Raillard.Lzeromem_dczva_final_16bytes_aligned:
346308d359bSDouglas Raillard	/*
347308d359bSDouglas Raillard	 * Calculate the last 16 bytes aligned address. It is assumed that the
348308d359bSDouglas Raillard	 * block size will never be smaller than 16 bytes so that the current
349308d359bSDouglas Raillard	 * cursor is aligned to at least 16 bytes boundary.
350308d359bSDouglas Raillard	 */
351308d359bSDouglas Raillard	bic	tmp1, stop_address, #15
352308d359bSDouglas Raillard
353308d359bSDouglas Raillard	cmp	cursor, tmp1
354308d359bSDouglas Raillard	b.hs	2f
355308d359bSDouglas Raillard1:
356308d359bSDouglas Raillard	stp	xzr, xzr, [cursor], #16
357308d359bSDouglas Raillard	cmp	cursor, tmp1
358308d359bSDouglas Raillard	b.lo	1b
359308d359bSDouglas Raillard2:
360308d359bSDouglas Raillard
361308d359bSDouglas Raillard	/* Fifth and final loop: zero byte per byte */
362308d359bSDouglas Raillard.Lzeromem_dczva_final_1byte_aligned:
363308d359bSDouglas Raillard	cmp	cursor, stop_address
364308d359bSDouglas Raillard	b.eq	2f
365308d359bSDouglas Raillard1:
366308d359bSDouglas Raillard	strb	wzr, [cursor], #1
367308d359bSDouglas Raillard	cmp	cursor, stop_address
368308d359bSDouglas Raillard	b.ne	1b
369308d359bSDouglas Raillard2:
370308d359bSDouglas Raillard	ret
371308d359bSDouglas Raillard
372308d359bSDouglas Raillard	/* Fallback for unaligned start addresses */
373308d359bSDouglas Raillard.Lzeromem_dczva_fallback_entry:
374308d359bSDouglas Raillard	/*
375308d359bSDouglas Raillard	 * If the start address is already aligned to 16 bytes, skip this loop.
376308d359bSDouglas Raillard	 */
377308d359bSDouglas Raillard	tst	cursor, #0xf
378308d359bSDouglas Raillard	b.eq	.Lzeromem_dczva_final_16bytes_aligned
379308d359bSDouglas Raillard
380308d359bSDouglas Raillard	/* Calculate the next address aligned to 16 bytes */
381308d359bSDouglas Raillard	orr	tmp1, cursor, #15
382308d359bSDouglas Raillard	add	tmp1, tmp1, #1
383308d359bSDouglas Raillard	/* If it overflows, fallback to byte per byte zeroing */
384308d359bSDouglas Raillard	cbz	tmp1, .Lzeromem_dczva_final_1byte_aligned
385308d359bSDouglas Raillard	/* If the next aligned address is after the stop address, fall back */
386308d359bSDouglas Raillard	cmp	tmp1, stop_address
387308d359bSDouglas Raillard	b.hs	.Lzeromem_dczva_final_1byte_aligned
388308d359bSDouglas Raillard
389308d359bSDouglas Raillard	/* Fallback entry loop: zero byte per byte */
390308d359bSDouglas Raillard1:
391308d359bSDouglas Raillard	strb	wzr, [cursor], #1
392308d359bSDouglas Raillard	cmp	cursor, tmp1
393308d359bSDouglas Raillard	b.ne	1b
394308d359bSDouglas Raillard
395308d359bSDouglas Raillard	b	.Lzeromem_dczva_final_16bytes_aligned
396308d359bSDouglas Raillard
397308d359bSDouglas Raillard	.unreq	cursor
398308d359bSDouglas Raillard	/*
399308d359bSDouglas Raillard	 * length is already unreq'ed to reuse the register for another
400308d359bSDouglas Raillard	 * variable.
401308d359bSDouglas Raillard	 */
402308d359bSDouglas Raillard	.unreq	stop_address
403308d359bSDouglas Raillard	.unreq	block_size
404308d359bSDouglas Raillard	.unreq	block_mask
405308d359bSDouglas Raillard	.unreq	tmp1
406308d359bSDouglas Raillard	.unreq	tmp2
407308d359bSDouglas Raillardendfunc zeromem_dczva
4084ecca339SDan Handley
4094ecca339SDan Handley/* --------------------------------------------------------------------------
4104ecca339SDan Handley * void memcpy16(void *dest, const void *src, unsigned int length)
4114ecca339SDan Handley *
4124ecca339SDan Handley * Copy length bytes from memory area src to memory area dest.
4134ecca339SDan Handley * The memory areas should not overlap.
4144ecca339SDan Handley * Destination and source addresses must be 16-byte aligned.
4154ecca339SDan Handley * --------------------------------------------------------------------------
4164ecca339SDan Handley */
4174ecca339SDan Handleyfunc memcpy16
418044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
419bc920128SSoby Mathew	orr	x3, x0, x1
420bc920128SSoby Mathew	tst	x3, #0xf
421bc920128SSoby Mathew	ASM_ASSERT(eq)
422bc920128SSoby Mathew#endif
4234ecca339SDan Handley/* copy 16 bytes at a time */
4244ecca339SDan Handleym_loop16:
4254ecca339SDan Handley	cmp	x2, #16
426ea926532SDouglas Raillard	b.lo	m_loop1
4274ecca339SDan Handley	ldp	x3, x4, [x1], #16
4284ecca339SDan Handley	stp	x3, x4, [x0], #16
4294ecca339SDan Handley	sub	x2, x2, #16
4304ecca339SDan Handley	b	m_loop16
4314ecca339SDan Handley/* copy byte per byte */
4324ecca339SDan Handleym_loop1:
4334ecca339SDan Handley	cbz	x2, m_end
4344ecca339SDan Handley	ldrb	w3, [x1], #1
4354ecca339SDan Handley	strb	w3, [x0], #1
4364ecca339SDan Handley	subs	x2, x2, #1
4374ecca339SDan Handley	b.ne	m_loop1
4388b779620SKévin Petitm_end:
4398b779620SKévin Petit	ret
4408b779620SKévin Petitendfunc memcpy16
4412f5dcfefSAndrew Thoelke
4422f5dcfefSAndrew Thoelke/* ---------------------------------------------------------------------------
4432f5dcfefSAndrew Thoelke * Disable the MMU at EL3
4442f5dcfefSAndrew Thoelke * ---------------------------------------------------------------------------
4452f5dcfefSAndrew Thoelke */
4462f5dcfefSAndrew Thoelke
4472f5dcfefSAndrew Thoelkefunc disable_mmu_el3
4482f5dcfefSAndrew Thoelke	mov	x1, #(SCTLR_M_BIT | SCTLR_C_BIT)
449ec0c8fdaSAntonio Nino Diazdo_disable_mmu_el3:
4502f5dcfefSAndrew Thoelke	mrs	x0, sctlr_el3
4512f5dcfefSAndrew Thoelke	bic	x0, x0, x1
4522f5dcfefSAndrew Thoelke	msr	sctlr_el3, x0
453ec0c8fdaSAntonio Nino Diaz	isb	/* ensure MMU is off */
45454dc71e7SAchin Gupta	dsb	sy
45554dc71e7SAchin Gupta	ret
4568b779620SKévin Petitendfunc disable_mmu_el3
4572f5dcfefSAndrew Thoelke
4582f5dcfefSAndrew Thoelke
4592f5dcfefSAndrew Thoelkefunc disable_mmu_icache_el3
4602f5dcfefSAndrew Thoelke	mov	x1, #(SCTLR_M_BIT | SCTLR_C_BIT | SCTLR_I_BIT)
461ec0c8fdaSAntonio Nino Diaz	b	do_disable_mmu_el3
4628b779620SKévin Petitendfunc disable_mmu_icache_el3
4632f5dcfefSAndrew Thoelke
4645c3272a7SAndrew Thoelke/* ---------------------------------------------------------------------------
465ec0c8fdaSAntonio Nino Diaz * Disable the MMU at EL1
466ec0c8fdaSAntonio Nino Diaz * ---------------------------------------------------------------------------
467ec0c8fdaSAntonio Nino Diaz */
468ec0c8fdaSAntonio Nino Diaz
469ec0c8fdaSAntonio Nino Diazfunc disable_mmu_el1
470ec0c8fdaSAntonio Nino Diaz	mov	x1, #(SCTLR_M_BIT | SCTLR_C_BIT)
471ec0c8fdaSAntonio Nino Diazdo_disable_mmu_el1:
472ec0c8fdaSAntonio Nino Diaz	mrs	x0, sctlr_el1
473ec0c8fdaSAntonio Nino Diaz	bic	x0, x0, x1
474ec0c8fdaSAntonio Nino Diaz	msr	sctlr_el1, x0
475ec0c8fdaSAntonio Nino Diaz	isb	/* ensure MMU is off */
476ec0c8fdaSAntonio Nino Diaz	dsb	sy
477ec0c8fdaSAntonio Nino Diaz	ret
478ec0c8fdaSAntonio Nino Diazendfunc disable_mmu_el1
479ec0c8fdaSAntonio Nino Diaz
480ec0c8fdaSAntonio Nino Diaz
481ec0c8fdaSAntonio Nino Diazfunc disable_mmu_icache_el1
482ec0c8fdaSAntonio Nino Diaz	mov	x1, #(SCTLR_M_BIT | SCTLR_C_BIT | SCTLR_I_BIT)
483ec0c8fdaSAntonio Nino Diaz	b	do_disable_mmu_el1
484ec0c8fdaSAntonio Nino Diazendfunc disable_mmu_icache_el1
485ec0c8fdaSAntonio Nino Diaz
486ec0c8fdaSAntonio Nino Diaz/* ---------------------------------------------------------------------------
4875c3272a7SAndrew Thoelke * Enable the use of VFP at EL3
4885c3272a7SAndrew Thoelke * ---------------------------------------------------------------------------
4895c3272a7SAndrew Thoelke */
4905c3272a7SAndrew Thoelke#if SUPPORT_VFP
4915c3272a7SAndrew Thoelkefunc enable_vfp
4925c3272a7SAndrew Thoelke	mrs	x0, cpacr_el1
4935c3272a7SAndrew Thoelke	orr	x0, x0, #CPACR_VFP_BITS
4945c3272a7SAndrew Thoelke	msr	cpacr_el1, x0
4955c3272a7SAndrew Thoelke	mrs	x0, cptr_el3
4965c3272a7SAndrew Thoelke	mov	x1, #AARCH64_CPTR_TFP
4975c3272a7SAndrew Thoelke	bic	x0, x0, x1
4985c3272a7SAndrew Thoelke	msr	cptr_el3, x0
4995c3272a7SAndrew Thoelke	isb
5005c3272a7SAndrew Thoelke	ret
5018b779620SKévin Petitendfunc enable_vfp
5025c3272a7SAndrew Thoelke#endif
503*931f7c61SSoby Mathew
504*931f7c61SSoby Mathew/* ---------------------------------------------------------------------------
505*931f7c61SSoby Mathew * Helper to fixup Global Descriptor table (GDT) and dynamic relocations
506*931f7c61SSoby Mathew * (.rela.dyn) at runtime.
507*931f7c61SSoby Mathew *
508*931f7c61SSoby Mathew * This function is meant to be used when the firmware is compiled with -fpie
509*931f7c61SSoby Mathew * and linked with -pie options. We rely on the linker script exporting
510*931f7c61SSoby Mathew * appropriate markers for start and end of the section. For GOT, we
511*931f7c61SSoby Mathew * expect __GOT_START__ and __GOT_END__. Similarly for .rela.dyn, we expect
512*931f7c61SSoby Mathew * __RELA_START__ and __RELA_END__.
513*931f7c61SSoby Mathew *
514*931f7c61SSoby Mathew * The function takes the limits of the memory to apply fixups to as
515*931f7c61SSoby Mathew * arguments (which is usually the limits of the relocable BL image).
516*931f7c61SSoby Mathew *   x0 -  the start of the fixup region
517*931f7c61SSoby Mathew *   x1 -  the limit of the fixup region
518*931f7c61SSoby Mathew * These addresses have to be page (4KB aligned).
519*931f7c61SSoby Mathew * ---------------------------------------------------------------------------
520*931f7c61SSoby Mathew */
521*931f7c61SSoby Mathewfunc fixup_gdt_reloc
522*931f7c61SSoby Mathew	mov	x6, x0
523*931f7c61SSoby Mathew	mov	x7, x1
524*931f7c61SSoby Mathew
525*931f7c61SSoby Mathew	/* Test if the limits are 4K aligned */
526*931f7c61SSoby Mathew#if ENABLE_ASSERTIONS
527*931f7c61SSoby Mathew	orr	x0, x0, x1
528*931f7c61SSoby Mathew	tst	x0, #(PAGE_SIZE - 1)
529*931f7c61SSoby Mathew	ASM_ASSERT(eq)
530*931f7c61SSoby Mathew#endif
531*931f7c61SSoby Mathew	/*
532*931f7c61SSoby Mathew	 * Calculate the offset based on return address in x30.
533*931f7c61SSoby Mathew	 * Assume that this funtion is called within a page of the start of
534*931f7c61SSoby Mathew	 * of fixup region.
535*931f7c61SSoby Mathew	 */
536*931f7c61SSoby Mathew	and	x2, x30, #~(PAGE_SIZE - 1)
537*931f7c61SSoby Mathew	sub	x0, x2, x6	/* Diff(S) = Current Address - Compiled Address */
538*931f7c61SSoby Mathew
539*931f7c61SSoby Mathew	adrp	x1, __GOT_START__
540*931f7c61SSoby Mathew	add	x1, x1, :lo12:__GOT_START__
541*931f7c61SSoby Mathew	adrp	x2, __GOT_END__
542*931f7c61SSoby Mathew	add	x2, x2, :lo12:__GOT_END__
543*931f7c61SSoby Mathew
544*931f7c61SSoby Mathew	/*
545*931f7c61SSoby Mathew	 * GOT is an array of 64_bit addresses which must be fixed up as
546*931f7c61SSoby Mathew	 * new_addr = old_addr + Diff(S).
547*931f7c61SSoby Mathew	 * The new_addr is the address currently the binary is executing from
548*931f7c61SSoby Mathew	 * and old_addr is the address at compile time.
549*931f7c61SSoby Mathew	 */
550*931f7c61SSoby Mathew1:
551*931f7c61SSoby Mathew	ldr	x3, [x1]
552*931f7c61SSoby Mathew	/* Skip adding offset if address is < lower limit */
553*931f7c61SSoby Mathew	cmp	x3, x6
554*931f7c61SSoby Mathew	b.lo	2f
555*931f7c61SSoby Mathew	/* Skip adding offset if address is >= upper limit */
556*931f7c61SSoby Mathew	cmp	x3, x7
557*931f7c61SSoby Mathew	b.ge	2f
558*931f7c61SSoby Mathew	add	x3, x3, x0
559*931f7c61SSoby Mathew	str	x3, [x1]
560*931f7c61SSoby Mathew2:
561*931f7c61SSoby Mathew	add	x1, x1, #8
562*931f7c61SSoby Mathew	cmp	x1, x2
563*931f7c61SSoby Mathew	b.lo	1b
564*931f7c61SSoby Mathew
565*931f7c61SSoby Mathew	/* Starting dynamic relocations. Use adrp/adr to get RELA_START and END */
566*931f7c61SSoby Mathew	adrp	x1, __RELA_START__
567*931f7c61SSoby Mathew	add	x1, x1, :lo12:__RELA_START__
568*931f7c61SSoby Mathew	adrp	x2, __RELA_END__
569*931f7c61SSoby Mathew	add	x2, x2, :lo12:__RELA_END__
570*931f7c61SSoby Mathew	/*
571*931f7c61SSoby Mathew	 * According to ELF-64 specification, the RELA data structure is as
572*931f7c61SSoby Mathew	 * follows:
573*931f7c61SSoby Mathew	 *	typedef struct
574*931f7c61SSoby Mathew	 * 	{
575*931f7c61SSoby Mathew	 *		Elf64_Addr r_offset;
576*931f7c61SSoby Mathew	 *		Elf64_Xword r_info;
577*931f7c61SSoby Mathew	 *		Elf64_Sxword r_addend;
578*931f7c61SSoby Mathew	 *	} Elf64_Rela;
579*931f7c61SSoby Mathew	 *
580*931f7c61SSoby Mathew	 * r_offset is address of reference
581*931f7c61SSoby Mathew	 * r_info is symbol index and type of relocation (in this case
582*931f7c61SSoby Mathew	 * 0x403 which corresponds to R_AARCH64_RELATIV).
583*931f7c61SSoby Mathew	 * r_addend is constant part of expression.
584*931f7c61SSoby Mathew	 *
585*931f7c61SSoby Mathew	 * Size of Elf64_Rela structure is 24 bytes.
586*931f7c61SSoby Mathew	 */
587*931f7c61SSoby Mathew1:
588*931f7c61SSoby Mathew	/* Assert that the relocation type is R_AARCH64_RELATIV */
589*931f7c61SSoby Mathew#if ENABLE_ASSERTIONS
590*931f7c61SSoby Mathew	ldr	x3, [x1, #8]
591*931f7c61SSoby Mathew	cmp	x3, #0x403
592*931f7c61SSoby Mathew	ASM_ASSERT(eq)
593*931f7c61SSoby Mathew#endif
594*931f7c61SSoby Mathew	ldr	x3, [x1]	/* r_offset */
595*931f7c61SSoby Mathew	add	x3, x0, x3
596*931f7c61SSoby Mathew	ldr	x4, [x1, #16]	/* r_addend */
597*931f7c61SSoby Mathew
598*931f7c61SSoby Mathew	/* Skip adding offset if r_addend is < lower limit */
599*931f7c61SSoby Mathew	cmp	x4, x6
600*931f7c61SSoby Mathew	b.lo	2f
601*931f7c61SSoby Mathew	/* Skip adding offset if r_addend entry is >= upper limit */
602*931f7c61SSoby Mathew	cmp	x4, x7
603*931f7c61SSoby Mathew	b.ge	2f
604*931f7c61SSoby Mathew
605*931f7c61SSoby Mathew	add	x4, x0, x4	/* Diff(S) + r_addend */
606*931f7c61SSoby Mathew	str	x4, [x3]
607*931f7c61SSoby Mathew
608*931f7c61SSoby Mathew2:	add	x1, x1, #24
609*931f7c61SSoby Mathew	cmp	x1, x2
610*931f7c61SSoby Mathew	b.lo	1b
611*931f7c61SSoby Mathew
612*931f7c61SSoby Mathew	ret
613*931f7c61SSoby Mathewendfunc fixup_gdt_reloc
614