1/* SPDX-License-Identifier: BSD-2-Clause */
2/*
3 * Copyright (c) 2021, EPAM Systems
4 */
5
6#include <asm.S>
7#include <arm.h>
8#include <arm64_macros.S>
9#include <kernel/cache_helpers.h>
10
11/* uint32_t __plat_romapi_wrapper(paddr_t func, uint64_t arg1, uint64_t arg2,
12 *				  uint64_t arg3)
13 * Call MaskROM function func(arg1, arg2, arg3).
14 * We need to disable MMU before calling any MaskROM API functions
15 */
16FUNC __plat_romapi_wrapper , : , .identity_map
17
18	push	fp,  lr
19	push	x19, x20
20	push	x21, x22
21	push	x23, x24
22
23	mov	x19, x0
24	mov	x20, x1
25	mov	x21, x2
26	mov	x22, x3
27
28	/* Get PA of stack pointer */
29	mov	x0, sp
30#ifdef CFG_CORE_ASLR
31	/*
32	 * We are running at identity location, so we can't use bl there,
33	 * because assembler will generate relative address to virt_to_phys(),
34	 * which is not identity mapped. So we need to use absolute address.
35	 */
36	ldr	x9, =virt_to_phys
37	blr	x9
38#else
39	bl	virt_to_phys
40#endif
41	mov	x23, x0
42
43	/* We about to disable MMU. Make sure that all writes reached memory */
44	mov     x0, #DCACHE_OP_CLEAN
45#ifdef CFG_CORE_ASLR
46	/* See the comment above */
47	ldr	x9, =dcache_op_all
48	blr	x9
49#else
50	bl	dcache_op_all
51#endif
52
53	/* Disable MMU */
54	mrs	x9, sctlr_el1
55	bic	x9, x9, #SCTLR_M
56	bic	x9, x9, #SCTLR_C
57	msr	sctlr_el1, x9
58	isb
59	/* Invalidate instruction cache and branch predictor */
60	ic	ialluis
61	dsb	ish	/* ensure that maintenance operations are seen */
62	isb
63
64	/* Save old SP to x24 and switch to a new stack */
65	mov	x24, sp
66	mov	sp, x23
67
68	/* call the function */
69	mov	x0, x20 	/* x20: uint64_t arg1 */
70	mov	x1, x21		/* x21: uint64_t arg2 */
71	mov	x2, x22		/* x22: uint64_t arg3 */
72	blr	x19		/* x19: paddr_t func */
73
74	/* restore sp */
75	mov	sp, x24
76
77	/* Enable MMU */
78	mrs	x9, sctlr_el1
79	orr	x9, x9, #SCTLR_M
80	orr	x9, x9, #SCTLR_C
81	msr	sctlr_el1, x9
82	isb
83
84	/* Invalidate instruction cache and branch predictor */
85	ic	iallu
86	isb
87
88	pop	x23, x24
89	pop	x21, x22
90	pop	x19, x20
91	pop	fp, lr
92	ret
93END_FUNC __plat_romapi_wrapper
94